-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleonardo_ser_to_key.ino
256 lines (225 loc) · 6.39 KB
/
leonardo_ser_to_key.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "HID-Project.h"
#include "Adafruit_NeoPixel.h"
#include <Wire.h>
#define PSOC_I2C_SLAVE_ADDRESS 0x08
#define BUFFER_SIZE 5
#define CIRCLE 2
#define CROSS 1
#define SQUARE 3
#define TRIANGLE 4
#define LL 19
#define LR 20
#define RL 21
#define RR 22
#define LU 15
#define LD 26
#define L1 5
#define R1 6
#define L2 17
#define R2 18
#define L3 9
#define R3 10
#define SHARE 7
#define OPTION 8
#define TOUCH 14
#define PS 13
#define CIRCLE_PIN 4
#define CROSS_PIN 5
#define SQUARE_PIN 6
#define TRIANGLE_PIN 7
#define LED_STRIP_PIN 0
#define TIMING_CHECK_PIN 8
#define PWM_PIN 9
#define SERIAL_DEBUG_PIN 10
#define EXT_POWER_CHECK_PIN 14
#define START_PIN 15
#define ENABLE_PIN 16
#define CIRCLE_LED_PIN 18
#define CROSS_LED_PIN 19
#define SQUARE_LED_PIN 20
#define TRIANGLE_LED_PIN 21
#define BUTTON_NUM 5
#define LED_NUM 4
#define LED_STRIP_NUM 30
#define MIN16 -32768
#define MAX16 32767
#define MIN8 -128
#define MAX8 127
const unsigned char axis_serial_table[8] = {
LU, LD, L2, R2, LL, LR, RL, RR
};
const unsigned char button_direct_table[8] = {
TRIANGLE, SQUARE, CROSS, CIRCLE, PS, 0, 0, 0
};
const unsigned char button_direct_logic = 0b00001000;
const int button_direct_pin_table[BUTTON_NUM] = {
TRIANGLE_PIN, SQUARE_PIN, CROSS_PIN, CIRCLE_PIN, START_PIN
};
const int led_direct_pin_table[LED_NUM] = {
TRIANGLE_LED_PIN, SQUARE_LED_PIN, CROSS_LED_PIN, CIRCLE_LED_PIN
};
unsigned char button_data_byte = 0;
int data_bytes_count = 0;
unsigned char serial_data_byte[BUFFER_SIZE] = {};
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_STRIP_NUM, LED_STRIP_PIN, NEO_GRB + NEO_KHZ800);
void selfTestLEDs(void);
unsigned char readDirectlyConnectedButtons(int *pin_table, unsigned char pin_logic);
void writeDirectlyConnectedLEDs(int *led_table, unsigned char led_data_byte);
void addHIDaxisReportFromTable(unsigned char serial_data_byte, unsigned char *button_table, int contents_of_table_num);
void addHIDreportFromTable(unsigned char serial_data_byte, unsigned char *button_table, int contents_of_table_num);
void sendRecievedI2CDataWithUART(unsigned char serial_data_byte[BUFFER_SIZE], int buffer_size);
void setup(void) {
for(int i = 0; i < BUTTON_NUM; i++) {
pinMode(button_direct_pin_table[i], INPUT_PULLUP);
}
for(int i = 0; i < LED_NUM; i++) {
pinMode(led_direct_pin_table[i], OUTPUT);
}
pinMode(TIMING_CHECK_PIN, OUTPUT);
pinMode(ENABLE_PIN, INPUT_PULLUP);
pinMode(SERIAL_DEBUG_PIN, INPUT_PULLUP);
// pinMode(PWM_PIN, OUTPUT);
// digitalWrite(PWM_PIN, HIGH);
analogWrite(PWM_PIN, 255);
Serial.begin(115200);
Wire.begin(); //このボードをI2Cマスターとして設定
Wire.setClock(400000L);
Gamepad.begin();
strip.begin();
strip.setBrightness(128);
strip.show();
selfTestLEDs();
}
void loop(void) {
digitalWrite(TIMING_CHECK_PIN, 1);
if(!digitalRead(ENABLE_PIN)) {
data_bytes_count = 0;
for(int i = 0; i < BUFFER_SIZE; i++) {
serial_data_byte[i] = 0;
}
Wire.requestFrom(PSOC_I2C_SLAVE_ADDRESS, BUFFER_SIZE);
button_data_byte = readDirectlyConnectedButtons(button_direct_pin_table, button_direct_logic);
writeDirectlyConnectedLEDs(led_direct_pin_table, button_data_byte);
while(Wire.available() && data_bytes_count < BUFFER_SIZE) { // シリアルで何らかの信号を受け取ったとき...
serial_data_byte[data_bytes_count] = Wire.read();
data_bytes_count++;
}
if(!digitalRead(SERIAL_DEBUG_PIN)) {
sendRecievedI2CDataWithUART(serial_data_byte, BUFFER_SIZE);
}
addHIDaxisReportFromTable(serial_data_byte[0], axis_serial_table, 8);
addHIDreportFromTable(button_data_byte, button_direct_table, BUTTON_NUM);
int touched = 0;
for(int i = 0; i < BUFFER_SIZE - 1; i++) {
for(int j = 0; j < 8; j++) {
if((serial_data_byte[i + 1] >> (7 - j)) & 0x01) {
strip.setPixelColor(i * 8 + j, 0x80, 0xFF, 0xCC);
touched = 1;
} else {
strip.setPixelColor(i * 8 + j, 0x0E, 0x10, 0x10);
}
}
}
if(touched == 0) {
for(int i = 0; i < LED_STRIP_NUM; i++) {
strip.setPixelColor(i, 0x1C, 0x20, 0x20);
}
}
digitalWrite(TIMING_CHECK_PIN, 0);
} else {
Gamepad.releaseAll();
}
strip.show();
Gamepad.write();
}
void selfTestLEDs(void) {
unsigned char data_byte;
for(int i = 0; i < LED_NUM + 1; i++) {
data_byte = 0x01 << (7 - i);
writeDirectlyConnectedLEDs(led_direct_pin_table, data_byte);
delay(200);
}
for(int i = 0; i < LED_STRIP_NUM; i++) {
for(int j = 0; j < LED_STRIP_NUM; j++) {
strip.setPixelColor(j, 0);
}
strip.setPixelColor(i, 0xFF, 0xFF, 0xFF);
strip.show();
delay(50);
}
}
unsigned char readDirectlyConnectedButtons(int *pin_table, unsigned char pin_logic) {
unsigned char result = 0;
for(int i = 0; i < BUTTON_NUM; i++) {
result |= (digitalRead(pin_table[i]) << (7 - i));
}
return result ^ pin_logic;
}
void writeDirectlyConnectedLEDs(int *led_table, unsigned char led_data_byte) {
for(int i = 0; i < LED_NUM; i++) {
digitalWrite(led_table[i], !((led_data_byte >> (7 - i)) & 0x01));
}
}
void addHIDaxisReportFromTable(unsigned char serial_data_byte, unsigned char *button_table, int contents_of_table_num) {
Gamepad.xAxis(0);
Gamepad.yAxis(0);
Gamepad.zAxis(0);
Gamepad.rxAxis(0);
Gamepad.ryAxis(0);
Gamepad.rzAxis(0);
if((serial_data_byte >> 7) & 0x01) {
Gamepad.yAxis(MIN16);
}
if((serial_data_byte >> 6) & 0x01) {
Gamepad.yAxis(MAX16);
}
if((serial_data_byte >> 5) & 0x01) {
Gamepad.zAxis(MIN8);
}
if((serial_data_byte >> 4) & 0x01) {
Gamepad.zAxis(MIN8);
}
if((serial_data_byte >> 3) & 0x01) {
Gamepad.xAxis(MIN16);
}
if((serial_data_byte >> 2) & 0x01) {
Gamepad.xAxis(MAX16);
}
if((serial_data_byte >> 1) & 0x01) {
Gamepad.rxAxis(MIN16);
}
if((serial_data_byte >> 0) & 0x01) {
Gamepad.rxAxis(MAX16);
}
}
void addHIDreportFromTable(unsigned char serial_data_byte, unsigned char *button_table, int contents_of_table_num) {
for(int i = 0; i < contents_of_table_num; i++) {
if((serial_data_byte >> (7 - i)) & 0x01) {
Gamepad.press(button_table[i]);
} else {
Gamepad.release(button_table[i]);
}
}
}
void sendRecievedI2CDataWithUART(unsigned char serial_data_byte[BUFFER_SIZE], int buffer_size) {
int len = BUFFER_SIZE * 9 + 2;
char c;
char send_data[len];
for(int i = 0; i < buffer_size; i++) {
for(int j = 0; j < 9; j++) {
if(j < 8) {
if((serial_data_byte[i] >> (7 - j)) & 0x01) {
c = '@';
} else {
c = '_';
}
} else {
c = ' ';
}
send_data[i * 9 + j] = c;
}
}
send_data[len - 2] = '\r';
send_data[len - 1] = '\n';
Serial.write(send_data, len);
}