void output_byte(uint8_t rs, uint8_t data, uint8_t en) { output_nibble(rs, HIGH_NIBBLE(data), en); output_nibble(rs, LOW_NIBBLE(data), en); #ifdef HD44780_READBACK /* wait until command is executed by checking busy flag, with timeout */ /* max execution time is for return home command, * which takes at most 1.52ms = 1520us */ uint8_t busy, timeout = 200; do { busy = input_byte(0,en) & _BV(BUSY_FLAG); _delay_us(10); timeout--; } while (busy && timeout > 0); #ifdef DEBUG if (timeout == 0) debug_printf("lcd timeout!\n"); #endif #else /* just wait the maximal time a command can take... */ _delay_ms(2); #endif }
/** * @brief Sends the command together with the given, paramValue (optional) * @param Command should include a trailing space if paramValue is set. Refer to RN2483 command ref * @param Command Parameter to send * @param Size of param buffer * @return Returns true on success or false if invalid. */ bool RN2483::sendCommand(const char* command, const uint8_t* paramValue, uint16_t size) { _RN2483.printf(command); for (uint16_t i = 0; i < size; ++i) { _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(paramValue[i])))); _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(paramValue[i])))); } _RN2483.printf(CRLF); return expectOK(); }
uint8_t Sodaq_RN2483::macTransmit(const char* type, uint8_t port, const uint8_t* payload, uint8_t size) { debugPrintLn("[macTransmit]"); this->loraStream->print(STR_CMD_MAC_TX); this->loraStream->print(type); this->loraStream->print(port); this->loraStream->print(" "); for (int i = 0; i < size; ++i) { this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(payload[i])))); this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(payload[i])))); } this->loraStream->print(CRLF); if (!expectOK()) { return lookupMacTransmitError(this->inputBuffer); // inputBuffer still has the last line read } this->packetReceived = false; // prepare for receiving a new packet debugPrint("Waiting for server response"); unsigned long timeout = millis() + RECEIVE_TIMEOUT; // hard timeout while (millis() < timeout) { sodaq_wdt_reset(); debugPrint("."); if (readLn() > 0) { debugPrintLn(".");debugPrint("(");debugPrint(this->inputBuffer);debugPrintLn(")"); if (strstr(this->inputBuffer, " ") != NULL) // to avoid double delimiter search { // there is a splittable line -only case known is mac_rx debugPrintLn("Splittable response found"); return onMacRX(); } else if (strstr(this->inputBuffer, STR_RESULT_MAC_TX_OK)) { // done debugPrintLn("Received mac_tx_ok"); return NoError; } else { // lookup the error message debugPrintLn("Some other string received (error)"); return lookupMacTransmitError(this->inputBuffer); } } } debugPrintLn("Timed-out waiting for a response!"); return Timeout; }
/** * @brief Sends the command together with the given paramValue (optional) * @param MAC param should include a trailing space if paramValue is set. Refer to RN2483 command ref. * @param Param value to send * @param Size of Param buffer * @return Returns true on success or false if invalid. */ bool RN2483::setMacParam(const char* paramName, const uint8_t* paramValue, uint16_t size) { printf("RN2483: Set MAC param: %s\n"); _RN2483.printf(STR_CMD_SET); _RN2483.printf(paramName); for (uint16_t i = 0; i < size; ++i) { _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(paramValue[i])))); _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(paramValue[i])))); } _RN2483.printf(CRLF); return expectOK(); }
// Sends the given mac command together with the given paramValue // to the device and awaits for the response. // Returns true on success. // NOTE: paramName should include a trailing space bool Sodaq_RN2483::setMacParam(const char* paramName, const uint8_t* paramValue, uint16_t size) { debugPrint("[setMacParam] "); debugPrint(paramName); debugPrint("= [array]"); this->loraStream->print(STR_CMD_SET); this->loraStream->print(paramName); for (uint16_t i = 0; i < size; ++i) { this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(paramValue[i])))); this->loraStream->print(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(paramValue[i])))); } this->loraStream->print(CRLF); return expectOK(); }
/** * @brief Sends a a payload and blocks until there is a response back, * or the receive windows have closed or the hard timeout has passed. * @param Transmit type * @param Port to use for transmit * @param Payload buffer * @param Size of payload buffer * @return Returns if sucessfull or if a MAC transmit error. */ uint8_t RN2483::macTransmit(const char* type, uint8_t port, const uint8_t* payload, uint8_t size) { _RN2483.printf(STR_CMD_MAC_TX); _RN2483.printf(type); _RN2483.printf("%u",port); _RN2483.printf(" "); for (int i = 0; i < size; ++i) { _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(HIGH_NIBBLE(payload[i])))); _RN2483.putc(static_cast<char>(NIBBLE_TO_HEX_CHAR(LOW_NIBBLE(payload[i])))); } _RN2483.printf(CRLF); if (!expectOK()) { return lookupMacTransmitError(this->inputBuffer); // inputBuffer still has the last line read } this->packetReceived = false; // prepare for receiving a new packet Timer t; t.start(); int timeout = t.read_ms() + RECEIVE_TIMEOUT; // hard timeouts while (t.read_ms() < timeout) { if (readLn() > 0) { if (strstr(this->inputBuffer, " ") != NULL) { // to avoid double delimiter search // there is a splittable line -only case known is mac_rx t.stop(); return onMacRX(); } else if (strstr(this->inputBuffer, STR_RESULT_MAC_TX_OK)) { // done t.stop(); return NoError; } else { // lookup the error message t.stop(); return lookupMacTransmitError(this->inputBuffer); } } } t.stop(); return Timedout; }