-
Notifications
You must be signed in to change notification settings - Fork 95
Autobaud, sleep, wake changes #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,7 +287,7 @@ uint8_t receivedPort(const char *s) | |
return port; | ||
} | ||
|
||
TheThingsNetwork::TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb) | ||
TheThingsNetwork::TheThingsNetwork(SerialType &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb) | ||
{ | ||
this->debugStream = &debugStream; | ||
this->modemStream = &modemStream; | ||
|
@@ -387,6 +387,12 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u | |
return readLine(buffer, size); | ||
} | ||
|
||
size_t TheThingsNetwork::checkModuleAvailable() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a function like this is a good idea. I would however like to change it to check the version of the RNxxxx module and return a value from an enum stating which hardware and firmware version it is. We can then use the result to check before we set the frequency plan, and prevent errors when setting the wrong FP into the wrong module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense |
||
{ | ||
// Send sys get ver check we have an answer | ||
return readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer)); | ||
} | ||
|
||
void TheThingsNetwork::autoBaud() | ||
{ | ||
// Courtesy of @jpmeijers | ||
|
@@ -396,19 +402,51 @@ void TheThingsNetwork::autoBaud() | |
while (attempts-- && length == 0) | ||
{ | ||
delay(100); | ||
// Send break + Autobaud | ||
modemStream->write((byte)0x00); | ||
modemStream->write(0x55); | ||
modemStream->write(SEND_MSG); | ||
sendCommand(SYS_TABLE, 0, true, false); | ||
sendCommand(SYS_TABLE, SYS_GET, true, false); | ||
sendCommand(SYS_TABLE, SYS_GET_VER, false, false); | ||
modemStream->write(SEND_MSG); | ||
length = modemStream->readBytesUntil('\n', buffer, sizeof(buffer)); | ||
// check we can talk | ||
length = checkModuleAvailable(); | ||
|
||
// We succeeded talking to the module ? | ||
baudDetermined = (length > 0) ; | ||
} | ||
delay(100); | ||
clearReadBuffer(); | ||
modemStream->setTimeout(10000); | ||
baudDetermined = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually don't know why we added this flag to start with. Presumably to do a check before every But for now maybe let's just scrap the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to know and distinct 2 cases
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Maybe not a flag then but an enum. Something like this perhaps? Please suggest naming.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good, may be UNKNOWN => NOT_FOUND or NONE or MISSING |
||
} | ||
|
||
bool TheThingsNetwork::isSleeping() | ||
{ | ||
return !baudDetermined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rename this flag to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
|
||
void TheThingsNetwork::wake() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't much difference between the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I do not wanted to change your original one, but I prefer also to merge both, much cleaner |
||
{ | ||
// If sleeping | ||
if (isSleeping()) | ||
{ | ||
// Send a 0 at lower speed to be sure always received | ||
// as a character a 57600 baud rate | ||
modemStream->flush(); | ||
#ifdef HARDWARE_UART | ||
modemStream->begin(2400); | ||
#endif | ||
modemStream->write((uint8_t) 0x00); | ||
modemStream->flush(); | ||
delay(20); | ||
// set baudrate back to normal and send autobaud | ||
#ifdef HARDWARE_UART | ||
modemStream->begin(57600); | ||
#endif | ||
modemStream->write((uint8_t)0x55); | ||
modemStream->flush(); | ||
modemStream->write(SEND_MSG); | ||
if (checkModuleAvailable() > 0) { | ||
baudDetermined = true; | ||
} | ||
} | ||
} | ||
|
||
void TheThingsNetwork::reset(bool adr) | ||
|
@@ -969,11 +1007,9 @@ void TheThingsNetwork::sleep(uint32_t mseconds) | |
modemStream->write(buffer); | ||
modemStream->write(SEND_MSG); | ||
debugPrintLn(buffer); | ||
} | ||
|
||
void TheThingsNetwork::wake() | ||
{ | ||
autoBaud(); | ||
// to be determined back on wake up | ||
baudDetermined = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's fine |
||
} | ||
|
||
void TheThingsNetwork::linkCheck(uint16_t seconds) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stream
is changing to aSerialType
.modemStream
tomodemSerial
or something to avoid confusion?