Skip to content

Commit

Permalink
feat: added smartflash lib
Browse files Browse the repository at this point in the history
  • Loading branch information
LasaleFamine committed Jul 22, 2024
1 parent 2d91241 commit efc3e88
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"configuration": "debug=l0,supportat=1",
"configuration": "debug=l0,supportat=1,supportlora=1,supportAS923=1,supportAU915=1,supportCN470=1,supportCN779=1,supportEU433=1,supportEU868=1,supportKR920=1,supportIN865=1,supportUS915=1,supportRU864=1,supportLA915=1",
"board": "rak_rui:stm32:WisDuoRAK3172EvaluationBoard",
"sketch": "src/src.ino",
"output": "build",
Expand Down
8 changes: 8 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
LualtekRAKRUI KEYWORD1
getBatteryVoltage KEYWORD2
getBatteryVoltage_10x KEYWORD2
getUplinkInterval KEYWORD2
handleChangeDutyCycle KEYWORD2
join KEYWORD2
Expand All @@ -8,5 +9,12 @@ send KEYWORD2
setClass KEYWORD2
setup KEYWORD2
setupTimers KEYWORD2
SmartFlash KEYWORD1
getCO2Altitude KEYWORD2
getCO2MeasurementDelay KEYWORD2
getUplinkIntervalIndex KEYWORD2
saveCO2Altitude KEYWORD2
saveCO2MeasurementDelay KEYWORD2
saveUplinkIntervalIndex KEYWORD2
DutyCycleHandler KEYWORD1
changeDutyCycle KEYWORD2
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lualtek-rakrui",
"version": "0.3.5",
"version": "0.3.6",
"description": "An opinionated wrapper around RUI3 APIs",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=LualtekRAKRUI
version=0.3.5
version=0.3.6
author=Lualtek
maintainer=Lualtek <[email protected]>
sentence=Library for the Lualtek RAK RUI3 based board.
Expand Down
10 changes: 6 additions & 4 deletions src/DutyCycleHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "DutyCycleHandler.h"
#include "SmartFlash.h"

SmartFlash smartflash;

DutyCycleHandler::DutyCycleHandler(lualtek_dowlink_command_dutycycle_index_t defaultDutyCycleIndex)
: defaultDutyCycleIndex(defaultDutyCycleIndex),
uplinkInterval(dutyCycleCommandTable[defaultDutyCycleIndex]),
previousMillis(0)
{
// Setup duty cycle from flash memory if available or use default
uint8_t uplinkIntervalFlash[1];
api.system.flash.get(0, uplinkIntervalFlash, 1);
changeDutyCycle(isDutyCycleIndex(uplinkIntervalFlash[0]) ? dutyCycleCommandTable[uplinkIntervalFlash[0]] : MINUTES_20_IN_MILLISECONDS);
uint8_t uplinkIntervalFlashIndex = smartflash.getUplinkIntervalIndex();
changeDutyCycle(isDutyCycleIndex(uplinkIntervalFlashIndex) ? dutyCycleCommandTable[uplinkIntervalFlashIndex] : MINUTES_20_IN_MILLISECONDS);
}

bool DutyCycleHandler::isDutyCycleIndex(unsigned int commandIndex)
Expand All @@ -25,5 +27,5 @@ void DutyCycleHandler::changeDutyCycle(int commandIndex)

uplinkInterval = dutyCycleCommandTable[commandIndex];
uint8_t commandIndexBuffer[1] = {commandIndex};
api.system.flash.set(0, commandIndexBuffer, 1);
smartflash.saveUplinkIntervalIndex(commandIndex);
}
3 changes: 1 addition & 2 deletions src/LualtekRAKRUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ float convertToBatteryVoltage(int adcReading)
/*
* To be used with getBatteryVoltage_10x()
*/

float convertToBatteryVoltage_RAK19007(int adcReading)
{
for (int i = 0; i < tableSize - 1; ++i)
Expand Down Expand Up @@ -311,4 +310,4 @@ int LualtekRAKRUI::getBatteryVoltage_10x()
analogReadResolution(10);
adc_value = sum / 10;
return convertToBatteryVoltage_RAK19007(adc_value) * 1000;
}
}
43 changes: 43 additions & 0 deletions src/SmartFlash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "SmartFlash.h"

SmartFlash::SmartFlash() {}

bool SmartFlash::saveUplinkIntervalIndex(uint8_t interval)
{
_intervalIndex = interval;
return writeToFlash(UPLINK_INTERVAL_OFFSET, &interval, sizeof(_intervalIndex));
}

uint8_t SmartFlash::getUplinkIntervalIndex()
{
return readFromFlash(UPLINK_INTERVAL_OFFSET, &_intervalIndex, sizeof(_intervalIndex));
}

bool SmartFlash::saveCO2MeasurementDelay(uint8_t data)
{
_co2MeasurementDelay = data;
_co2MeasurementDelayOffset = UPLINK_INTERVAL_OFFSET + sizeof(_intervalIndex);
return writeToFlash(_co2MeasurementDelayOffset, &data, sizeof(_co2MeasurementDelay));
}

uint8_t SmartFlash::getCO2MeasurementDelay()
{
return readFromFlash(_co2MeasurementDelayOffset, &_co2MeasurementDelay, sizeof(_co2MeasurementDelay));
}

bool SmartFlash::saveCO2Altitude(uint8_t data)
{
_co2Altitude = data;
_co2AltitudeOffset = _co2MeasurementDelayOffset + sizeof(_co2MeasurementDelay);
return writeToFlash(_co2AltitudeOffset, &data, sizeof(_co2Altitude));
}

bool SmartFlash::writeToFlash(uint32_t offset, uint8_t *data, size_t length)
{
return api.system.flash.set(offset, data, length);
}

bool SmartFlash::readFromFlash(uint32_t offset, uint8_t *data, size_t length)
{
return api.system.flash.get(offset, data, length);
}
34 changes: 34 additions & 0 deletions src/SmartFlash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef _SMARTFLASH_H_
#define _SMARTFLASH_H_

#include <Arduino.h>

#define UPLINK_INTERVAL_OFFSET 0

class SmartFlash
{
public:
SmartFlash();

// Save and retrieve uplink interval
bool saveUplinkIntervalIndex(uint8_t interval);
uint8_t getUplinkIntervalIndex();

// Save and retrieve CO2 sensor data
bool saveCO2MeasurementDelay(uint8_t data);
uint8_t getCO2MeasurementDelay();

bool saveCO2Altitude(uint8_t data);
uint8_t getCO2Altitude();

private:
uint8_t _intervalIndex = 0;
uint8_t _co2MeasurementDelay = 0;
uint8_t _co2MeasurementDelayOffset = 0;
uint8_t _co2Altitude = 0;
uint8_t _co2AltitudeOffset = 0;
bool writeToFlash(uint32_t offset, uint8_t *data, size_t length);
bool readFromFlash(uint32_t offset, uint8_t *data, size_t length);
};

#endif // SMARTFLASH_H

0 comments on commit efc3e88

Please sign in to comment.