-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathThreadsafe_Serial_ProtocolWrapping.ino
110 lines (90 loc) · 3.21 KB
/
Threadsafe_Serial_ProtocolWrapping.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
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <Arduino_Threads.h>
/**************************************************************************************
* CONSTANTS
**************************************************************************************/
static size_t constexpr NUM_THREADS = 5;
/**************************************************************************************
* FUNCTION DECLARATION
**************************************************************************************/
void serial_thread_func();
/**************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************/
static char thread_name[NUM_THREADS][32];
#undef Serial
#ifdef ARDUINO_PORTENTA_H7_M4
SerialDispatcher Serial(Serial1); /* No SerialUSB for Portenta H7 / M4 Core */
#else
SerialDispatcher Serial(SerialUSB);
#endif
/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/
void setup()
{
/* Fire up some threads all accessing the LSM6DSOX */
for(size_t i = 0; i < NUM_THREADS; i++)
{
snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i);
rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]);
t->start(serial_thread_func);
}
}
void loop()
{
}
/**************************************************************************************
* FUNCTION DEFINITION
**************************************************************************************/
String nmea_message_prefix(String const & /* msg */)
{
return String("$");
}
String nmea_message_suffix(String const & prefix, String const & msg)
{
/* NMEA checksum is calculated over the complete message
* starting with '$' and ending with the end of the message.
*/
byte checksum = 0;
std::for_each(msg.c_str(),
msg.c_str() + msg.length(),
[&checksum](char const c)
{
checksum ^= static_cast<uint8_t>(c);
});
/* Assemble the footer of the NMEA message. */
char footer[16] = {0};
snprintf(footer, sizeof(footer), "*%02X\r\n", checksum);
return String(footer);
}
void serial_thread_func()
{
Serial.begin(9600);
Serial.prefix(nmea_message_prefix);
Serial.suffix(nmea_message_suffix);
for(;;)
{
/* Sleep between 5 and 500 ms */
rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500)));
/* Print a fake NMEA GPRMC message:
* $GPRMC,062101.714,A,5001.869,N,01912.114,E,955535.7,116.2,290520,000.0,W*45\r\n
*/
Serial.block();
Serial.print("GPRMC,");
Serial.print(millis());
Serial.print(",A,");
Serial.print("5001.869,");
Serial.print("N,");
Serial.print("01912.114,");
Serial.print("E,");
Serial.print("955535.7,");
Serial.print("116.2,");
Serial.print("290520,");
Serial.print("000.0,");
Serial.print("W");
Serial.unblock();
}
}