Skip to content

Commit

Permalink
Add support for Arduino Nano 33 BLE #40
Browse files Browse the repository at this point in the history
  • Loading branch information
xreef committed Feb 23, 2022
1 parent 26dfa41 commit f075870
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 137 deletions.
20 changes: 16 additions & 4 deletions LoRa_E32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ LoRa_E32::LoRa_E32(SoftwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin,
#endif

bool LoRa_E32::begin(){
DEBUG_PRINT("RX MIC ---> ");
DEBUG_PRINTLN(this->txE32pin);
DEBUG_PRINT("TX MIC ---> ");
DEBUG_PRINTLN(this->rxE32pin);
DEBUG_PRINT("AUX ---> ");
DEBUG_PRINTLN(this->auxPin);
DEBUG_PRINT("M0 ---> ");
DEBUG_PRINTLN(this->m0Pin);
DEBUG_PRINT("M1 ---> ");
DEBUG_PRINTLN(this->m1Pin);

if (this->auxPin != -1) {
pinMode(this->auxPin, INPUT);
DEBUG_PRINTLN("Init AUX pin!");
Expand Down Expand Up @@ -336,15 +347,15 @@ Method to indicate availability
*/

int LoRa_E32::available(unsigned long timeout) {
int LoRa_E32::available() {
// unsigned long t = millis();
//
// // make darn sure millis() is not about to reach max data type limit and start over
// if (((unsigned long) (t + timeout)) == 0){
// t = 0;
// }
//
// if (this->auxPin != 0) {
// if (this->auxPin != -1) {
// if (digitalRead(this->auxPin) == HIGH){
// return 0;
// }else{
Expand Down Expand Up @@ -534,7 +545,8 @@ MODE_TYPE LoRa_E32::getMode(){

void LoRa_E32::writeProgramCommand(PROGRAM_COMMAND cmd){
uint8_t CMD[3] = {cmd, cmd, cmd};
uint8_t size = this->serialDef.stream->write(CMD, 3);
// uint8_t size =
this->serialDef.stream->write(CMD, 3);
DEBUG_PRINTLN(size);
this->managedDelay(50); //need ti check
}
Expand Down Expand Up @@ -792,7 +804,7 @@ ResponseStatus LoRa_E32::sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const
return this->sendFixedMessage(ADDH, ADDL, CHAN, (uint8_t *)messageFixed, size);
}
ResponseStatus LoRa_E32::sendBroadcastFixedMessage(byte CHAN, const String message){
return this->sendFixedMessage(0xFF, 0xFF, CHAN, message);
return this->sendFixedMessage(BROADCAST_ADDRESS, BROADCAST_ADDRESS, CHAN, message);
}

typedef struct fixedStransmission
Expand Down
158 changes: 78 additions & 80 deletions LoRa_E32.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#ifndef LoRa_E32_h
#define LoRa_E32_h

#if !defined(__STM32F1__) && !defined(ESP32) && !defined(ARDUINO_ARCH_SAMD)
#if !defined(__STM32F1__) && !defined(ESP32) && !defined(ARDUINO_ARCH_SAMD) &&!defined(ARDUINO_ARCH_MBED)
#define ACTIVATE_SOFTWARE_SERIAL
#endif
#if defined(ESP32)
Expand Down Expand Up @@ -72,12 +72,12 @@

enum MODE_TYPE
{
MODE_0_NORMAL = 0,
MODE_1_WAKE_UP = 1,
MODE_2_POWER_SAVING = 2,
MODE_3_SLEEP = 3,
MODE_3_PROGRAM =3,
MODE_INIT = 0xFF
MODE_0_NORMAL = 0,
MODE_1_WAKE_UP = 1,
MODE_2_POWER_SAVING = 2,
MODE_3_SLEEP = 3,
MODE_3_PROGRAM = 3,
MODE_INIT = 0xFF
};

enum PROGRAM_COMMAND
Expand Down Expand Up @@ -182,27 +182,27 @@ struct ResponseContainer {

class LoRa_E32 {
public:
#ifdef ACTIVATE_SOFTWARE_SERIAL
LoRa_E32(byte txE32pin, byte rxE32pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(byte txE32pin, byte rxE32pin, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(byte txE32pin, byte rxE32pin, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#endif
#ifdef ACTIVATE_SOFTWARE_SERIAL
LoRa_E32(byte txE32pin, byte rxE32pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(byte txE32pin, byte rxE32pin, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(byte txE32pin, byte rxE32pin, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#endif

LoRa_E32(HardwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(HardwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(HardwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);

#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
LoRa_E32(byte txE32pin, byte rxE32pin, HardwareSerial* serial, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
LoRa_E32(byte txE32pin, byte rxE32pin, HardwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
LoRa_E32(byte txE32pin, byte rxE32pin, HardwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
#endif
#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
LoRa_E32(byte txE32pin, byte rxE32pin, HardwareSerial* serial, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
LoRa_E32(byte txE32pin, byte rxE32pin, HardwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
LoRa_E32(byte txE32pin, byte rxE32pin, HardwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate, uint32_t serialConfig = SERIAL_8N1);
#endif

#ifdef ACTIVATE_SOFTWARE_SERIAL
LoRa_E32(SoftwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(SoftwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(SoftwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#endif
#ifdef ACTIVATE_SOFTWARE_SERIAL
LoRa_E32(SoftwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(SoftwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
LoRa_E32(SoftwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
#endif

// LoRa_E32(byte txE32pin, byte rxE32pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600, MODE_TYPE mode = MODE_0_NORMAL);
// LoRa_E32(HardwareSerial* serial = &Serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600, MODE_TYPE mode = MODE_0_NORMAL);
Expand All @@ -216,30 +216,31 @@ class LoRa_E32 {
ResponseStatus setConfiguration(Configuration configuration, PROGRAM_COMMAND saveType = WRITE_CFG_PWR_DWN_LOSE);

ResponseStructContainer getModuleInformation();
ResponseStatus resetModule();
ResponseStatus resetModule();

ResponseStatus sendMessage(const void *message, const uint8_t size);
ResponseStructContainer receiveMessage(const uint8_t size);
ResponseStatus sendMessage(const void *message, const uint8_t size);

ResponseStatus sendMessage(const String message);
ResponseContainer receiveMessage();
ResponseContainer receiveMessageUntil(char delimiter = '\0');
ResponseStructContainer receiveMessage(const uint8_t size);

ResponseStatus sendFixedMessage(byte ADDH,byte ADDL, byte CHAN, const String message);
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const String message);
ResponseStatus sendMessage(const String message);
ResponseContainer receiveMessage();

ResponseStatus sendFixedMessage(byte ADDH, byte ADDL, byte CHAN, const String message);

ResponseStatus sendFixedMessage(byte ADDH,byte ADDL, byte CHAN, const void *message, const uint8_t size);
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const void *message, const uint8_t size );
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const void *message, const uint8_t size);
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const String message);

ResponseContainer receiveInitialMessage(const uint8_t size);
ResponseContainer receiveMessageUntil(char delimiter = '\0');
ResponseContainer receiveInitialMessage(const uint8_t size);

int available(unsigned long timeout = 1000);
int available();
private:
HardwareSerial* hs;

#ifdef ACTIVATE_SOFTWARE_SERIAL
SoftwareSerial* ss;
#endif
#ifdef ACTIVATE_SOFTWARE_SERIAL
SoftwareSerial* ss;
#endif

bool isSoftwareSerial = true;

Expand All @@ -248,26 +249,26 @@ class LoRa_E32 {
int8_t auxPin = -1;

#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
uint32_t serialConfig = SERIAL_8N1;
uint32_t serialConfig = SERIAL_8N1;
#endif

int8_t m0Pin = -1;
int8_t m1Pin = -1;
int8_t m0Pin = -1;
int8_t m1Pin = -1;

unsigned long halfKeyloqKey = 0x06660708;
unsigned long encrypt(unsigned long data);
unsigned long decrypt(unsigned long data);
unsigned long halfKeyloqKey = 0x06660708;
unsigned long encrypt(unsigned long data);
unsigned long decrypt(unsigned long data);

UART_BPS_RATE bpsRate = UART_BPS_RATE_9600;
UART_BPS_RATE bpsRate = UART_BPS_RATE_9600;

struct NeedsStream{
template< typename T >
void begin( T &t, int baud){
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud);
stream = &t;
}
struct NeedsStream {
template<typename T>
void begin(T &t, int baud) {
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud);
stream = &t;
}

#ifdef HARDWARE_SERIAL_SELECTABLE_PIN
// template< typename T >
Expand All @@ -278,47 +279,44 @@ class LoRa_E32 {
// stream = &t;
// }
//
template< typename T >
void begin( T &t, int baud, uint32_t config ){
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud, config);
stream = &t;
}

template< typename T >
void begin( T &t, int baud, uint32_t config, int8_t txE32pin, int8_t rxE32pin ){
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud, config, txE32pin, rxE32pin);
stream = &t;
}
template< typename T >
void begin( T &t, int baud, uint32_t config ) {
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud, config);
stream = &t;
}

template< typename T >
void begin( T &t, int baud, uint32_t config, int8_t txE32pin, int8_t rxE32pin ) {
DEBUG_PRINTLN("Begin ");
t.setTimeout(500);
t.begin(baud, config, txE32pin, rxE32pin);
stream = &t;
}
#endif

void listen(){

}

void listen() {}

Stream *stream;
Stream *stream;
};
NeedsStream serialDef;

MODE_TYPE mode = MODE_0_NORMAL;
MODE_TYPE mode = MODE_0_NORMAL;

void managedDelay(unsigned long timeout);
Status waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100);
void flush();
void cleanUARTBuffer();
void managedDelay(unsigned long timeout);
Status waitCompleteResponse(unsigned long timeout = 1000, unsigned int waitNoAux = 100);
void flush();
void cleanUARTBuffer();

Status sendStruct(void *structureManaged, uint16_t size_);
Status receiveStruct(void *structureManaged, uint16_t size_);
Status sendStruct(void *structureManaged, uint16_t size_);
Status receiveStruct(void *structureManaged, uint16_t size_);
void writeProgramCommand(PROGRAM_COMMAND cmd);

RESPONSE_STATUS checkUARTConfiguration(MODE_TYPE mode);
RESPONSE_STATUS checkUARTConfiguration(MODE_TYPE mode);

#ifdef LoRa_E32_DEBUG
void printParameters(struct Configuration *configuration);
void printParameters(struct Configuration *configuration);
#endif
};

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
7. [LoRa E32 device for Arduino, esp32 or esp8266: WOR (wake on radio) microcontroller and new WeMos D1 mini shield](https://www.mischianti.org/2020/01/17/lora-e32-device-for-arduino-esp32-or-esp8266-wor-wake-on-radio-microcontroller-and-new-wemos-d1-mini-shield-part-7/)

## Changelog
- 31/12/2021 1.5.3 Add support for Arduino SAMD boards
- 2021-01-24 1.5.4 Add Arduino Nano BLE support and minor adjustment
- 2021-12-31 1.5.3 Add support for Arduino SAMD boards

# An Arduino UNO shield to simplify the use
Arduino UNO shield
Expand Down Expand Up @@ -410,7 +411,8 @@ Type of transmission, pull-up settings, wake-up time, FEC, Transmission power

#### SPED detail

UART Parity bit: _UART mode can be different between communication parties_
UART Parity bit: _UART mode can be different between communication parties

|7|6|UART parity bit|Const value|
|---|---|---|---|---|
|0|0|8N1 (default)|MODE_00_8N1|
Expand Down
Loading

0 comments on commit f075870

Please sign in to comment.