Skip to content

Commit

Permalink
Merge pull request #256 from jgauchia/devel_refactor
Browse files Browse the repository at this point in the history
Refactor code
  • Loading branch information
jgauchia authored Nov 24, 2024
2 parents 60cabcb + 373f4ec commit 2aa43be
Show file tree
Hide file tree
Showing 50 changed files with 253 additions and 338 deletions.
98 changes: 48 additions & 50 deletions lib/battery/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,73 +9,71 @@

#include "battery.hpp"

/**
* @brief Battery values
*
*/
uint8_t battLevel = 0;
uint8_t battLevelOld = 0;
float batteryMax = 4.2; // maximum voltage of battery
float batteryMin = 3.6; // minimum voltage of battery before shutdown
Battery::Battery() {}

/**
* @brief Configure ADC Channel for battery reading
*
*/
void initADC()
void Battery::initADC()
{
// When VDD_A is 3.3V:
// 0dB attenuation (ADC_ATTEN_DB_0) gives full-scale voltage 1.1V
// 2.5dB attenuation (ADC_ATTEN_DB_2_5) gives full-scale voltage 1.5V
// 6dB attenuation (ADC_ATTEN_DB_6) gives full-scale voltage 2.2V
// 12dB attenuation (ADC_ATTEN_DB_12) gives full-scale voltage 3.9V

#ifdef ADC1
#ifdef ADC1
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
#endif
adc1_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
#endif

#ifdef ADC2
#ifdef ADC2
adc2_config_channel_atten(BATT_PIN, ADC_ATTEN_DB_12);
#endif
#endif
}

/**
* @brief Set battery voltage levels
*
* @param maxVoltage -> Full Charge voltage
* @param minVoltage -> Min Charge voltage
*/
void Battery::setBatteryLevels(float maxVoltage, float minVoltage)
{
batteryMax = maxVoltage;
batteryMin = minVoltage;
}

/**
* @brief Read battery charge and return %
* @brief Read battery charge and return %.
*
* @return float -> % Charge
*/
float batteryRead()
float Battery::readBattery()
{
long sum = 0; // sum of samples taken
float voltage = 0.0; // calculated voltage
float output = 0.0; // output value
for (int i = 0; i < 100; i++)
{
long sum = 0; // Sum of samples taken
float voltage = 0.0; // Calculated voltage
float output = 0.0; // Output value

for (int i = 0; i < 100; i++)
{
#ifdef ADC1
sum += static_cast<long>(adc1_get_raw(BATT_PIN));
#endif

#ifdef ADC2
int readRaw;
esp_err_t r = adc2_get_raw(BATT_PIN, ADC_WIDTH_BIT_12, &readRaw);
if (r == ESP_OK)
sum += static_cast<long>(readRaw);
#endif

#ifdef ADC1
sum += (long)adc1_get_raw(BATT_PIN);
#endif
delayMicroseconds(150);
}

#ifdef ADC2
int readRaw;
esp_err_t r = adc2_get_raw(BATT_PIN, ADC_WIDTH_BIT_12, &readRaw);
if (r == ESP_OK)
sum += (long)readRaw;
#endif
voltage = sum / 100.0;
// Custom board has a divider circuit
constexpr float R1 = 100000.0; // Resistance of R1 (100K)
constexpr float R2 = 100000.0; // Resistance of R2 (100K)
voltage = (voltage * V_REF) / 4096.0;
voltage = voltage / (R2 / (R1 + R2));
voltage = roundf(voltage * 100) / 100;

delayMicroseconds(150);
}
voltage = sum / (float)100;
// custom board has a divider circuit
float R1 = 100000.0; // resistance of R1 (100K)
float R2 = 100000.0; // resistance of R2 (100K)
voltage = (voltage * V_REF) / 4096.0;
voltage = voltage / (R2 / (R1 + R2));
voltage = roundf(voltage * 100) / 100;
output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100;
if (output <= 160)
return output;
else
return 0.0f;
output = ((voltage - batteryMin) / (batteryMax - batteryMin)) * 100;
return (output <= 160) ? output : 0.0f;
}
21 changes: 12 additions & 9 deletions lib/battery/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
#include <driver/adc.h>
#include <esp_adc_cal.h>

