diff --git a/src/app/MBPeripherals.cpp b/src/app/MBPeripherals.cpp index a9e2665..704c9c0 100644 --- a/src/app/MBPeripherals.cpp +++ b/src/app/MBPeripherals.cpp @@ -2,7 +2,7 @@ void defaultExtIntIsr(void){} -MBPeripherals::MBPeripherals(const BoardConfig &boardConfig, void (*extIntIsr)(void)): _boardConfig(boardConfig), _NRF(boardConfig.NRFCe, boardConfig.NRFCs) +MBPeripherals::MBPeripherals(const BoardConfig &boardConfig, void (*extIntIsr)(void)): _boardConfig(boardConfig) { if(!extIntIsr) _extIntIsr = &(defaultExtIntIsr); //We provide a default ISR @@ -22,23 +22,34 @@ uint8_t MBPeripherals::init() //Unused pins are set as inputs with internal pullup enabled to reduce power consumption during sleep pinMode(0,INPUT_PULLUP); //pinMode(1,INPUT_PULLUP); TX pin for serial - pinMode(6,INPUT_PULLUP); - pinMode(7,INPUT_PULLUP); - pinMode(8,INPUT_PULLUP); - pinMode(9,INPUT_PULLUP); - pinMode(10,INPUT_PULLUP); - pinMode(11,INPUT_PULLUP); - pinMode(12,INPUT_PULLUP); - pinMode(A2,INPUT_PULLUP); - pinMode(A3,INPUT_PULLUP); - pinMode(A4,INPUT_PULLUP); - pinMode(A5,INPUT_PULLUP); - pinMode(A6,INPUT_PULLUP); - pinMode(A7,INPUT_PULLUP); + pinMode(3, INPUT_PULLUP); + // Set to input pull up because it is its rest state + pinMode(_boardConfig.SwitchVSensEnable, INPUT_PULLUP); + pinMode(9, INPUT_PULLUP); + + //RFM95W control pins, these are set in the deinitExternalPeripherals method. + //pinMode(6, INPUT_PULLUP); + //pinMode(8, INPUT_PULLUP); + //pinMode(10, INPUT_PULLUP); + //pinMode(11, INPUT_PULLUP); + //pinMode(12, INPUT_PULLUP); + //pinMode(13, INPUT_PULLUP); + + //These pins are all unused + pinMode(A2, INPUT_PULLUP); + pinMode(A3, INPUT_PULLUP); + pinMode(A4, INPUT_PULLUP); + pinMode(A5, INPUT_PULLUP); + pinMode(A6, INPUT_PULLUP); + pinMode(A7, INPUT_PULLUP); + + //Defines the RFM95W IOs for the class (does not physically set pin states) + _RFM95W.setPins(_boardConfig.RFMCs, _boardConfig.RFMRst, _boardConfig.RFMDI0); //We check that every external devices are responding _3V3PowerRail(ON); - toReturn |= _NRF.begin(); + toReturn |= initExternalPeripherals(); + deinitExternalPeripherals(); _3V3PowerRail(OFF); attachInterrupt(digitalPinToInterrupt(_boardConfig.ExternalInt), _extIntIsr, FALLING); @@ -50,11 +61,24 @@ uint8_t MBPeripherals::initExternalPeripherals(void) { uint8_t toReturn(0); - toReturn |= _NRF.begin(); + toReturn |= _RFM95W.begin(LORA_BAND); return toReturn; } +void MBPeripherals::deinitExternalPeripherals(void) +{ + //Let's properly turn the deinitialize the RFM95W module + _RFM95W.end(); + //Don't forget to set associated radio IO's to input WITHOUT PULLUPS resistors (current leak when the module is not powered...) + pinMode(_boardConfig.RFMRst, INPUT); + pinMode(_boardConfig.RFMDI0, INPUT); + pinMode(_boardConfig.RFMCs, INPUT); + pinMode(_boardConfig.MOSI, INPUT); + pinMode(_boardConfig.MISO, INPUT); + pinMode(_boardConfig.SCK, INPUT); +} + float MBPeripherals::batteryVoltage(void) { //We close the voltage divider bridge and we do the measurement @@ -72,13 +96,13 @@ void MBPeripherals::_3V3PowerRail(State state) delay(10); } -const RF24 &MBPeripherals::getRadio(void){return _NRF;} +LoRaClass &MBPeripherals::getRadio(void) {return _RFM95W;} -void MBPeripherals::applyRadioConfig(uint8_t channel, uint8_t paLevel, bool enableLNA, rf24_datarate_e datarate) +void MBPeripherals::applyRadioConfig(int spreadingFactor, bool enableCRC, int txPower) { - _NRF.setChannel(channel); - _NRF.setPALevel(paLevel, enableLNA); - _NRF.setDataRate(datarate); + _RFM95W.setSpreadingFactor(spreadingFactor); + enableCRC ? _RFM95W.enableCrc() : _RFM95W.disableCrc(); + _RFM95W.setTxPower(txPower); } MAILBOX_EVENT_e MBPeripherals::readMailboxEvent(void) @@ -93,8 +117,9 @@ MAILBOX_EVENT_e MBPeripherals::readMailboxEvent(void) return MAILBOX_EVENT_e::MAILBOX_LETTER; else if(analogValue >= 526 && analogValue < 711) return MAILBOX_EVENT_e::MAILBOX_PACKAGE; + //The collected event is not used for the moment. else if(analogValue >= 711 && analogValue < 1000) - return MAILBOX_EVENT_e::MAILBOX_COLLECTED; + return MAILBOX_EVENT_e::MAILBOX_TEST; else return MAILBOX_EVENT_e::MAILBOX_UNKNOWN; } diff --git a/src/app/MBPeripherals.h b/src/app/MBPeripherals.h index f5b6dab..3dc9bcb 100644 --- a/src/app/MBPeripherals.h +++ b/src/app/MBPeripherals.h @@ -7,7 +7,7 @@ #ifndef MBPERIPHERALS_H #define MBPERIPHERALS_H -#include +#include #include #include "BoardConfig.h" @@ -28,14 +28,20 @@ class MBPeripherals uint8_t init(void); /* - * After calling this methode , you need to execute initExternalPeripherals() to init the peripherals. + * After calling this method with the ON parameter, you'll need to execute initExternalPeripherals() to init the peripherals. + * Don't forget to properly deinit devices using the deinitExternalPeripherals() method before turning off power to the external peripherals */ void externalPeripherals(State state){_3V3PowerRail(state);} /* - * Used to init devices after each LDO powerup, external devices need to be turned one externalPeripherals(ON) before calling this function. + * Used to init devices after each LDO powerups, external devices need to be turned on with externalPeripherals(ON) before calling this function. */ uint8_t initExternalPeripherals(void); + + /* + * Used to properly deinitialize devices before each LDO powerdowns. + */ + void deinitExternalPeripherals(void); /* * Starts a battery voltage measurement and returns a float value in Volts. @@ -44,9 +50,9 @@ class MBPeripherals /* * Before calling this method, externalPeripherals(ON) and initExternalPeripherals() must be called respectively. - * This methods applies the NRF radio configuration. + * This methods applies the RFM95W radio base configuration. */ - void applyRadioConfig(uint8_t channel = RADIO_CHANNEL, uint8_t paLevel = RADIO_PA_LEVEL, bool enableLNA = ENABLE_LNA, rf24_datarate_e datarate = RADIO_DATARATE); + void applyRadioConfig(int spreadingFactor = LORA_SPREADING_FACTOR, bool enableCRC = RFM95W_ENABLE_CRC, int txPower = RFM95W_TX_POWER); /* * Use this function just after the interrupt wake up retrieve the wake up event. @@ -65,15 +71,15 @@ class MBPeripherals void unmaskExtInt(void); /* - * Gets the NRF radio object to access it's api. + * Gets the LoRaClass object to access its api. */ - const RF24 &getRadio(void); + LoRaClass &getRadio(void); protected: private: void _3V3PowerRail(State state); const BoardConfig &_boardConfig; - const RF24 _NRF; + LoRaClass _RFM95W; ExtIntIsr _extIntIsr; };