35 uint32_t lastWaitUntilCommandAcceptMs = 0;
36 uint32_t lastRestartMs = 0;
43 Im920sL(HardwareSerial& uart, uint16_t nodeNum, uint8_t busyPin)
55 Im920sL(HardwareSerial& uart, uint16_t nodeNum)
78 operator bool()
const override {
return uart && not isTimeout(700); }
82 bool begin(uint8_t channel);
92 Serial.print(
"IM920sL: OK ");
96 Serial.print(
"IM920sL: NG ");
99 switch (getTransmitMode())
101 case TransmitMode::Send: Serial.print(
"SendMode ");
break;
102 case TransmitMode::Receive: Serial.print(
"ReceiveMode ");
break;
103 case TransmitMode::TwoWay: Serial.print(
"TwoWayMode ");
break;
104 case TransmitMode::Empty: Serial.print(
"Empty ");
break;
112 waitUntilCommandAccept();
113 uart.print(
"RDNN\r\n");
114 Serial.print(uart.readStringUntil(
'\n'));
127 return constrain(channel, 1, 45);
131 enum class TransmitMode
140 TransmitMode getTransmitMode()
const
146 return TransmitMode::TwoWay;
150 return TransmitMode::Send;
155 return TransmitMode::Receive;
159 return TransmitMode::Empty;
170 const int sendTimeMs = 52;
172 if (millis() - txNode->
transmitMs < sendTimeMs)
182 { uart.write(data); });
187 if (uart.readStringUntil(
'\n') ==
"OK\r")
198 constexpr int HeaderSize = 2 + 1 + 4 + 1 + 2 + 1;
201 constexpr int FooterSize = 1 + 1;
207 if (uart.available() < FrameSize)
213 for (
int i = 0; i < HeaderSize; ++i)
220 { return uart.read(); }))
223 while (uart.available())
231 for (
int i = 0; i < FooterSize; ++i)
237 while (uart.available())
250 const double sendTime = 52.0;
251 const double receiveTime = 52.0;
253 static uint32_t lastTransmitMs = 0;
255 if (millis() - lastTransmitMs > sendTime + receiveTime && receiveUpdate())
258 lastTransmitMs = millis();
262 if (millis() - lastTransmitMs > (uint32_t)random(2000))
265 lastTransmitMs = millis();
270 bool isTimeout(uint32_t timeoutMs)
const
272 switch (getTransmitMode())
274 case TransmitMode::Send:
275 return millis() - txNode->
transmitMs > timeoutMs;
276 case TransmitMode::Receive:
277 return millis() - rxNode->
transmitMs > timeoutMs;
278 case TransmitMode::TwoWay:
280 case TransmitMode::Empty:
287 void RestartOnTimeout()
289 if (not isTimeout(2000))
295 if (millis() - lastRestartMs > 2000)
297 waitUntilCommandAccept();
298 uart.print(
"SRST\r\n");
299 lastRestartMs = millis();
300 while (uart.available())
308 void waitUntilCommandAccept()
312 lastWaitUntilCommandAcceptMs = millis();
314 while (digitalRead(*busyPin))
316 delayMicroseconds(10);
318 if (millis() - lastWaitUntilCommandAcceptMs > 200)
330 while (uart.available())
344 pinMode(*busyPin, INPUT);
351 waitUntilCommandAccept();
352 uart.print(
"RPRM\r\n");
354 (void)uart.readStringUntil(
'\n');
355 (void)uart.readStringUntil(
'\n');
356 (void)uart.readStringUntil(
'\n');
357 (void)uart.readStringUntil(
'\n');
358 const int defaultChannel = uart.readStringUntil(
'\n').substring(5).toInt();
359 const int defaultPower = uart.readStringUntil(
'\n').substring(5).toInt();
360 (void)uart.readStringUntil(
'\n');
361 (void)uart.readStringUntil(
'\n');
362 (void)uart.readStringUntil(
'\n');
363 (void)uart.readStringUntil(
'\n');
364 (void)uart.readStringUntil(
'\n');
365 (void)uart.readStringUntil(
'\n');
366 (void)uart.readStringUntil(
'\n');
367 (void)uart.readStringUntil(
'\n');
368 (void)uart.readStringUntil(
'\n');
369 (void)uart.readStringUntil(
'\n');
370 (void)uart.readStringUntil(
'\n');
371 (void)uart.readStringUntil(
'\n');
372 (void)uart.readStringUntil(
'\n');
373 const String defaultCharIOMode = uart.readStringUntil(
'\n');
374 (void)uart.readStringUntil(
'\n');
375 (void)uart.readStringUntil(
'\n');
376 (void)uart.readStringUntil(
'\n');
377 (void)uart.readStringUntil(
'\n');
378 (void)uart.readStringUntil(
'\n');
379 (void)uart.readStringUntil(
'\n');
380 (void)uart.readStringUntil(
'\n');
381 (void)uart.readStringUntil(
'\n');
382 (void)uart.readStringUntil(
'\n');
383 (void)uart.readStringUntil(
'\n');
384 (void)uart.readStringUntil(
'\n');
385 (void)uart.readStringUntil(
'\n');
386 const String defaultStatusOutputOnReceipt = uart.readStringUntil(
'\n');
387 (void)uart.readStringUntil(
'\n');
390 waitUntilCommandAccept();
391 uart.print(
"ENWR\r\n");
392 if (uart.readStringUntil(
'\n') !=
"OK\r")
398 if (uart.readStringUntil(
'\n') !=
"OK\r")
404 if (channel != defaultChannel)
406 waitUntilCommandAccept();
408 if (uart.readStringUntil(
'\n') !=
"OK\r")
415 if (defaultPower != 2)
417 waitUntilCommandAccept();
419 if (uart.readStringUntil(
'\n') !=
"OK\r")
426 if (defaultCharIOMode !=
"ECIO\r")
428 waitUntilCommandAccept();
429 uart.print(
"ECIO\r\n");
430 if (uart.readStringUntil(
'\n') !=
"OK\r")
437 if (defaultStatusOutputOnReceipt !=
"ERXI\r")
439 waitUntilCommandAccept();
440 uart.print(
"ERXI\r\n");
441 if (uart.readStringUntil(
'\n') !=
"OK\r")
448 waitUntilCommandAccept();
449 uart.print(
"DSWR\r\n");
450 if (uart.readStringUntil(
'\n') !=
"OK\r")
void BitPack(const InputIterator begin, const InputIterator end, Functor callback)
バイト列のMSB(最上位ビット)を抽出し、7bitごとに分割してコールバック関数に渡す
Definition BitPack.hpp:23