tutrcos
読み取り中…
検索中…
一致する文字列を見つけられません
ps3.hpp
[詳解]
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6
8#include "tutrcos/utility.hpp"
9
10#include <cstdio>
11
12namespace tutrcos {
13namespace module {
14
55class PS3 {
56public:
57 enum class Axis {
58 LEFT_X,
59 LEFT_Y,
60 RIGHT_X,
61 RIGHT_Y,
62 };
63
64 enum class Key {
65 UP,
66 DOWN,
67 RIGHT,
68 LEFT,
70 CROSS,
71 CIRCLE,
72 SQUARE = 8,
73 L1,
74 L2,
75 R1,
76 R2,
77 START,
78 SELECT,
79 };
80
81 PS3(peripheral::UART &uart) : uart_{uart} {}
82
83 void update() {
84 keys_prev_ = keys_;
85
86 while (true) {
87 for (size_t i = 0; i < 8; ++i) {
88 if (buf_[0] == 0x80 || !uart_.receive(buf_.data(), 1, 0)) {
89 break;
90 }
91 }
92 if (buf_[0] != 0x80 || !uart_.receive(buf_.data() + 1, 7, 0)) {
93 return;
94 }
95
96 uint8_t checksum = 0;
97 for (size_t i = 1; i < 7; ++i) {
98 checksum += buf_[i];
99 }
100
101 if ((checksum & 0x7F) == buf_[7]) {
102 keys_ = (buf_[1] << 8) | buf_[2];
103 if ((keys_ & 0x03) == 0x03) {
104 keys_ &= ~0x03;
105 keys_ |= 1 << 13;
106 }
107 if ((keys_ & 0x0C) == 0x0C) {
108 keys_ &= ~0x0C;
109 keys_ |= 1 << 14;
110 }
111 for (size_t i = 0; i < 4; ++i) {
112 axes_[i] = (static_cast<float>(buf_[i + 3]) - 64) / 64;
113 }
114 }
115
116 buf_.fill(0);
117 }
118 }
119
120 float get_axis(Axis axis) { return axes_[utility::to_underlying(axis)]; }
121
122 bool get_key(Key key) {
123 return (keys_ & (1 << utility::to_underlying(key))) != 0;
124 }
125
126 bool get_key_down(Key key) {
127 return ((keys_ ^ keys_prev_) & keys_ &
128 (1 << utility::to_underlying(key))) != 0;
129 }
130
131 bool get_key_up(Key key) {
132 return ((keys_ ^ keys_prev_) & keys_prev_ &
133 (1 << utility::to_underlying(key))) != 0;
134 }
135
136private:
137 peripheral::UART &uart_;
138 std::array<uint8_t, 8> buf_{};
139 std::array<float, 4> axes_{};
140 uint16_t keys_ = 0;
141 uint16_t keys_prev_ = 0;
142};
143
144} // namespace module
145} // namespace tutrcos
Definition ps3.hpp:55
Key
Definition ps3.hpp:64
bool get_key(Key key)
Definition ps3.hpp:122
float get_axis(Axis axis)
Definition ps3.hpp:120
Axis
Definition ps3.hpp:57
bool get_key_up(Key key)
Definition ps3.hpp:131
void update()
Definition ps3.hpp:83
bool get_key_down(Key key)
Definition ps3.hpp:126
PS3(peripheral::UART &uart)
Definition ps3.hpp:81
Definition uart.hpp:51
bool receive(uint8_t *data, size_t size, uint32_t timeout)
Definition uart.hpp:85
constexpr std::underlying_type_t< T > to_underlying(T value) noexcept
Definition utility.hpp:23
Definition kernel.hpp:7