static esp_adc_cal_characteristics_t characteristics;
#define V_REF 3.9 // ADC reference voltage
class Battery
{
private:
float batteryMax;
float batteryMin;
static constexpr float V_REF = 3.9; // ADC reference voltage

extern float batteryMax; // 4.2; // maximum voltage of battery
extern float batteryMin; // 3.6; // minimum voltage of battery before shutdown
public:
Battery();

extern uint8_t battLevel;
extern uint8_t battLevelOld;

void initADC();
float batteryRead();
void initADC();
void setBatteryLevels(float maxVoltage, float minVoltage);
float readBattery();
};

#endif
4 changes: 3 additions & 1 deletion lib/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const char logo[] =
""
;

extern Power power;

/**
* @brief Reboot ESP
*/
Expand All @@ -35,7 +37,7 @@ void wcli_reboot(char *args, Stream *response)
*/
void wcli_poweroff(char *args, Stream *response)
{
deviceShutdown();
power.deviceShutdown();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/gpx/src/addWaypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "addWaypoint.hpp"

extern const int SD_CS;
extern const uint8_t SD_CS;

wayPoint addWpt = {0, 0, 0, (char *)"", (char *)"", (char *)"", (char *)"", (char *)"", (char *)"", 0, 0, 0, 0};
wayPoint loadWpt = {0, 0, 0, (char *)"", (char *)"", (char *)"", (char *)"", (char *)"", (char *)"", 0, 0, 0, 0};
Expand Down
6 changes: 1 addition & 5 deletions lib/gpx/src/addWaypoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
#ifndef ADDWAYPOINT_HPP
#define ADDWAYPOINT_HPP

#include "globalGpxDef.h"
#include "SD.h"
#include "FS.h"
#include "tft.hpp"
#include "storage.hpp"
#include "lvgl.h"
#include "globalGpxDef.h"

extern wayPoint addWpt;

Expand Down
4 changes: 0 additions & 4 deletions lib/gpx/src/deleteWaypoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#define DELETEWAYPOINT_HPP

#include "SD.h"
#include "FS.h"
#include "tft.hpp"
#include "storage.hpp"
#include "globalGuiDef.h"
#include "globalGpxDef.h"

void deleteWaypointName(char * wpt);
Expand Down
4 changes: 0 additions & 4 deletions lib/gpx/src/editWaypoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#define EDITWAYPOINT_HPP

#include "SD.h"
#include "FS.h"
#include "tft.hpp"
#include "storage.hpp"
#include "globalGuiDef.h"
#include "globalGpxDef.h"

void editWaypointName(char* oldName, char *newName);
Expand Down
5 changes: 0 additions & 5 deletions lib/gpx/src/loadWaypoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#ifndef LOADWAYPOINT_HPP
#define LOADWAYPOINT_HPP

#include "SD.h"
#include "FS.h"
#include "tft.hpp"
#include "storage.hpp"
#include "globalGuiDef.h"
#include "globalGpxDef.h"

extern wayPoint loadWpt;
Expand Down
5 changes: 0 additions & 5 deletions lib/gui/src/buttonBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
*/

#include "buttonBar.hpp"
#include "waypointScr.hpp"
#include "waypointListScr.hpp"
#include "display/lv_display.h"
#include "globalGuiDef.h"

bool isWaypointOpt = false;
bool isTrackOpt = false;
Expand Down Expand Up @@ -59,7 +55,6 @@ void buttonBarEvent(lv_event_t *event)
if (strcmp(option,"waypoint") == 0)
{
log_v("Waypoint");
// isMainScreen = false;
isWaypointOpt = true;
isTrackOpt = false;
if (!isOptionLoaded)
Expand Down
2 changes: 2 additions & 0 deletions lib/gui/src/buttonBar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "globalGuiDef.h"
#include "mainScr.hpp"
#include "waypointScr.hpp"
#include "waypointListScr.hpp"

static const char *waypointIconFile PROGMEM = "/wpt.bin"; // Waypoint icon
static const char *trackIconFile PROGMEM = "/track.bin"; // Track icon
Expand Down
1 change: 0 additions & 1 deletion lib/gui/src/deviceSettingsScr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

#include "deviceSettingsScr.hpp"
#include "globalGuiDef.h"

lv_obj_t *deviceSettingsScreen; // Device Settings Screen

Expand Down
2 changes: 0 additions & 2 deletions lib/gui/src/deviceSettingsScr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#define DEVICESETTINGSCR_HPP

#include "globalGuiDef.h"
#include "gps.hpp"
#include "settings.hpp"
#include "lvglSetup.hpp"

static lv_obj_t *deviceSettingsOptions;
Expand Down
9 changes: 2 additions & 7 deletions lib/gui/src/globalGuiDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,9 @@ static bool getPngSize(const char* filename, uint16_t *width, uint16_t *height)
return false;
}

byte table[32];
uint8_t table[32];

// for (int i = 0; file.available() && i < 32; i++)
// {
// table[i] = file.read();
// }

fread(table, sizeof(byte), 32, file);
fread(table, sizeof(uint8_t), 32, file);

*width=table[16]*256*256*256+table[17]*256*256+table[18]*256+table[19];
*height=table[20]*256*256*256+table[21]*256*256+table[22]*256+table[23];
Expand Down
30 changes: 4 additions & 26 deletions lib/gui/src/mainScr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@
*/

#include "mainScr.hpp"
#include "buttonBar.hpp"
#include "core/lv_obj.h"
#include "core/lv_obj_pos.h"
#include "globalGuiDef.h"
#include "globalMapsDef.h"
#include "settings.hpp"
#include "tft.hpp"

extern const int SD_CS;
extern const uint8_t TFT_SPI_CS;

bool isMainScreen = false; // Flag to indicate main screen is selected
bool isScrolled = true; // Flag to indicate when tileview was scrolled
Expand All @@ -27,12 +17,12 @@ uint8_t wptAction = WPT_NONE; // Current Waypoint Action
int wptPosX, wptPosY = 0; // Waypoint position on map

#ifdef LARGE_SCREEN
int toolBarOffset = 100;
int toolBarSpace = 60;
uint8_t toolBarOffset = 100;
uint8_t toolBarSpace = 60;
#endif
#ifndef LARGE_SCREEN
int toolBarOffset = 80;
int toolBarSpace = 50;
uint8_t toolBarOffset = 80;
uint8_t toolBarSpace = 50;
#endif

lv_obj_t *tilesScreen;
Expand All @@ -59,10 +49,8 @@ void updateCompassScr(lv_event_t * event)
lv_obj_t *obj = (lv_obj_t *)lv_event_get_current_target(event);
if (obj==compassHeading)
{
//#ifdef ENABLE_COMPASS
lv_label_set_text_fmt(compassHeading, "%5d\xC2\xB0", heading);
lv_img_set_angle(compassImg, -(heading * 10));
//#endif
}
if (obj==latitude)
lv_label_set_text_fmt(latitude, "%s", latFormatString(gpsData.latitude));
Expand All @@ -86,11 +74,6 @@ void getActTile(lv_event_t *event)
isScrolled = true;
redrawMap = true;

if (activeTile == SATTRACK)
{
// createSatSprite(spriteSat);
// createConstelSprite(constelSprite);
}
if (activeTile == MAP)
{
createMapScrSprites();
Expand Down Expand Up @@ -206,7 +189,6 @@ void updateMainScreen(lv_timer_t *t)
*/
void gestureEvent(lv_event_t *event)
{
lv_obj_t *screen = (lv_obj_t *)lv_event_get_current_target(event);
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
if (activeTile == MAP && isMainScreen)
{
Expand Down Expand Up @@ -241,8 +223,6 @@ void updateMap(lv_event_t *event)

getMapBlocks(viewPort.bbox, memCache);

// deleteMapScrSprites();
// createMapScrSprites();
generateVectorMap(viewPort, memCache, mapTempSprite);

isPosMoved = false;
Expand Down Expand Up @@ -419,9 +399,7 @@ void updateNavEvent(lv_event_t *event)
if (wptDistance == 0)
{
lv_img_set_src(arrowNav, &navfinish);
//#ifdef ENABLE_COMPASS
lv_img_set_angle(arrowNav, 0);
//#endif
}
else
{
Expand Down
Loading

0 comments on commit 2aa43be

Please sign in to comment.