-
Notifications
You must be signed in to change notification settings - Fork 0
/
XIAO_BLE_MINI_LED_BADGE_01.ino
404 lines (352 loc) · 13.5 KB
/
XIAO_BLE_MINI_LED_BADGE_01.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Nora BADGE 4th
// Seeed XIAO BLE
// Import libraries (ArduinoBLE-Peripheral-LED)
#include <ArduinoBLE.h>
#include <string.h>
#include "NoraPeri.h"
// LED pin デバッグ用LED Pin
#define LED_PIN LED_BUILTIN // Red LED - LED_BUILTIN or LEDR, Green LED - LEDG, Blue LED - LEDB
// Import libraries (I2C 16x8 LED Matrix)
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <misakiUTF16.h> // for example
Adafruit_8x16matrix matrix = Adafruit_8x16matrix();
Adafruit_8x16matrix matrix1 = Adafruit_8x16matrix();
#define SCREENS 4 // LED Matrixカスケード接続数
// B1144format Header
#define ONE_CHAR_SIZE 11 //1文字分の文字データサイズ
#define MAX_WRITE_LENGTH 16 //BLE Writeデータ最大サイズ
/* 左、右、固定スクロール対応 Y.Iida */
#define MODE_LEFT 0
#define MODE_RIGHT 1
#define MAXSCREENS 4 // 最大接続数
static uint8_t buf[8*MAXSCREENS];
uint8_t* MAX7219_getBuffer() {
return buf;
}
// create service
BLEService ledService(BLE_SERVICE_UUID);
BLEService ledCharService(BLE_CHARACTERISTIC_SERVICE_UUID);
// create switch characteristic
unsigned char num = 16; //
int MaxWriteLen = 16; //Write受信サイズ
unsigned char LED_RecieveValue[16] = ""; //Write受信データ
int LED_RecieveLength = 0; //Write受信データサイズ
int ConcateCount = 0; //Write連結数(ヘッダ含む)
uint8_t LED_payload[1408] = {}; //ペイロード配列
unsigned int B1122_CharLength = 0; //ペイロードの文字数
unsigned int B1122_PayloadLines = 0; //ペーロード自体の数
unsigned int MAX7219__CharLength = 0; //ペイロードの文字数(LEDボード入力用)
bool isWritting = false; //Writeイベント発生フラグ
int margecount = 0; //マージ用ループカウンター
int PayloadLineCounter = 0; //ペイロードカウント
int SurplusLength = 0; //文字格納余り
int WritePayloadSize = 0; //ペーロードのサイズ ( 11 * 文字数 )
uint8_t MAX7219__payload[1024] = {}; //文字配列(LED出力用)
uint8_t display_Flash = 1; //フラッシュ(点滅)設定
uint8_t display_Marqee = 0; //マーキー設定
uint8_t display_speed_mode = 0x60; //表示スピード&モード
uint8_t save_Flash = 0; //フラッシュ(点滅)設定
uint8_t save_Marqee = 1; //マーキー設定
uint8_t save_speed_mode = 0x71; //表示スピード&モード
int blanklength = 32;
//16byteのデータ受信用Charasteristic
BLECharacteristic switchCharacteristic(BLE_CHARACTERISTIC_DATA_UUID, BLERead | BLEWrite | BLENotify, num);
//BLEByteCharacteristic switchCharacteristic(BLE_CHARACTERISTIC_DATA_UUID, BLERead | BLEWrite | BLENotify);
void setup() {
Serial.begin(115200); // BLE通信に必要
while (!Serial);
// for debug: set LED pin to output mode
pinMode(LED_PIN, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE module failed!");
while (1);
}
// set advertised local name and service UUID
BLE.setLocalName("LSLED");
BLE.setDeviceName("LSLED");
BLE.setAdvertisedService(ledService);
BLE.setAdvertisedService(ledCharService);
// add characteristic to the service
ledCharService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
BLE.addService(ledCharService);
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
// start advertising
BLE.advertise();
Serial.println("BLE device active, waiting for connections...");
int DefaultLength = 32;
memset(MAX7219__payload, '¥0', sizeof(MAX7219__payload));
uint8_t base[DefaultLength] = {0xc6, 0xe6, 0xe6, 0xf6, 0xde, 0xce, 0xce, 0xc6,
0xfe, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xfe,
0xfe, 0xc6, 0xc6, 0xfe, 0xd8, 0xcc, 0xc6, 0xc6,
0xfe, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0xc6
};
for(int basecount = 0; basecount < DefaultLength; basecount++){
MAX7219__payload[basecount] = base[basecount];
}
MAX7219__CharLength = 4;
// begin I2C 16x8 LED Matrix
Wire.begin(); // SDAとSCLのPIN指定ができるか要調査
matrix.begin(0x71);
matrix1.begin(0x70);
// matrix.setTextSize(1);
// matrix1.setTextSize(1);
matrix.setBrightness(0);
matrix1.setBrightness(0);
matrix.setTextWrap(false);
matrix1.setTextWrap(false);
matrix.setTextColor(LED_ON);
matrix1.setTextColor(LED_ON);
matrix.setRotation(1);
matrix1.setRotation(1);
}
void loop() {
// poll for BLE events
BLE.poll();
if(isWritting == false){
MAX7219Display(MAX7219__payload, MAX7219__CharLength, display_Flash, display_Marqee, display_speed_mode);
// TestLEDMatrix();
DebugLEDOnOff();
//delay(100);
}
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
// 書き込みフラグをON
isWritting = true;
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
// 書き込みフラグをOFF
isWritting = false;
}
//Writeイベント
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, update LED
Serial.println(F("Characteristic event, writen: "));
// 書き込みフラグをON
//isWritting = true;
//Write受信データサイズ
LED_RecieveLength = characteristic.valueLength();
//Writeデータ最大サイズ(16)回ループ
//LED_RecieveValue[16] = {};
memset(LED_RecieveValue, '¥0', sizeof(LED_RecieveValue));
for(int loopcount = 0; loopcount < 16; loopcount++){
LED_RecieveValue[loopcount] = *(characteristic.value()+loopcount);
}
//ヘッダとペイロード切り分け、ヘッダは必要部分だけ抽出。ペイロードは配列に格納。
if (LED_RecieveValue[0] == 0x77) {
isWritting = true;
ConcateCount = 0;
PayloadLineCounter = 0;
save_Flash = LED_RecieveValue[6];
save_Marqee = LED_RecieveValue[7];
save_speed_mode = LED_RecieveValue[8];
//DebugLEDOnOff();
//ヘッダ抽出 ※後で配列でなく変数にする。
} else {
//Debug
//DebugLEDOnOff();
//ヘッダ抽出続き
if(ConcateCount == 1){
//ヘッダから文字数を取得
B1122_CharLength = LED_RecieveValue[1];
//ペーロードのサイズを算出 ( 文字数 * 11 )
WritePayloadSize = B1122_CharLength * ONE_CHAR_SIZE;
//ペイロードの数 16byteのペイロードの数を算出
//文字データ数(文字数x11Byte) / 16 + 1(あまりが出た時)
B1122_PayloadLines = WritePayloadSize / MAX_WRITE_LENGTH;
SurplusLength = WritePayloadSize % MAX_WRITE_LENGTH;
if( SurplusLength != 0){
B1122_PayloadLines = B1122_PayloadLines + 1;
}
//配列初期化(文字数 x 11)
//LED_payload[WritePayloadSize] = {};
memset(LED_payload, '¥0', sizeof(LED_payload));
//ペイロード格納
}else if(ConcateCount >= 4){
//最終行のあまりがでるまでPayloadをマージ格納
for(margecount = 0; margecount < MAX_WRITE_LENGTH; margecount++){
//ペイロードカウントがMAXペイロード数未満
if((PayloadLineCounter < B1122_PayloadLines)){
LED_payload[(PayloadLineCounter * MAX_WRITE_LENGTH) + margecount] = LED_RecieveValue[margecount];
}else{
//最後のPayload
//ループカウンタがデータが埋まってるところまでデータ移行
if(margecount < (MAX_WRITE_LENGTH - SurplusLength)){
LED_payload[(PayloadLineCounter * MAX_WRITE_LENGTH) + margecount] = LED_RecieveValue[margecount];
}
}
}
PayloadLineCounter++;
}
//
/**/
//連結カウンタが最大値(全データ受信)
if(PayloadLineCounter == B1122_PayloadLines){
int charcount = 0; //文字byteデータ格納ループカウンタ
int B1144dotcout = 0; //1文字(11byte)カウンタ
int MAX7219dotcout = 0; //1文字(8byte)カウンタ
//文字列(文字数x8)分の配列初期化
//MAX7219__payload[B1122_CharLength * 8] = {};
memset(MAX7219__payload, '¥0', sizeof(MAX7219__payload));
//文字数分ループ
//
//Debug ダミーデータ(NORA)を格納。
/**
MAX7219__payload[5*8] = {};
LED_payload[55] = {};
uint8_t deb[55] = {0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
0x00, 0x00, 0xfe, 0xc0, 0xc0, 0xfe, 0xc0, 0xc0, 0xc0, 0xfe, 0x00,
0x00, 0x00, 0xfe, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0xc6, 0x00,
0x00, 0x00, 0xc6, 0xee, 0xfe, 0xd6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00,
0x00, 0x00, 0x7C, 0x00, 0xFE, 0x10, 0x10, 0x10, 0x20, 0x40, 0x00};
for(int t=0;t<55;t++){
LED_payload[t] = deb[t];
}
B1122_CharLength = 5;
/**/
//Debug
//
int display_mode = save_speed_mode & 0x0F;
if(display_mode == MODE_RIGHT){
for(int blankcount = 0; blankcount < blanklength; blankcount++){
MAX7219__payload[ MAX7219dotcout] = 0x00;
MAX7219dotcout++;
}
}
for(charcount = 0; charcount < B1122_CharLength; charcount++){
//11byte分ループ
for(B1144dotcout = 0; B1144dotcout < ONE_CHAR_SIZE; B1144dotcout++){
//上2列、下1列は格納しない。
if((B1144dotcout != 0) && (B1144dotcout != 1) && (B1144dotcout != 10) ){
MAX7219__payload[MAX7219dotcout] = LED_payload[(charcount*11) + B1144dotcout];
MAX7219dotcout++;
}
}
}
if(display_mode != MODE_RIGHT){
for(int blankcount = 0; blankcount < blanklength; blankcount++){
MAX7219__payload[ MAX7219dotcout ] = 0x00;
MAX7219dotcout++;
}
}
//文字列長差し替え
MAX7219__CharLength = B1122_CharLength + 4;
display_Flash = save_Flash;
display_Marqee = save_Marqee;
display_speed_mode = save_speed_mode;
//書き込み中フラグをオフ
isWritting = false;
}
}
ConcateCount++;
//delay(50);
}
void MAX7219Display(uint8_t *dotArray,int charlen, uint8_t Flash, uint8_t Marquee, uint8_t SpMode){
uint8_t font[8];
uint8_t* ptr = MAX7219_getBuffer();
uint8_t Mode = 0;
uint8_t Speed = 0;
uint8_t* WkdotArray;
uint8_t wkbuff[8*4];
memset(wkbuff, '¥0', sizeof(wkbuff));
// モードの取得
Mode = SpMode & 0x0F;
// スピードの取得
Speed = SpMode >> 4;
Speed = Speed & 0x0F;
/* LEDの表示を初期化する */
// MAX7219_clear();
// 4文字以下なら4文字にする
if(charlen < 4){
memcpy(wkbuff, dotArray, charlen*8);
charlen = 4;
WkdotArray = wkbuff;
}
else{
WkdotArray = dotArray;
}
/* スクロール表示 */
// 右スクロール
if(Mode == MODE_RIGHT){
for (int8_t x=-8*charlen-16; x<=17; x++) {
uint16_t n = 0;
matrix.clear();
matrix1.clear();
matrix.setCursor(x,0);
matrix1.setCursor(x+16,0);
for(int Mcount = 0; Mcount < charlen; Mcount++){
memcpy(font, WkdotArray+(Mcount*8), 8);
matrix.drawBitmap(x+n,0,font,8,8,1);
matrix1.drawBitmap(x+16+n,0,font,8,8,1);
n+=8;
}
matrix.writeDisplay();
matrix1.writeDisplay();
delay(100+((3-Speed)*20));
}
}
// 左スクロール
else{
for (int8_t x=17; x>=-8*charlen-16; x--) {
uint16_t n = 0;
matrix.clear();
matrix1.clear();
matrix.setCursor(x,0);
matrix1.setCursor(x+16,0);
for(int Mcount = 0; Mcount < charlen; Mcount++){
memcpy(font, WkdotArray+(Mcount*8), 8);
matrix.drawBitmap(x+n,0,font,8,8,1);
matrix1.drawBitmap(x+16+n,0,font,8,8,1);
n+=8;
}
matrix.writeDisplay();
matrix1.writeDisplay();
delay(100+((3-Speed)*20));
}
}
return;
}
void DebugLEDOnOff(){
digitalWrite(LED_PIN, LOW); // LED ON
delay(100);
digitalWrite(LED_PIN, HIGH); // LED OFF
delay(100);
}
// for example
void TestLEDMatrix(){
uint8_t buf[8];
char *str="日本語のテストです";
for (int8_t x=17; x>=-88; x--) {
char *ptr = str;
uint16_t n = 0;
matrix.clear();
matrix1.clear();
matrix.setCursor(x,0);
matrix1.setCursor(x+16,0);
while(*ptr){
ptr = getFontData(buf,ptr,true);
if(!ptr)
break;
matrix.drawBitmap(x+n,0,buf,8,8,1);
matrix1.drawBitmap(x+16+n,0,buf,8,8,1);
n+=8;
}
matrix.writeDisplay();
matrix1.writeDisplay();
delay(20);
}
}