|
4 | 4 |
|
5 | 5 | #include <Arduino_Threads.h>
|
6 | 6 |
|
7 |
| -/************************************************************************************** |
8 |
| - * CONSTANTS |
9 |
| - **************************************************************************************/ |
10 |
| - |
11 |
| -static size_t constexpr NUM_THREADS = 5; |
12 |
| - |
13 |
| -/************************************************************************************** |
14 |
| - * FUNCTION DECLARATION |
15 |
| - **************************************************************************************/ |
16 |
| - |
17 |
| -void serial_thread_func(); |
18 |
| - |
19 |
| -/************************************************************************************** |
20 |
| - * GLOBAL VARIABLES |
21 |
| - **************************************************************************************/ |
22 |
| - |
23 |
| -static char thread_name[NUM_THREADS][32]; |
24 |
| -#undef Serial |
25 |
| -#ifdef ARDUINO_PORTENTA_H7_M4 |
26 |
| - SerialDispatcher Serial(Serial1); /* No SerialUSB for Portenta H7 / M4 Core */ |
27 |
| -#else |
28 |
| - SerialDispatcher Serial(SerialUSB); |
29 |
| -#endif |
30 |
| - |
31 | 7 | /**************************************************************************************
|
32 | 8 | * SETUP/LOOP
|
33 | 9 | **************************************************************************************/
|
34 | 10 |
|
35 | 11 | void setup()
|
36 | 12 | {
|
37 |
| - /* Fire up some threads all accessing the LSM6DSOX */ |
38 |
| - for(size_t i = 0; i < NUM_THREADS; i++) |
39 |
| - { |
40 |
| - snprintf(thread_name[i], sizeof(thread_name[i]), "Thread #%02d", i); |
41 |
| - rtos::Thread * t = new rtos::Thread(osPriorityNormal, OS_STACK_SIZE, nullptr, thread_name[i]); |
42 |
| - t->start(serial_thread_func); |
43 |
| - } |
44 |
| -} |
45 |
| - |
46 |
| -void loop() |
47 |
| -{ |
48 |
| - |
49 |
| -} |
50 |
| - |
51 |
| -/************************************************************************************** |
52 |
| - * FUNCTION DEFINITION |
53 |
| - **************************************************************************************/ |
| 13 | + Serial.begin(9600); |
| 14 | + while (!Serial) { } |
54 | 15 |
|
55 |
| -String nmea_message_prefix(String const & /* msg */) |
56 |
| -{ |
57 |
| - return String("$"); |
| 16 | + GPS_Thread.start(); |
58 | 17 | }
|
59 | 18 |
|
60 |
| -String nmea_message_suffix(String const & prefix, String const & msg) |
| 19 | +void loop() |
61 | 20 | {
|
62 |
| - /* NMEA checksum is calculated over the complete message |
63 |
| - * starting with '$' and ending with the end of the message. |
| 21 | + /* If we don't hand back control then the main thread |
| 22 | + * will hog the CPU and all other thread's won't get |
| 23 | + * time to be executed. |
64 | 24 | */
|
65 |
| - byte checksum = 0; |
66 |
| - std::for_each(msg.c_str(), |
67 |
| - msg.c_str() + msg.length(), |
68 |
| - [&checksum](char const c) |
69 |
| - { |
70 |
| - checksum ^= static_cast<uint8_t>(c); |
71 |
| - }); |
72 |
| - /* Assemble the footer of the NMEA message. */ |
73 |
| - char footer[16] = {0}; |
74 |
| - snprintf(footer, sizeof(footer), "*%02X\r\n", checksum); |
75 |
| - return String(footer); |
76 |
| -} |
77 |
| - |
78 |
| -void serial_thread_func() |
79 |
| -{ |
80 |
| - Serial.begin(9600); |
81 |
| - |
82 |
| - Serial.prefix(nmea_message_prefix); |
83 |
| - Serial.suffix(nmea_message_suffix); |
84 |
| - |
85 |
| - for(;;) |
86 |
| - { |
87 |
| - /* Sleep between 5 and 500 ms */ |
88 |
| - rtos::ThisThread::sleep_for(rtos::Kernel::Clock::duration_u32(random(5,500))); |
89 |
| - |
90 |
| - /* Print a fake NMEA GPRMC message: |
91 |
| - * $GPRMC,062101.714,A,5001.869,N,01912.114,E,955535.7,116.2,290520,000.0,W*45\r\n |
92 |
| - */ |
93 |
| - Serial.block(); |
94 |
| - |
95 |
| - Serial.print("GPRMC,"); |
96 |
| - Serial.print(millis()); |
97 |
| - Serial.print(",A,"); |
98 |
| - Serial.print("5001.869,"); |
99 |
| - Serial.print("N,"); |
100 |
| - Serial.print("01912.114,"); |
101 |
| - Serial.print("E,"); |
102 |
| - Serial.print("955535.7,"); |
103 |
| - Serial.print("116.2,"); |
104 |
| - Serial.print("290520,"); |
105 |
| - Serial.print("000.0,"); |
106 |
| - Serial.print("W"); |
107 |
| - |
108 |
| - Serial.unblock(); |
109 |
| - } |
| 25 | + rtos::ThisThread::yield(); |
110 | 26 | }
|
0 commit comments