-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
456 lines (395 loc) · 12.3 KB
/
main.cpp
File metadata and controls
456 lines (395 loc) · 12.3 KB
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
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
/**
* \file
* \brief Main file for the WEICLEDFeatherWing.
*
* \copyright (c) 2024 Würth Elektronik eiSos GmbH & Co. KG
*
* \page License
*
* THE SOFTWARE INCLUDING THE SOURCE CODE IS PROVIDED “AS IS”. YOU ACKNOWLEDGE
* THAT WÜRTH ELEKTRONIK EISOS MAKES NO REPRESENTATIONS AND WARRANTIES OF ANY
* KIND RELATED TO, BUT NOT LIMITED TO THE NON-INFRINGEMENT OF THIRD PARTIES’
* INTELLECTUAL PROPERTY RIGHTS OR THE MERCHANTABILITY OR FITNESS FOR YOUR
* INTENDED PURPOSE OR USAGE. WÜRTH ELEKTRONIK EISOS DOES NOT WARRANT OR
* REPRESENT THAT ANY LICENSE, EITHER EXPRESS OR IMPLIED, IS GRANTED UNDER ANY
* PATENT RIGHT, COPYRIGHT, MASK WORK RIGHT, OR OTHER INTELLECTUAL PROPERTY
* RIGHT RELATING TO ANY COMBINATION, MACHINE, OR PROCESS IN WHICH THE PRODUCT
* IS USED. INFORMATION PUBLISHED BY WÜRTH ELEKTRONIK EISOS REGARDING
* THIRD-PARTY PRODUCTS OR SERVICES DOES NOT CONSTITUTE A LICENSE FROM WÜRTH
* ELEKTRONIK EISOS TO USE SUCH PRODUCTS OR SERVICES OR A WARRANTY OR
* ENDORSEMENT THEREOF
*
* THIS SOURCE CODE IS PROTECTED BY A LICENSE.
* FOR MORE INFORMATION PLEASE CAREFULLY READ THE LICENSE AGREEMENT FILE LOCATED
* IN THE ROOT DIRECTORY OF THIS PACKAGE
*/
#define PROTEUSIIIFEATHERWING false
#define SENSORFEATHERWING false
/** Includes */
#include <ConfigPlatform.h>
#include <we_print.h>
#include "ICLED.h"
#include "ICLED_demos.h"
#if SENSORFEATHERWING == true
#include <sensorBoard.h>
static float ITDS_acceleration_y;
static volatile bool check_orientation = false;
#define HIDS_PART_NUMBER 2525020210002
static float HIDS_humidity;
static float TIDS_temp;
void startTimer(int frequencyHz);
void TC3_Handler();
#endif
#if PROTEUSIIIFEATHERWING == true
#include <global/global.h>
#include <global_platform_types.h>
#include <ProteusIII/ProteusIII.h>
#define PAYLOAD_BUFFER_SIZE 64
static uint8_t payload_buffer[PAYLOAD_BUFFER_SIZE];
/*Connect the pins 1 and 2 on JP1 to use the Wake-up feature on Pin GPIO9*/
static const WE_Arduino_Pin_t pin_SleepWakeUp = WE_ARDUINO_PIN(9);
/*Connect the pins 3 and 4 on JP1 to use the mode change feature on Pin
* GPIOA3/Pin17*/
static const WE_Arduino_Pin_t pin_Mode = WE_ARDUINO_PIN(17);
static ProteusIII_Pins_t ProteusIII_pins = {
.ProteusIII_Pin_Reset = WE_PIN_UNDEFINED(),
.ProteusIII_Pin_SleepWakeUp = WE_PIN((void *)&pin_SleepWakeUp),
.ProteusIII_Pin_Boot = WE_PIN_UNDEFINED(),
.ProteusIII_Pin_Mode = WE_PIN((void *)&pin_Mode),
.ProteusIII_Pin_Busy = WE_PIN_UNDEFINED(),
.ProteusIII_Pin_StatusLed2 = WE_PIN_UNDEFINED()};
static WE_UART_t ProteusIII_uart;
/* Callback functions for various indications sent by the Proteus-III. */
static void RxCallback(uint8_t *payload, uint16_t payloadLength, uint8_t *btMac,
int8_t rssi);
#endif
/* Test Modes */
typedef enum
{
TEST1,
TEST2,
TEST3,
TEST4,
TEST5,
TEST6,
TEST7,
TEST8,
#if PROTEUSIIIFEATHERWING == true
TEST9,
#endif
#if SENSORFEATHERWING == true
TEST10,
TEST11,
#endif
TEST_Total_Count
} TestMode;
static volatile TestMode current_mode = TEST1;
static void PROG_ISR_handler();
static unsigned long prog_debounce_time_elapsed = 0;
static bool running_loop = true;
static volatile bool initial_test_run = true;
static bool orientation_changed = false;
void setup()
{
// Using the USB serial port for debug messages
#if ((WE_DEBUG_PRINT_LEVEL > WE_Debug_Print_Level_Off) || \
defined(WE_APP_PRINT_ENABLED))
WE_Print_Init();
#endif
#if PROTEUSIIIFEATHERWING == true
ProteusIII_CallbackConfig_t callbackConfig = {0};
callbackConfig.rxCb = RxCallback;
ProteusIII_uart.baudrate = PROTEUSIII_DEFAULT_BAUDRATE;
ProteusIII_uart.flowControl = WE_FlowControl_NoFlowControl;
ProteusIII_uart.parity = WE_Parity_None;
ProteusIII_uart.uartInit = WE_UART_RXPin11_TXPin10_Init;
ProteusIII_uart.uartDeinit = WE_UART_RXPin11_TXPin10_DeInit;
ProteusIII_uart.uartTransmit = WE_UART_RXPin11_TXPin10_Transmit;
if (!ProteusIII_Init(&ProteusIII_uart, &ProteusIII_pins,
ProteusIII_OperationMode_CommandMode,
callbackConfig))
{
WE_APP_PRINT("Proteus init failed \r\n");
}
uint8_t oldName[32];
uint16_t oldnameLength;
if (!ProteusIII_GetDeviceName(oldName, &oldnameLength))
{
WE_APP_PRINT("Proteus failed to get name \r\n");
exit(0);
}
char newName[32] = "ICLED";
if (memcmp(newName, oldName, strlen(newName)) != 0)
{
// Set name to ICLED
if (!ProteusIII_SetDeviceName((uint8_t *)newName, strlen(newName)))
{
WE_APP_PRINT("Proteus failed to set name \r\n");
}
}
#endif
#if SENSORFEATHERWING == true
if (!sensorBoard_Init())
{
WE_APP_PRINT("I2C init failed \r\n");
}
if (!ITDS_2533020201601_simpleInit())
{
WE_APP_PRINT("ITDS init failed \r\n");
}
if (!TIDS_2521020222501_simpleInit())
{
WE_APP_PRINT("TIDS init failed \r\n");
}
#if HIDS_PART_NUMBER == 2525020210001
if (!HIDS_2525020210001_simpleInit())
{
WE_APP_PRINT("HIDS init failed \r\n");
}
#elif HIDS_PART_NUMBER == 2525020210002
if (!HIDS_2525020210002_simpleInit())
{
WE_APP_PRINT("HIDS init failed \r\n");
}
#endif
startTimer(5);
#endif
if (!ICLED_Init(RGB, Landscape))
{
WE_APP_PRINT("ICLED init failed \r\n");
}
pinMode(ICLED_PROG_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ICLED_PROG_PIN), PROG_ISR_handler, FALLING);
}
void loop()
{
#if SENSORFEATHERWING == true
ICLED_Orientation current_orientation = ICLED_get_orientation();
if (check_orientation)
{
if (!TIDS_2521020222501_readSensorData(&TIDS_temp))
{
WE_APP_PRINT("error reading TIDS data \r\n");
}
#if HIDS_PART_NUMBER == 2525020210001
if (!HIDS_2525020210001_readSensorData(&HIDS_humidity, NULL))
{
WE_APP_PRINT("error reading TIDS data \r\n");
}
#elif HIDS_PART_NUMBER == 2525020210002
if (!HIDS_2525020210002_readSensorData(&HIDS_humidity, NULL))
{
WE_APP_PRINT("error reading TIDS data \r\n");
}
#endif
if (ITDS_2533020201601_readSensorData(NULL, &ITDS_acceleration_y, NULL, NULL))
{
ICLED_set_orientation(ITDS_acceleration_y > 0.05 ? Landscape_UpsideDown : Landscape);
}
check_orientation = false;
}
orientation_changed = (current_orientation != ICLED_get_orientation());
#endif
ICLED_set_color_system(RGB);
switch (current_mode)
{
case TEST1:
{
running_loop = true;
ICLED_demo_start_show((bool *)&initial_test_run, &running_loop, (uint8_t *)¤t_mode);
break;
}
case TEST2:
{
if (!initial_test_run && !orientation_changed)
{
break;
}
ICLED_clear(false);
initial_test_run = false;
ICLED_set_pixel(0, 0, 0, 50, 128);
ICLED_set_screen_pixel(3, 6, 0, 50, 0, 128);
ICLED_set_pixel(104, 50, 0, 0, 255);
break;
}
case TEST3:
{
ICLED_clear(false);
running_loop = true;
ICLED_demo_send_alphabet(128, 128, 128, 10, 80, &running_loop);
break;
}
case TEST4:
{
ICLED_clear(false);
running_loop = true;
uint16_t place = 12;
char string[] = "Hello World!";
ICLED_set_string(string, &place, 128, 128, 128, 10, false);
static uint16_t hello_world_current_column = place;
ICLED_start_conditional_loop(0, place, 100, &hello_world_current_column, &running_loop);
break;
}
case TEST5:
{
ICLED_clear(false);
running_loop = true;
ICLED_demo_show_rainbow(8, 35, &running_loop);
break;
}
case TEST6:
{
char price[] = "42,69";
ICLED_clear(false);
running_loop = true;
ICLED_demo_show_price(price, 0, 128, 0, 10, 80, &running_loop);
break;
}
case TEST7:
{
uint16_t place = 0;
ICLED_clear(false);
running_loop = true;
ICLED_set_emoji(Emoji_Smile_Face, &place, 128, 128, 128, 10, false);
ICLED_set_emoji(Emoji_Neutral_Face, &place, 128, 128, 128, 10, false);
ICLED_set_emoji(Emoji_Frown_Face, &place, 128, 128, 128, 10, false);
ICLED_set_emoji(Emoji_Heart, &place, 128, 0, 0, 15, false);
static uint16_t emojis_current_column = 0;
ICLED_start_conditional_loop(0, place, 100, &emojis_current_column, &running_loop);
break;
}
case TEST8:
{
if (!initial_test_run && !orientation_changed)
{
break;
}
ICLED_clear(false);
initial_test_run = false;
ICLED_set_color_system(RGB);
for (uint8_t i = 0; i < ICLED_ROWS; i++)
{
ICLED_set_screen_pixel(i, 0, 227, 0, 11, 50);
}
ICLED_set_color_system(HSV);
for (uint8_t i = 0; i < ICLED_ROWS; i++)
{
ICLED_set_screen_pixel(i, ICLED_COLUMNS - 1, 357, 100, 50, 50);
}
break;
}
#if PROTEUSIIIFEATHERWING == true
case TEST9:
{
if (initial_test_run)
{
strcpy((char *)payload_buffer, "Send BLE Data");
}
initial_test_run = false;
ICLED_clear(false);
running_loop = true;
ICLED_demo_show_ble_data((char *)payload_buffer, 0, 0, 255, 25, 80, &running_loop);
break;
}
#endif
#if SENSORFEATHERWING == true
case TEST10:
{
ICLED_clear(false);
running_loop = true;
ICLED_demo_show_temp_sensor_data(TIDS_temp, 25, 80, &running_loop);
break;
}
case TEST11:
{
ICLED_clear(false);
running_loop = true;
ICLED_demo_show_hum_sensor_data(HIDS_humidity, 25, 80, &running_loop);
break;
}
#endif
case TEST_Total_Count:
{
WE_APP_PRINT("Invalid Test \r\n");
exit(0);
}
}
}
#if PROTEUSIIIFEATHERWING == true
static void RxCallback(uint8_t *payload, uint16_t payloadLength, uint8_t *btMac,
int8_t rssi)
{
if (current_mode != TEST9)
{
return;
}
if (payloadLength > (PAYLOAD_BUFFER_SIZE - 1))
{
return;
}
memcpy(payload_buffer, payload, payloadLength);
payload_buffer[payloadLength] = '\0';
running_loop = false;
}
#endif
static void PROG_ISR_handler()
{
if ((millis() - prog_debounce_time_elapsed) < 300)
{
return;
}
prog_debounce_time_elapsed = millis();
current_mode = (TestMode)((current_mode + 1) % TEST_Total_Count);
running_loop = false;
initial_test_run = true;
}
#if SENSORFEATHERWING == true
#define CPU_HZ 48000000
#define TIMER_PRESCALER_DIV 1024
void startTimer(int frequencyHz)
{
REG_GCLK_CLKCTRL = (uint16_t)(GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_TCC2_TC3);
while (GCLK->STATUS.bit.SYNCBUSY == 1)
; // wait for sync
TcCount16 *TC = (TcCount16 *)TC3;
TC->CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (TC->STATUS.bit.SYNCBUSY == 1)
; // wait for sync
// Use the 16-bit timer
TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
while (TC->STATUS.bit.SYNCBUSY == 1)
; // wait for sync
// Use match mode so that the timer counter resets when the count matches the compare register
TC->CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
while (TC->STATUS.bit.SYNCBUSY == 1)
; // wait for sync
// Set prescaler to 1024
TC->CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1024;
while (TC->STATUS.bit.SYNCBUSY == 1)
; // wait for sync
int compareValue = (CPU_HZ / (TIMER_PRESCALER_DIV * frequencyHz)) - 1;
// Make sure the count is in a proportional position to where it was
// to prevent any jitter or disconnect when changing the compare value.
TC->COUNT.reg = map(TC->COUNT.reg, 0, TC->CC[0].reg, 0, compareValue);
TC->CC[0].reg = compareValue;
while (TC->STATUS.bit.SYNCBUSY == 1)
;
// Enable the compare interrupt
TC->INTENSET.reg = 0;
TC->INTENSET.bit.MC0 = 1;
NVIC_EnableIRQ(TC3_IRQn);
TC->CTRLA.reg |= TC_CTRLA_ENABLE;
while (TC->STATUS.bit.SYNCBUSY == 1)
; // wait for sync
}
void TC3_Handler()
{
TcCount16 *TC = (TcCount16 *)TC3;
if (TC->INTFLAG.bit.MC0 == 1)
{
TC->INTFLAG.bit.MC0 = 1;
check_orientation = true;
running_loop = false;
}
}
#endif