diff --git a/firmware/src/logic/cfg_mem.cpp b/firmware/src/logic/cfg_mem.cpp new file mode 100644 index 0000000..8196dda --- /dev/null +++ b/firmware/src/logic/cfg_mem.cpp @@ -0,0 +1,138 @@ +/**** Includes ****/ +#include "cfg_mem.h" +#include "../bsp/mcu/mcu_hal.h" + +using namespace logic; + +/**** Private definitions ****/ +/**** Private constants ****/ +const uint16_t addr_btn_force = 0x0000; +const uint16_t addr_bmode = 0x0001; +const uint16_t addr_pot_mode = 0x0002; +const uint16_t addr_dsp_brigth = 0x0003; +const uint16_t addr_dsp_dimm = 0x0004; +const uint16_t addr_lock_current = 0x0005; + +/**** Private variables ****/ +/**** Private function declarations ****/ +/**** Public function definitions ****/ +logic::CfgMemory::CfgMemory(void) +{ + this->mem_btn_force = 0; + this->mem_bmode = 0; + this->mem_pot_mode = 0; + this->mem_dsp_brigth = 0; + this->mem_dsp_dimm = 0; + this->mem_lock_current = 0; + + this->restore(); +} + +logic::CfgMemory::~CfgMemory(void) +{ + return; +} + +void logic::CfgMemory::init(void) +{ + this->mem_btn_force = mcu::eeprom_read8b(addr_btn_force); + this->mem_bmode = mcu::eeprom_read8b(addr_bmode); + this->mem_pot_mode = mcu::eeprom_read8b(addr_pot_mode); + this->mem_dsp_brigth = mcu::eeprom_read8b(addr_dsp_brigth); + this->mem_dsp_dimm = mcu::eeprom_read8b(addr_dsp_dimm); + this->mem_lock_current = mcu::eeprom_read16b(addr_lock_current); + + if(this->mem_btn_force > 100) + { + this->mem_btn_force = 0; + mcu::eeprom_write8b(addr_btn_force, this->mem_btn_force); + }; + + if(this->mem_bmode > 2) + { + this->mem_bmode = 0; + mcu::eeprom_write8b(addr_bmode, this->mem_bmode); + }; + + if(this->mem_pot_mode > 1) + { + this->mem_pot_mode = 0; + mcu::eeprom_write8b(addr_pot_mode, this->mem_pot_mode); + }; + + if(this->mem_dsp_brigth > 100) + { + this->mem_dsp_brigth = 100; + mcu::eeprom_write8b(addr_dsp_brigth, this->mem_dsp_brigth); + }; + + if(this->mem_dsp_dimm > 100) + { + this->mem_dsp_dimm = 50; + mcu::eeprom_write8b(addr_dsp_dimm, this->mem_dsp_dimm); + }; + + if(this->mem_lock_current > 6000) + { + this->mem_lock_current = 4500; + mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current); + }; + + this->restore(); +} + +void logic::CfgMemory::save(void) +{ + if(this->btn_force != this->mem_btn_force) + { + this->mem_btn_force = this->btn_force; + mcu::eeprom_write8b(addr_btn_force, this->mem_btn_force); + }; + + if(this->bmode != this->mem_bmode) + { + this->mem_bmode = this->bmode; + mcu::eeprom_write8b(addr_bmode, this->mem_bmode); + }; +} + +void logic::CfgMemory::save_all(void) +{ + this->save(); + + if(this->pot_mode != this->mem_pot_mode) + { + this->mem_pot_mode = this->pot_mode; + mcu::eeprom_write8b(addr_pot_mode, this->mem_pot_mode); + }; + + if(this->dsp_brigth != this->mem_dsp_brigth) + { + this->mem_dsp_brigth = this->dsp_brigth; + mcu::eeprom_write8b(addr_dsp_brigth, this->mem_dsp_brigth); + }; + + if(this->dsp_dimm != this->mem_dsp_dimm) + { + this->mem_dsp_dimm = this->dsp_dimm; + mcu::eeprom_write8b(addr_dsp_dimm, this->mem_dsp_dimm); + }; + + if(this->lock_current != this->mem_lock_current) + { + this->mem_lock_current = this->lock_current; + mcu::eeprom_write8b(addr_lock_current, this->mem_lock_current); + }; +} + +void logic::CfgMemory::restore(void) +{ + this->btn_force = this->mem_btn_force; + this->bmode = this->mem_bmode; + this->pot_mode = this->mem_pot_mode; + this->dsp_brigth = this->mem_dsp_brigth; + this->dsp_dimm = this->mem_dsp_dimm; + this->lock_current = this->mem_lock_current; +} + +/**** Private function definitions ****/ diff --git a/firmware/src/logic/cfg_mem.h b/firmware/src/logic/cfg_mem.h new file mode 100644 index 0000000..45b513e --- /dev/null +++ b/firmware/src/logic/cfg_mem.h @@ -0,0 +1,46 @@ +#ifndef CONFIG_H_ +#define CONFIG_H_ + +/**** Includes ****/ +#include +#include "../hw/button.h" + +namespace logic { + +/**** Public definitions ****/ + +class CfgMemory +{ + protected: + uint8_t mem_btn_force; + uint8_t mem_bmode; + uint8_t mem_pot_mode; + uint8_t mem_dsp_brigth; + uint8_t mem_dsp_dimm; + uint16_t mem_lock_current; + + public: + CfgMemory(void); + ~CfgMemory(void); + + uint8_t btn_force; + uint8_t bmode; + uint8_t pot_mode; + uint8_t dsp_brigth; + uint8_t dsp_dimm; + uint16_t lock_current; + + void init(void); + void save(void); + void save_all(void); + void restore(void); +}; + +/**** Public function declarations ****/ + +#ifdef TESTING +#endif + +} //namespace + +#endif /* DCCD_FORCE_H_ */ \ No newline at end of file diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 5975207..7d723ee 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -7,9 +7,13 @@ #include "logic/button_force.h" #include "logic/dccd_force.h" +#include "logic/cfg_mem.h" + /**** Private definitions ****/ /**** Private constants ****/ /**** Private variables ****/ +static logic::CfgMemory cfg_mem = logic::CfgMemory(); + static logic::ButtonForce button_force = logic::ButtonForce(&btn_up, &btn_down); static logic::DccdForce dccd_force = logic::DccdForce(&btn_mode, &sw_hbrake, &sw_brakes); @@ -20,51 +24,13 @@ int main(void) // HW setup devices_init(); + // Read saved config + cfg_mem.init(); + uint8_t user_force = 0; - button_force.force = mcu::eeprom_read8b(0x0000); - dccd_force.brake_mode = mcu::eeprom_read8b(0x0001); - uint8_t pot_mode = mcu::eeprom_read8b(0x0002); - uint8_t dsp_brigth = mcu::eeprom_read8b(0x0003); - uint8_t dsp_dimm = mcu::eeprom_read8b(0x0004); - uint16_t lock_current = mcu::eeprom_read16b(0x0005); - - // Safeguard eeprom values - if(button_force.force > 100) - { - button_force.force = 0; - mcu::eeprom_write8b(0x0000, button_force.force); - }; - - if(dccd_force.brake_mode > 2) - { - button_force.force = 0; - mcu::eeprom_write8b(0x0001, dccd_force.brake_mode); - }; - - if(pot_mode > 1) - { - pot_mode = 0; - mcu::eeprom_write8b(0x0002, pot_mode); - }; - - if(dsp_brigth > 100) - { - dsp_brigth = 100; - mcu::eeprom_write8b(0x0003, dsp_brigth); - }; - - if(dsp_dimm > 100) - { - dsp_dimm = 50; - mcu::eeprom_write8b(0x0004, dsp_dimm); - }; - - if(lock_current > 6000) - { - lock_current = 4500; - mcu::eeprom_write16b(0x0005, lock_current); - }; + button_force.force = cfg_mem.btn_force; + dccd_force.brake_mode = cfg_mem.bmode; // Super loop while(1) @@ -76,7 +42,7 @@ int main(void) button_force.update(); // Select user force input - if(pot_mode) user_force = pot.percent; + if(cfg_mem.pot_mode) user_force = pot.percent; else user_force = button_force.force; // Calculate next target force @@ -86,7 +52,7 @@ int main(void) if((sup_fuse.fault)||(out_fuse.fault)) dccd_force.force = 0; // Convert force to current - ccout.target = util::percent_of(dccd_force.force, lock_current); + ccout.target = util::percent_of(dccd_force.force, cfg_mem.lock_current); // Execute outputs ccout.update(); @@ -95,21 +61,13 @@ int main(void) display.show_percent(dccd_force.force, hw::DisplayLed::LED_DSP_DOT10); // Process dimm - if(sw_dimm.state == hw::BUTTON_ON) display.set_brigthness(dsp_dimm); - else display.set_brigthness(dsp_brigth); + if(sw_dimm.state == hw::BUTTON_ON) display.set_brigthness(cfg_mem.dsp_dimm); + else display.set_brigthness(cfg_mem.dsp_brigth); // Save user setting changes - if(button_force.is_new) - { - mcu::eeprom_write8b(0x0000, button_force.force); - button_force.is_new = 0; - }; - - if(dccd_force.is_new_bmode) - { - mcu::eeprom_write8b(0x0001, dccd_force.brake_mode); - dccd_force.is_new_bmode = 0; - }; + cfg_mem.btn_force = button_force.force; + cfg_mem.bmode = dccd_force.brake_mode; + cfg_mem.save(); continue; // End of super loop } diff --git a/firmware/src/uDCCD.cppproj b/firmware/src/uDCCD.cppproj index cf0e235..a4b7be9 100644 --- a/firmware/src/uDCCD.cppproj +++ b/firmware/src/uDCCD.cppproj @@ -20,10 +20,10 @@ false true true - + 0x20000000 true - + exception_table 2 0 0 @@ -249,6 +249,12 @@ compile + + compile + + + compile + compile