Newer
Older
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
/*
Boitarire software
Plays a sound through an external MP3 module
when triggered by external sensor :
- inclination switch
- accelerometer module
- touch sensors
Configuration is selected through compilation switches.
Uses an editable playlist allowing any song order,
up to 255 different songs in the playlist, individual
repetition number for eah song.
created 25 nov 2021
by Florian Savard
This example code is in the public domain.
https://code.electrolab.fr/Flax/boitarire
*/
#include<Wire.h>
// Defines - Macros - Constants
//#define SENSOR_INCLINATION
#define SENSOR_ACCELEROMETER
//#define SENSOR_TOUCH_SENSOR1
//#define SENSOR_TOUCH_SENSOR2
// Other constants
//Hardwaere constatnts : do not modify >_< !
#define PIN_SENSOR_POSITION 9 // Position sensor input - Input low = upside down
#define PIN_TOUCH_SENSOR1 8 // Touch sensor 1 input - Input active high
#define PIN_TOUCH_SENSOR2 5 // Touch sensor 2 input - Input active high
#define PIN_GROUND_SWITCH 7 // MP3 module ground switch command output - Output active high
#define PIN_BUSY 2 // MP3 module busy signal - Input active low
//Calibration constants : do adjust :-)
//#define MP3_RESET
//
//#define DEBUG_UART_ACCELEROMETER // Warning : conflict with MP3 player
#define ACCEL_ROTATION_DETECT // When set with SENSOR_ACCELEROMETER, trigger is activated on rotation detection, instead of raw movement threshold
#define DELAY_MP3_RESET 100 // Time of ground cut of MP3 module for re-init (ms) : Adjust only if hardware proiblems with new revisions of the mp3 module
#define DEBOUNCE_DELAY 200 // Position sensor input debounce (ms)
#define ACCEL_THRES_GYX 9999 // Detection threshold for X axis acceleration
#define ACCEL_THRES_GYY 9999 // Detection threshold for Y axis acceleration
#define ACCEL_THRES_GYZ 25000 // Detection threshold for Z axis acceleration
#define ACCEL_THRES_ROT 200 // Rotation threshold detection, for rotational speed integration pre-filtering
#define ACCEL_TRIG_ROT 2000 // Rotational distance to be traveled to trigger detection
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
#define PLAYLIST_LENGTH 4 // Number of songs in the playlist
// Typedefs
// Main state machine : start actions
typedef enum
{
//Initialisation
STM_INIT,
//Wainting for input (capteur)
STM_IDLE,
//When input, play sample
STM_PLAY,
//Waiting after playing sample
STM_BLANK,
//Optional hardware reset of the MP3 module (See calibration constants for activation)
STM_MP3_RESET,
} tStateMachine;
typedef enum
{
STM_DEBOUNCE_IDLE_0,
STM_DEBOUNCE_IDLE_1,
STM_DEBOUNCE_DETECT_0,
STM_DEBOUNCE_DETECT_1,
STM_DEBOUNCE_DEBOUNCE_0,
STM_DEBOUNCE_DEBOUNCE_1,
} tStateMachineDebounce;
typedef struct
{
uint8_t song_index_u8; // Index of the song in the device /!\ counts from 0
uint8_t cycles_number_u8; // Number of times the song must be played before switching to the next
uint16_t blank_delay_u16; // Blanking time for the song (ms)
} tSongItem;
/* Accelerometer sample structure
* Stores two samples, filtered value, integrated value
*/
typedef struct
{
int16_t sample1_s16;
int16_t sample2_s16;
int16_t sample_filt_s16;
int16_t sample_int_s16;
} tAccelSample;
/* Accelerometer samples
* Stores all samples for signal processing of accelerometer data
*/
typedef struct
{
tAccelSample AcX; // X linear axis
tAccelSample AcY; // Y linear axis
tAccelSample AcZ; // Z linear axis
tAccelSample GyX; // X rotational axis
tAccelSample GyY; // Y rotational axis
tAccelSample GyZ; // Z rotational axis
} tAccelData;
// Constant variables
/* Indifference table for songs cycles
* Contains the indexes of the songs to be played and the number of times each one must be played.
* Contains couples {index of the song in the MP3 device memory, number of times the song must be played, blanking time},
* Playlist cycles from first element to last and loops.
* Don't forget to update PLAYLIST_LENGTH define.
*/
const tSongItem cPlaylistIndif_TA[PLAYLIST_LENGTH] =
{
{0, 2, 1000},
{1, 2, 1000},
};
// Local variables
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
unsigned long debounce_delay;
int input_position_sensor, input_touch_sensor1, input_touch_sensor2;
unsigned long play_blank_delay, play_blank_delay_threshold, mp3_reset_delay;
unsigned long millis_temp;
const int MPU=0x68;
int16_t Tmp;
tAccelData AccelData_T;
bool accel_detect;
bool accel_detect_x, accel_detect_y, accel_detect_z;
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
uint8_t song_cycles_cnt_u8; // Counts the number of times the current song has been played
uint8_t song_playing_current_u8; // Holds the index of the playlist song actually being played
// State machines states
tStateMachine stmState;
tStateMachineDebounce stmDebounceState;
// Local function prototypes
void ReadPlayState (void);
void WritePlay (void);
void WritePlaySong (uint16_t index);
uint8_t CrcCalculate (uint8_t *buff, uint8_t size);
// Setup function
void setup() {
// Initialize serial:
Serial.begin(9600);
// Reserve 200 bytes for the inputString:
inputString.reserve(200);
// Position sensor input pin - See hardware constants for pin definition - Input pull-up
pinMode(PIN_SENSOR_POSITION, INPUT_PULLUP);
// Touch sensor 1 input pin - See hardware constants for pin definition - Input no pull
pinMode(PIN_TOUCH_SENSOR1, INPUT);
// Touch sensor 2 input pin - See hardware constants for pin definition - Input no pull
pinMode(PIN_TOUCH_SENSOR2, INPUT);
// LED output for visualisation
pinMode(LED_BUILTIN, OUTPUT);
// MP3 module ground switch command - See hardware constants for pin definition - Output push-pull
pinMode(PIN_GROUND_SWITCH, OUTPUT);
// MP3 module BUSY signal
pinMode(PIN_BUSY, INPUT);
//Initialise variables, do not define constants value here
play_blank_delay = 0;
mp3_reset_delay = 0;
stmState = STM_INIT;
stmDebounceState = STM_DEBOUNCE_IDLE_0;
debounce_delay = 0;
input_position_sensor = LOW;
input_touch_sensor1 = LOW;
input_touch_sensor2 = LOW;
song_cycles_cnt_u8 = 0;
song_playing_current_u8 = 0;
#ifdef SENSOR_ACCELEROMETER
// Accelerometer setup
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
// Set FIFO enable register
/* Wire.beginTransmission(MPU);
Wire.write(0x23);
Wire.write(0x78);
Wire.endTransmission(true);*/
// Set filters
Wire.beginTransmission(MPU);
Wire.write(0x1A);
Wire.write(0x06);
Wire.endTransmission(true);
#endif
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
accel_detect = false;
// Connect ground for MP3 module
digitalWrite(PIN_GROUND_SWITCH, HIGH);
}
// Main loop
void loop() {
// print the string when a newline arrives: for debug purposes only
if (stringComplete) {
Serial.println(inputString);
switch (inputString[0])
{
case 0x30:
// Play first song in the device
WritePlaySong(1);
break;
case 0x31:
// Play second song in the device
WritePlaySong(2);
break;
default:
break;
}
// clear the string:
inputString = "";
stringComplete = false;
}
#ifdef SENSOR_ACCELEROMETER
// Accelerometer communication
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true);
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
AccelData_T.AcX.sample1_s16=Wire.read()<<8|Wire.read();
AccelData_T.AcY.sample1_s16=Wire.read()<<8|Wire.read();
AccelData_T.AcZ.sample1_s16=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
AccelData_T.GyX.sample1_s16=Wire.read()<<8|Wire.read();
AccelData_T.GyY.sample1_s16=Wire.read()<<8|Wire.read();
AccelData_T.GyZ.sample1_s16=Wire.read()<<8|Wire.read();
#ifdef ACCEL_ROTATION_DETECT
/*
* Rotation detection
* Detection is done independantely on each axis.
* On each axis, Gy... input is filtered high-pass in order to filter the idle / background noise.
* High(pass filter is standard first order IIR with alpha = 0.5.
* If the high-pass filtered data reaches the ACCEL_THRES_ROT threshold, or if the unfiltered data reaches 2^4 times ACCEL_THRES_ROT,
* the data is then considered valid and integrated.
* Once valid, the input measure is added to the integrated data.
* Once the integrated data reaches ACCEL_TRIG_ROT, a trig is launched on this axis.
* A trigger occurs if any of the three axis is triggered.
*/
// X axis processing -----
// GyX high-pass filtering
AccelData_T.GyX.sample_filt_s16 = (AccelData_T.GyX.sample_filt_s16 + (AccelData_T.GyX.sample1_s16 - AccelData_T.GyX.sample2_s16)) >> 1;
if ((AccelData_T.GyX.sample1_s16 > (ACCEL_THRES_ROT << 4)) ||
(AccelData_T.GyX.sample1_s16 < -(ACCEL_THRES_ROT << 4)) ||
(AccelData_T.GyX.sample_filt_s16 > ACCEL_THRES_ROT) ||
(AccelData_T.GyX.sample_filt_s16 < -(ACCEL_THRES_ROT))) // Threshold for movement detection
{
AccelData_T.GyX.sample_int_s16 += (AccelData_T.GyX.sample1_s16 >> 9); // Integration
}
if ((AccelData_T.GyX.sample_int_s16 > ACCEL_TRIG_ROT) ||
(AccelData_T.GyX.sample_int_s16 < -(ACCEL_TRIG_ROT)))
{
AccelData_T.GyX.sample_int_s16 = 0; // Reset integrator
accel_detect_x = true;
}
else
{
accel_detect_x = false;
}
AccelData_T.GyX.sample2_s16=AccelData_T.GyX.sample1_s16; // N-1 sample memorisation
// Y Axis processing -----
// GyY high-pass filtering
AccelData_T.GyY.sample_filt_s16 = (AccelData_T.GyY.sample_filt_s16 + (AccelData_T.GyY.sample1_s16 - AccelData_T.GyY.sample2_s16)) >> 1;
if ((AccelData_T.GyY.sample1_s16 > (ACCEL_THRES_ROT << 4)) ||
(AccelData_T.GyY.sample1_s16 < -(ACCEL_THRES_ROT << 4)) ||
(AccelData_T.GyY.sample_filt_s16 > ACCEL_THRES_ROT) ||
(AccelData_T.GyY.sample_filt_s16 < -(ACCEL_THRES_ROT))) // Threshold for movement detection
{
AccelData_T.GyY.sample_int_s16 += (AccelData_T.GyY.sample1_s16 >> 9); // Integration
}
if ((AccelData_T.GyY.sample_int_s16 > ACCEL_TRIG_ROT) ||
(AccelData_T.GyY.sample_int_s16 < -(ACCEL_TRIG_ROT)))
{
AccelData_T.GyY.sample_int_s16 = 0; // Reset integrator
accel_detect_y = true;
}
else
{
accel_detect_y = false;
}
AccelData_T.GyY.sample2_s16=AccelData_T.GyY.sample1_s16; // N-1 sample memorisation
// Z Axis processing -----
// GyZ high-pass filtering
AccelData_T.GyZ.sample_filt_s16 = (AccelData_T.GyZ.sample_filt_s16 + (AccelData_T.GyZ.sample1_s16 - AccelData_T.GyZ.sample2_s16)) >> 1;
if ((AccelData_T.GyZ.sample1_s16 > (ACCEL_THRES_ROT << 4)) ||
(AccelData_T.GyZ.sample1_s16 < -(ACCEL_THRES_ROT << 4)) ||
(AccelData_T.GyZ.sample_filt_s16 > ACCEL_THRES_ROT) ||
(AccelData_T.GyZ.sample_filt_s16 < -(ACCEL_THRES_ROT))) // Threshold for movement detection
{
AccelData_T.GyZ.sample_int_s16 += (AccelData_T.GyZ.sample1_s16 >> 9); // Integration
}
if ((AccelData_T.GyZ.sample_int_s16 > ACCEL_TRIG_ROT) ||
(AccelData_T.GyZ.sample_int_s16 < -(ACCEL_TRIG_ROT)))
{
AccelData_T.GyZ.sample_int_s16 = 0; // Reset integrator
accel_detect_z = true;
}
else
{
accel_detect_z = false;
}
AccelData_T.GyZ.sample2_s16=AccelData_T.GyZ.sample1_s16; // N-1 sample memorisation
// Concatenation of the three axis
accel_detect = accel_detect_x || accel_detect_y ||accel_detect_z;
#else //ACCEL_ROTATION_DETECT
if ((AccelData_T.GyX.sample1_s16 > ACCEL_THRES_GYX) ||
(AccelData_T.GyY.sample1_s16 > ACCEL_THRES_GYY) ||
(AccelData_T.GyZ.sample1_s16 > ACCEL_THRES_GYZ) ||
(AccelData_T.GyX.sample1_s16 < ((int16_t)(-1) * ACCEL_THRES_GYX)) ||
(AccelData_T.GyY.sample1_s16 < ((int16_t)(-1) * ACCEL_THRES_GYY)) ||
(AccelData_T.GyZ.sample1_s16 < ((int16_t)(-1) * ACCEL_THRES_GYZ)))
{
accel_detect = true;
}
else
{
accel_detect = false;
}
#endif //ACCEL_ROTATION_DETECT
#else //SENSOR_ACCELEROMETER
accel_detect = false;
#endif //SENSOR_ACCELEROMETER
#ifdef SENSOR_INCLINATION
// Read instantaneous value of position sensor
input_position_sensor = digitalRead(PIN_SENSOR_POSITION);
#else //SENSOR_INCLINATION
input_position_sensor = LOW;
#endif //SENSOR_INCLINATION
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Inclination debounce state machine
//-----------------------------------
switch (stmDebounceState)
{
case STM_DEBOUNCE_IDLE_0:
if (input_position_sensor == HIGH)
{
stmDebounceState = STM_DEBOUNCE_DETECT_1;
debounce_delay = millis();
}
break;
case STM_DEBOUNCE_IDLE_1:
if (input_position_sensor == LOW)
{
stmDebounceState = STM_DEBOUNCE_DETECT_0;
debounce_delay = millis();
}
break;
case STM_DEBOUNCE_DETECT_0:
millis_temp = millis();
if (input_position_sensor == HIGH)
{
stmDebounceState = STM_DEBOUNCE_IDLE_1;
}
else if ((millis_temp >= debounce_delay) && ((millis_temp - debounce_delay) > DEBOUNCE_DELAY))
{
stmDebounceState = STM_DEBOUNCE_DEBOUNCE_0;
}
else if (millis_temp < debounce_delay)
{ // Overflow protection for uptime of several weeks or more.
debounce_delay = 0;
}
break;
case STM_DEBOUNCE_DETECT_1:
millis_temp = millis();
if (input_position_sensor == LOW)
{
stmDebounceState = STM_DEBOUNCE_IDLE_0;
}
else if ((millis_temp >= debounce_delay) && ((millis_temp - debounce_delay) > DEBOUNCE_DELAY))
{
stmDebounceState = STM_DEBOUNCE_DEBOUNCE_1;
}
else if (millis_temp < debounce_delay)
{ // Overflow protection for uptime of several weeks or more.
debounce_delay = 0;
}
break;
case STM_DEBOUNCE_DEBOUNCE_0:
stmDebounceState = STM_DEBOUNCE_IDLE_0;
//digitalWrite(LED_BUILTIN, LOW); // DEBUG
break;
case STM_DEBOUNCE_DEBOUNCE_1:
stmDebounceState = STM_DEBOUNCE_IDLE_1;
//digitalWrite(LED_BUILTIN, HIGH); // DEBUG
break;
default:
break;
}
#ifdef SENSOR_TOUCH_SENSOR1
input_touch_sensor1 = digitalRead(PIN_TOUCH_SENSOR1);
#else
input_touch_sensor1 = LOW;
#endif
#ifdef SENSOR_TOUCH_SENSOR2
input_touch_sensor2 = digitalRead(PIN_TOUCH_SENSOR2);
#else
input_touch_sensor2 = LOW;
#endif
// Main state machine
//-------------------
switch (stmState)
{
//Init is not used, here for future use and coding best practice
case STM_INIT:
stmState = STM_IDLE;
break;
case STM_IDLE:
//digitalWrite(LED_BUILTIN, LOW); // DEBUG
if ((stmDebounceState == STM_DEBOUNCE_DEBOUNCE_1) ||
(input_touch_sensor1 == HIGH) ||
(input_touch_sensor2 == HIGH) ||
(accel_detect == true))
{
stmState = STM_PLAY;
}
break;
case STM_PLAY:
WritePlaySong(cPlaylistIndif_TA[song_playing_current_u8].song_index_u8 + 1U); // Play the current song
//digitalWrite(LED_BUILTIN, HIGH); // DEBUG
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
play_blank_delay_threshold = cPlaylistIndif_TA[song_playing_current_u8].blank_delay_u16; // Load blankig time for the current song
song_cycles_cnt_u8++; // Increment cycles counter
if (song_cycles_cnt_u8 >= cPlaylistIndif_TA[song_playing_current_u8].cycles_number_u8)
{ // Check cycles counter overflow
song_cycles_cnt_u8 = 0; // Reset the cycles counter
song_playing_current_u8++; // Increment the playlist counter
}
if (song_playing_current_u8 >= PLAYLIST_LENGTH)
{ // Check playlist song counter overflow
song_playing_current_u8 = 0;
}
play_blank_delay = millis(); // Record current time for blanking delay measurement
stmState = STM_BLANK;
break;
case STM_BLANK:
millis_temp = millis(); // Get current time
if ((millis_temp >= play_blank_delay) && ((millis_temp - play_blank_delay) > play_blank_delay_threshold))
{ // Compare current time with the memorised time at song start
#ifdef MP3_RESET
stmState = STM_MP3_RESET;
mp3_reset_delay = millis(); // Record current time for reset delay measurement
digitalWrite(PIN_GROUND_SWITCH, LOW);
#else // MP3_RESET
stmState = STM_IDLE;
#endif // MP3_RESET
}
else if (millis_temp < play_blank_delay)
{ // Overflow protection for uptime of several weeks or more.
play_blank_delay = 0;
}
break;
case STM_MP3_RESET:
millis_temp = millis();
//digitalWrite(LED_BUILTIN, LOW); // DEBUG
if ((millis_temp >= mp3_reset_delay) && ((millis_temp - mp3_reset_delay) > DELAY_MP3_RESET))
{
stmState = STM_IDLE;
digitalWrite(PIN_GROUND_SWITCH, HIGH);
}
else if (millis_temp < mp3_reset_delay)
{ // Overflow protection for uptime of several weeks or more.
mp3_reset_delay = 0;
}
break;
default:
stmState = STM_INIT;
break;
}
#ifdef DEBUG_UART_ACCELEROMETER
// Format : https://diyrobocars.com/2020/05/04/arduino-serial-plotter-the-missing-manual/
// Adapted for serial plotter
// //Serial.print("Accelerometer: ");
//Serial.print("X:"); Serial.print(AccelData_T.AcX.sample1_s16);Serial.print(",");
//Serial.print("Y:"); Serial.print(AccelData_T.AcY.sample1_s16);Serial.print(",");
//Serial.print("Z:"); Serial.print(AccelData_T.AcZ.sample1_s16);Serial.print(",");
//Serial.print("Gyroscope: ");
//Serial.print("GX:"); Serial.print(AccelData_T.GyX.sample1_s16);Serial.print(",");
//Serial.print("GY:"); Serial.print(AccelData_T.GyY.sample1_s16);Serial.print(",");
//Serial.print("GZ:"); Serial.print(AccelData_T.GyZ.sample1_s16);Serial.print(",");
//Serial.print("GXfilt:"); Serial.print(AccelData_T.GyX.sample_filt_s16);Serial.print(",");
Serial.print("GXint:"); Serial.print(AccelData_T.GyX.sample_int_s16);Serial.print(",");
Serial.print("GYint:"); Serial.print(AccelData_T.GyY.sample_int_s16);Serial.print(",");
Serial.print("GZint:"); Serial.print(AccelData_T.GyZ.sample_int_s16);Serial.print(",");
Serial.print("Temp:"); Serial.println(Tmp);
//Serial.println(" ");
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#endif
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
void ReadPlayState (void)
{
// Send Play State read request
Serial.write(170); // 0xAA
Serial.write(1); // 0x01
Serial.write(0); // 0x00
Serial.write(171); // 0xAB
}
void WritePlay (void)
{
// Send Play request
Serial.write(170); // 0xAA
Serial.write(2); // 0x02
Serial.write(0); // 0x00
Serial.write(172); // 0xAC
}
void WritePlaySong (uint16_t index)
{
uint8_t buffer_u8A[5];
buffer_u8A[0] = 0xAA;
buffer_u8A[1] = 0x07;
buffer_u8A[2] = 0x02;
buffer_u8A[3] = (uint8_t)((index >> 8U) & (uint16_t)0x00FF);
buffer_u8A[4] = (uint8_t)(index & (uint16_t)0x00FF);
uint8_t crc_u8 = CrcCalculate(buffer_u8A, 5);
// Send Play request
Serial.write((int)buffer_u8A[0]); // 0xAA
Serial.write((int)buffer_u8A[1]); // 0x07
Serial.write((int)buffer_u8A[2]); // 0x02
Serial.write((int)buffer_u8A[3]); // Song number high byte
Serial.write((int)buffer_u8A[4]); // Song number low byte
Serial.write((int)crc_u8); // CRC
}
uint8_t CrcCalculate (uint8_t *buff, uint8_t size)
{
uint16_t ret_u16 = 0;
uint8_t cnt_u8 = 0;
for (cnt_u8 = 0; cnt_u8 < size; cnt_u8++)
{
ret_u16 += *(buff + cnt_u8);
}
return ((uint8_t)(ret_u16 & (uint16_t)0x00FF));