amiro-os / components / bluetooth / bluetooth-wiimote.cpp @ 309980f0
History | View | Annotate | Download (4.697 KB)
1 | 58fe0e0b | Thomas Schöpping | #include <ch.hpp> |
---|---|---|---|
2 | #include <hal.h> |
||
3 | |||
4 | #include <amiro/bluetooth/bluetooth-wiimote.hpp> |
||
5 | |||
6 | 61b0791a | Thomas Schöpping | #include <global.hpp> |
7 | |||
8 | 58fe0e0b | Thomas Schöpping | using namespace chibios_rt; |
9 | using namespace amiro; |
||
10 | |||
11 | 61b0791a | Thomas Schöpping | extern Global global;
|
12 | |||
13 | 58fe0e0b | Thomas Schöpping | /*
|
14 | * Class constructor
|
||
15 | */
|
||
16 | BluetoothWiimote::BluetoothWiimote(BLUETOOTH *bluetooth, uint8_t rxtx) : |
||
17 | BaseStaticThread<128>(), wiimoteConn(bluetooth, this, "WIIMOTE"), |
||
18 | mailbox(mailboxBuffer, BLUETOOTH_WIIMOTE_MAILBOX_SIZE), accelerometer{0,0,0} { |
||
19 | iwrap = &(bluetooth->iwrap); |
||
20 | rx_tx = rxtx; |
||
21 | linkId = 0xFF;
|
||
22 | stopflag = 0;
|
||
23 | } |
||
24 | |||
25 | //----------------------------------------------------------------
|
||
26 | |||
27 | msg_t BluetoothWiimote::main(void) {
|
||
28 | setName("BluetoothWiimote");
|
||
29 | |||
30 | while (!this->shouldTerminate()) { |
||
31 | if (wiimoteReceive())
|
||
32 | this->requestTerminate();
|
||
33 | } |
||
34 | return RDY_OK;
|
||
35 | } |
||
36 | |||
37 | /*
|
||
38 | * Member functions
|
||
39 | */
|
||
40 | msg_t BluetoothWiimote::wiimoteReceive() { |
||
41 | BluetoothDescriptor* recv_descriptor = NULL;
|
||
42 | uint8_t *buffer; |
||
43 | size_t length; |
||
44 | msg_t msg; |
||
45 | |||
46 | msg = mailbox.fetch((msg_t*) &recv_descriptor, TIME_INFINITE); |
||
47 | if ((msg == RDY_RESET) || stopflag)
|
||
48 | return RDY_RESET;
|
||
49 | |||
50 | buffer = recv_descriptor->bluetoothDescriptorGetPayload(); |
||
51 | length = recv_descriptor->bluetoothDescriptorGetPayloadLength(); |
||
52 | |||
53 | 61b0791a | Thomas Schöpping | if (buffer[0] == 0xA1 && (buffer[1] == 0x30 || buffer[1] == 0x31)) { |
54 | buttons.left = (buffer[2] & 0x01) ? 1 : 0; |
||
55 | buttons.right = (buffer[2] & 0x02) ? 1 : 0; |
||
56 | buttons.down = (buffer[2] & 0x04) ? 1 : 0; |
||
57 | buttons.up = (buffer[2] & 0x08) ? 1 : 0; |
||
58 | buttons.plus = (buffer[2] & 0x10) ? 1 : 0; |
||
59 | buttons.two = (buffer[3] & 0x01) ? 1 : 0; |
||
60 | buttons.one = (buffer[3] & 0x02) ? 1 : 0; |
||
61 | buttons.B = (buffer[3] & 0x04) ? 1 : 0; |
||
62 | buttons.A = (buffer[3] & 0x08) ? 1 : 0; |
||
63 | buttons.minus = (buffer[3] & 0x10) ? 1 : 0; |
||
64 | buttons.home = (buffer[3] & 0x80) ? 1 : 0; |
||
65 | |||
66 | accelerometer.x_axis = (buffer[4] << 2) + ((buffer[2] & 0x60) >> 5) - 0x1FF; |
||
67 | accelerometer.y_axis = (buffer[5] << 2) + ((buffer[3] & 0x20) >> 4) - 0x1FF; |
||
68 | accelerometer.z_axis = (buffer[6] << 2) + ((buffer[3] & 0x40) >> 5) - 0x1FF; |
||
69 | 58fe0e0b | Thomas Schöpping | |
70 | bluetoothWiimoteDataBtnAcc(); |
||
71 | } else {
|
||
72 | 61b0791a | Thomas Schöpping | chSequentialStreamWrite((BaseSequentialStream*) &global.sercanmux1, buffer, length); |
73 | 58fe0e0b | Thomas Schöpping | } |
74 | |||
75 | msg = iwrap->transport.bluetoothTransportGetStorageMailbox()->post((msg_t) recv_descriptor, TIME_INFINITE); |
||
76 | if ((msg == RDY_RESET) || stopflag)
|
||
77 | return RDY_RESET;
|
||
78 | |||
79 | return RDY_OK;
|
||
80 | } |
||
81 | |||
82 | msg_t BluetoothWiimote::wiimoteTransmit(const uint8_t* wiimotecmd, size_t length) {
|
||
83 | msg_t msg; |
||
84 | |||
85 | if ((rx_tx & 0x01) && (linkId != 0xFF) && (!stopflag)) |
||
86 | msg = iwrap->iwrapTransmit(linkId, wiimotecmd, length); |
||
87 | else
|
||
88 | msg = RDY_RESET; |
||
89 | |||
90 | return msg;
|
||
91 | } |
||
92 | |||
93 | void BluetoothWiimote::bluetoothWiimoteStart(uint8_t linkid) {
|
||
94 | linkId = linkid; |
||
95 | iwrap->transport.bluetoothTransportSetReceiveMailbox(linkId, &this->mailbox);
|
||
96 | |||
97 | if ((rx_tx & 0x02)) { // && (!stopflag) |
||
98 | this->start(NORMALPRIO);
|
||
99 | } |
||
100 | stopflag = 0;
|
||
101 | } |
||
102 | |||
103 | void BluetoothWiimote::bluetoothWiimoteStop() {
|
||
104 | linkId = 0xFF;
|
||
105 | stopflag = 1;
|
||
106 | mailbox.post(RDY_RESET, TIME_INFINITE); // TIME_IMMEDIATE TIME_INFINITE
|
||
107 | } |
||
108 | |||
109 | bool BluetoothWiimote::bluetoothWiimoteIsConnected() {
|
||
110 | if (linkId == 0xFF) |
||
111 | return false; |
||
112 | |||
113 | return true; |
||
114 | } |
||
115 | |||
116 | void BluetoothWiimote::bluetoothWiimoteListen(const char *addr) { |
||
117 | wiimoteConn.bluetoothConnectorListen(addr); |
||
118 | } |
||
119 | |||
120 | void BluetoothWiimote::bluetoothWiimoteConnect(const char *addr) { |
||
121 | wiimoteConn.bluetoothConnectorConnect(addr); |
||
122 | } |
||
123 | |||
124 | void BluetoothWiimote::bluetoothWiimoteDisconnect(const char *addr) { |
||
125 | wiimoteConn.bluetoothConnectorDisconnect(addr); |
||
126 | } |
||
127 | |||
128 | BluetoothWiimote::Accelerometer * BluetoothWiimote::getAccelerometer() { |
||
129 | return &accelerometer;
|
||
130 | } |
||
131 | |||
132 | 61b0791a | Thomas Schöpping | BluetoothWiimote::Buttons * BluetoothWiimote::getButtons() { |
133 | return &buttons;
|
||
134 | } |
||
135 | |||
136 | 58fe0e0b | Thomas Schöpping | /*
|
137 | * @brief : On-off LEDs and Motor of Wiimote.
|
||
138 | *
|
||
139 | * @param[in] ledid_flag On-off flag of LEDs 1,2,3,4
|
||
140 | * (LED1: 0x10, LED2: 0x20, LED3: 0x40, LED4: 0x80)
|
||
141 | * @param[in] vibrate_flag On-off flag of Vibration Motor (On: 1, off: 0)
|
||
142 | */
|
||
143 | void BluetoothWiimote::bluetoothWiimoteLedVibrate(uint8_t ledid_flag, uint8_t vibrate_flag) {
|
||
144 | const uint8_t data[] = {0xA2, 0x11, (uint8_t) (ledid_flag | vibrate_flag)}; |
||
145 | wiimoteTransmit(data, 3);
|
||
146 | } |
||
147 | |||
148 | /*
|
||
149 | * @brief : Show status information of Wiimote.
|
||
150 | */
|
||
151 | void BluetoothWiimote::bluetoothWiimoteStatusInfo() {
|
||
152 | const uint8_t data[] = {0xA2, 0x15, 0x00}; |
||
153 | wiimoteTransmit(data, 3);
|
||
154 | } |
||
155 | |||
156 | /*
|
||
157 | * @brief : Send button status (change event) of Wiimote.
|
||
158 | */
|
||
159 | void BluetoothWiimote::bluetoothWiimoteDataBtn() {
|
||
160 | const uint8_t data[] = {0xA2, 0x12, 0x00, 0x30}; |
||
161 | wiimoteTransmit(data, 4);
|
||
162 | } |
||
163 | |||
164 | /*
|
||
165 | * @brief : Send button & accelerometer status (change event) of Wiimote.
|
||
166 | */
|
||
167 | void BluetoothWiimote::bluetoothWiimoteDataBtnAcc() {
|
||
168 | const uint8_t data[] = {0xA2, 0x12, 0x00, 0x31}; |
||
169 | wiimoteTransmit(data, 4);
|
||
170 | } |