33#include < chrono>
44#include < fstream>
55#include < cstdint>
6+ #include < cstring>
7+
8+ #include < thread>
9+ #include < queue>
10+ #include < mutex>
11+ #include < condition_variable>
12+ #include < string>
13+
614#include " sensor_data.h"
715#include " log_format.h"
816#include " crc.h"
917
18+ #define STR2 (x ) #x
19+ #define STR (x ) STR2(x)
20+ #pragma message "__cplusplus=" STR(__cplusplus)
21+
1022size_t struct_sizes[16 ];
1123
1224#define ASSOCIATE (ty, id ) struct_sizes[id] = sizeof (ty)
13- #define DEBUG
25+ // #define DEBUG
1426
1527FILE* inptr;
1628FILE* outptr;
@@ -101,15 +113,58 @@ bool read_entry(entry_t& entry) {
101113 return true ;
102114}
103115
116+ class InputReader {
117+
118+ private:
119+
120+ std::queue<std::string> _q;
121+ std::istream& _in;
122+ std::thread _th;
123+ std::mutex _mut;
124+ std::condition_variable _cv;
125+ bool _stop = false ;
126+
127+ void run () {
128+ std::string line;
129+
130+ while (std::getline (_in, line)) {
131+ { std::lock_guard<std::mutex> lk (_mut); _q.push (line); }
132+ _cv.notify_one ();
133+ }
134+ // EOF or error: mark stop so mainside can finish if desired
135+ { std::lock_guard<std::mutex> lk (_mut); _stop = true ; }
136+ _cv.notify_all ();
137+ }
138+
139+ public:
140+ explicit InputReader (std::istream& in) : _in(in), _th([this ]{ run (); }) {}
141+ ~InputReader () {
142+ {
143+ std::lock_guard<std::mutex> lk (_mut); _stop = true ;
144+ }
145+ _cv.notify_all ();
146+ _th.join ();
147+ }
148+
149+ bool get (std::string& out) {
150+ std::lock_guard<std::mutex> lk (_mut);
151+ if (_q.empty ()) { return false ; }
152+ out = std::move (_q.front ());
153+ _q.pop ();
154+ return true ;
155+ }
156+ };
157+
104158
105159
106160void send_data (serialib& s, entry_t & dat) {
107161 // $ 4 bytes ts, 1 byte disc, n bytes payload, 2 bytes crc
162+ size_t size_of_send_buf = 0 ;
108163 uint8_t buf[150 ];
109- buf[0 ] = (uint8_t )' $' ;
164+ buf[0 ] = (uint8_t )' $' ; size_of_send_buf += 1 ;
110165
111- memcpy (buf + 1 , &dat.raw_data_stream , dat.raw_data_stream_size );
112- memcpy (buf + 1 + dat.raw_data_stream_size , &dat.crc , sizeof (uint16_t ));
166+ memcpy (buf + 1 , &dat.raw_data_stream , dat.raw_data_stream_size ); size_of_send_buf += dat. raw_data_stream_size ;
167+ memcpy (buf + 1 + dat.raw_data_stream_size , &dat.crc , sizeof (uint16_t )); size_of_send_buf += sizeof ( uint16_t );
113168
114169 if (outptr) {
115170 fputs (" .D $" , outptr);
@@ -127,7 +182,7 @@ void send_data(serialib& s, entry_t& dat) {
127182 // }
128183 // printf("\n");
129184 #else
130- s.writeBytes (buf, dat. _data_size + num_overhead_bytes );
185+ s.writeBytes (buf, size_of_send_buf );
131186 #endif
132187}
133188
@@ -141,6 +196,7 @@ int main(int argc, char** argv) {
141196 setup_ssizes ();
142197
143198 serialib Serial;
199+ bool _ser_open = false ;
144200
145201 printf (" Awaiting commands from stdin...\n " );
146202
@@ -153,11 +209,25 @@ int main(int argc, char** argv) {
153209 auto start_time = std::chrono::high_resolution_clock::now ();
154210 auto current_time = std::chrono::high_resolution_clock::now ();
155211
212+ InputReader _ireader (std::cin);
213+ std::string line;
214+
156215 while (true ) {
157216
217+ // handle serial output
218+ if (_ser_open) {
219+ // do something
220+
221+ // TBD
222+ }
223+
224+ // Handle inputs from stdin
225+ if (!_ireader.get (line)) {
226+ continue ;
227+ }
228+
158229 char _inbuf[255 ];
159- fgets (_inbuf, 255 , stdin);
160- // std::cout << (uint8_t)cmd << std::endl;
230+ memcpy (_inbuf, line.c_str (), line.length () + 2 );
161231
162232 switch (_inbuf[0 ]) {
163233 case ' S' :
@@ -169,8 +239,10 @@ int main(int argc, char** argv) {
169239 char serial_open_err = Serial.openDevice (sbuf, 115200 );
170240 if (serial_open_err != 1 ) {
171241 printf (" .SERIAL_BAD\n " );
242+ _ser_open = false ;
172243 } else {
173244 printf (" .SERIAL_OK\n " );
245+ _ser_open = true ;
174246 }
175247 fflush (stdout);
176248 }
@@ -267,6 +339,13 @@ int main(int argc, char** argv) {
267339 fflush (stdout);
268340 if (!inptr) {
269341 std::cerr << " No file specified" << std::endl;
342+ printf (" .ERR no_ifs\n " );
343+ break ;
344+ }
345+
346+ if (!_ser_open) {
347+ std::cerr << " No serial open" << std::endl;
348+ printf (" .ERR no_ser\n " );
270349 break ;
271350 }
272351
0 commit comments