12
12
#include < csignal>
13
13
#include < cstdint>
14
14
#include < bitset>
15
+ #include < vector>
16
+
17
+ #include < pthread.h>
18
+ #include < sched.h>
19
+
20
+ void set_realtime_priority () {
21
+ int ret;
22
+
23
+ // We'll operate on the currently running thread.
24
+ pthread_t this_thread = pthread_self ();
25
+ // struct sched_param is used to store the scheduling priority
26
+ struct sched_param params;
27
+ // We'll set the priority to the maximum.
28
+ params.sched_priority = sched_get_priority_max (SCHED_FIFO);
29
+ std::cout << " Trying to set thread realtime prio = " << params.sched_priority << std::endl;
30
+
31
+ // Attempt to set thread real-time priority to the SCHED_FIFO policy
32
+ ret = pthread_setschedparam (this_thread, SCHED_FIFO, ¶ms);
33
+ if (ret != 0 ) {
34
+ // Print the error
35
+ std::cout << " Unsuccessful in setting thread realtime prio (erno=" << ret << " )" << std::endl;
36
+ return ;
37
+ }
38
+
39
+ // Now verify the change in thread priority
40
+ int policy = 0 ;
41
+ ret = pthread_getschedparam (this_thread, &policy, ¶ms);
42
+ if (ret != 0 ) {
43
+ std::cout << " Couldn't retrieve real-time scheduling paramers" << std::endl;
44
+ return ;
45
+ }
46
+
47
+ // Check the correct policy was applied
48
+ if (policy != SCHED_FIFO) {
49
+ std::cout << " Scheduling is NOT SCHED_FIFO!" << std::endl;
50
+ } else {
51
+ std::cout << " SCHED_FIFO OK" << std::endl;
52
+ }
53
+
54
+ // Print thread scheduling priority
55
+ std::cout << " Thread priority is " << params.sched_priority << std::endl;
56
+ }
57
+
15
58
16
- #include < QElapsedTimer>
59
+ struct ColorSignal
60
+ {
61
+ uint8_t green_1;
62
+ uint8_t green_2;
63
+ uint8_t green_3;
64
+ uint8_t green_4;
65
+
66
+ uint8_t red_1;
67
+ uint8_t red_2;
68
+ uint8_t red_3;
69
+ uint8_t red_4;
70
+
71
+ uint8_t blue_1;
72
+ uint8_t blue_2;
73
+ uint8_t blue_3;
74
+ uint8_t blue_4;
75
+ };
76
+
77
+ static ColorSignal RED_Signal = { 0xCE , 0xCE , 0xCE , 0xCE ,
78
+ 0xCE , 0x8C , 0x8C , 0x8C ,
79
+ 0xCE , 0xCE , 0xCE , 0xCE };
80
+ static ColorSignal GREEN_Signal = { 0xCE , 0x8C , 0x8C , 0x8C ,
81
+ 0xCE , 0xCE , 0xCE , 0xCE ,
82
+ 0xCE , 0xCE , 0xCE , 0xCE };
83
+ static ColorSignal BLUE_Signal = { 0xCE , 0xCE , 0xCE , 0xCE ,
84
+ 0xCE , 0xCE , 0xCE , 0xCE ,
85
+ 0xCE , 0x8C , 0x8C , 0x8C };
86
+ static ColorSignal BLACK_Signal = { 0xCE , 0xCE , 0xCE , 0xCE ,
87
+ 0xCE , 0xCE , 0xCE , 0xCE ,
88
+ 0xCE , 0xCE , 0xCE , 0xCE };
17
89
18
90
static volatile bool _running;
19
91
@@ -46,7 +118,7 @@ int main()
46
118
// immediately with a failure status if the output can't be written immediately.
47
119
//
48
120
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
49
- uart0_filestream = open (" /dev/ttyAMA0" , O_RDWR | O_NOCTTY | O_NDELAY); // Open in non blocking read/write mode
121
+ uart0_filestream = open (" /dev/ttyAMA0" , O_WRONLY | O_NOCTTY | O_NDELAY); // Open in non blocking read/write mode
50
122
if (uart0_filestream == -1 )
51
123
{
52
124
// ERROR - CAN'T OPEN SERIAL PORT
@@ -67,17 +139,18 @@ int main()
67
139
// PARODD - Odd parity (else even)
68
140
struct termios options;
69
141
tcgetattr (uart0_filestream, &options);
70
- options.c_cflag = B4000000 | CS8 | CLOCAL | CREAD ; // <Set baud rate
142
+ options.c_cflag = B4000000 | CS8 | CLOCAL; // <Set baud rate
71
143
options.c_iflag = IGNPAR;
72
144
options.c_oflag = 0 ;
73
145
options.c_lflag = 0 ;
74
- tcflush (uart0_filestream, TCIFLUSH );
146
+ cfmakeraw (&options );
75
147
76
148
std::cout << " options.c_cflag = " << options.c_cflag << std::endl;
77
149
std::cout << " options.c_iflag = " << options.c_iflag << std::endl;
78
150
std::cout << " options.c_oflag = " << options.c_oflag << std::endl;
79
151
std::cout << " options.c_lflag = " << options.c_lflag << std::endl;
80
152
153
+ tcflush (uart0_filestream, TCIFLUSH);
81
154
tcsetattr (uart0_filestream, TCSANOW, &options);
82
155
// Let's verify configured options
83
156
tcgetattr (uart0_filestream, &options);
@@ -128,47 +201,9 @@ int main()
128
201
}
129
202
130
203
// ----- TX BYTES -----
131
- uint8_t tx_buffer[3 *3 *8 *4 ];
132
- uint8_t *p_tx_buffer;
133
-
134
- // for (int i=0; i<3; ++i)
135
- // {
136
- // Writing 0xFF, 0x00, 0x00
137
- // *p_tx_buffer++ = 0x8C;
138
- // *p_tx_buffer++ = 0x8C;
139
- // *p_tx_buffer++ = 0x8C;
140
- // *p_tx_buffer++ = 0x8C;
141
-
142
- std::default_random_engine generator;
143
- std::uniform_int_distribution<int > distribution (1 ,2 );
144
-
145
- p_tx_buffer = &tx_buffer[0 ];
146
- for (int i=0 ; i<9 ; ++i)
147
- {
148
- int coinFlip = distribution (generator);
149
- if (coinFlip == 1 )
150
- {
151
- *p_tx_buffer++ = 0xCE ;
152
- *p_tx_buffer++ = 0xCE ;
153
- *p_tx_buffer++ = 0xCE ;
154
- *p_tx_buffer++ = 0xCE ;
155
- }
156
- else
157
- {
158
- *p_tx_buffer++ = 0x8C ;
159
- *p_tx_buffer++ = 0x8C ;
160
- *p_tx_buffer++ = 0x8C ;
161
- *p_tx_buffer++ = 0x8C ;
162
- }
163
- }
164
-
165
- std::cout << " Binary stream: [" ;
166
- for (unsigned char * txIt=&(tx_buffer[0 ]); txIt!=p_tx_buffer; ++txIt)
167
- {
168
- std::cout << " 1 " << (std::bitset<8 >) (*txIt) << " 0 " ;
169
- }
170
- std::cout << " ]" << std::endl;
204
+ std::vector<ColorSignal> signalData (10 , RED_Signal);
171
205
206
+ int loopCnt = 0 ;
172
207
std::cout << " Type 'c' to continue, 'q' or 'x' to quit: " ;
173
208
while (_running)
174
209
{
@@ -182,38 +217,28 @@ int main()
182
217
continue ;
183
218
}
184
219
185
- int count = write (uart0_filestream, &tx_buffer[ 0 ], (p_tx_buffer - &tx_buffer[ 0 ])); // Filestream, bytes to write, number of bytes to write
186
- if (count < 0 )
220
+ set_realtime_priority ();
221
+ for ( int iRun= 0 ; iRun< 10 ; ++iRun )
187
222
{
188
- std::cerr << " UART TX error" << std::endl;
223
+ // tcflush(uart0_filestream, TCOFLUSH);
224
+ write (uart0_filestream, signalData.data (), signalData.size ()*sizeof (ColorSignal));
225
+ tcdrain (uart0_filestream);
189
226
190
- // ----- CLOSE THE UART -----
191
- close (uart0_filestream);
192
- return -1 ;
193
- }
194
- std::cout << " Writing " << count << " bytes to uart" << std::endl;
227
+ usleep (100000 );
228
+ ++loopCnt;
229
+
230
+ if (loopCnt%3 == 2 )
231
+ signalData = std::vector<ColorSignal>(10 , GREEN_Signal);
232
+ else if (loopCnt%3 == 1 )
233
+ signalData = std::vector<ColorSignal>(10 , BLUE_Signal);
234
+ else if (loopCnt%3 == 0 )
235
+ signalData = std::vector<ColorSignal>(10 , RED_Signal);
195
236
196
- p_tx_buffer = &tx_buffer[0 ];
197
- for (int i=0 ; i<9 ; ++i)
198
- {
199
- int coinFlip = distribution (generator);
200
- if (coinFlip == 1 )
201
- {
202
- *p_tx_buffer++ = 0xCE ;
203
- *p_tx_buffer++ = 0xCE ;
204
- *p_tx_buffer++ = 0xCE ;
205
- *p_tx_buffer++ = 0xCE ;
206
- }
207
- else
208
- {
209
- *p_tx_buffer++ = 0x8C ;
210
- *p_tx_buffer++ = 0x8C ;
211
- *p_tx_buffer++ = 0x8C ;
212
- *p_tx_buffer++ = 0x8C ;
213
- }
214
237
}
215
238
}
216
239
240
+ signalData = std::vector<ColorSignal>(50 , BLACK_Signal);
241
+ write (uart0_filestream, signalData.data (), signalData.size ()*sizeof (ColorSignal));
217
242
// ----- CLOSE THE UART -----
218
243
close (uart0_filestream);
219
244
0 commit comments