Files
uDCCD/firmware/logic/settings.c
2024-03-12 21:23:47 +02:00

185 lines
3.1 KiB
C

/**** Includes ****/
#include "settings.h"
/**** Private definitions ****/
/**** Private constants ****/
static const uint8_t addr_force = 0x01;
static const uint8_t addr_bmode = 0x02;
static const uint8_t addr_inmode = 0x03;
static const uint8_t addr_dsp_bright = 0x04;
static const uint8_t addr_dsp_dimm = 0x05;
static const uint8_t addr_lock_amps = 0x06;
/**** Private variables ****/
static uint16_t save_force_timer = 0;
static uint16_t save_bmode_timer = 0;
/**** Private function declarations ****/
/**** Public function definitions ****/
void Setings_Update(uint8_t is_new_force, uint8_t force, uint8_t is_new_bmode, brakeMode_t bmode)
{
if(is_new_force) save_force_timer = 5000;
if(is_new_bmode) save_bmode_timer = 5000;
if(save_force_timer)
{
save_force_timer--;
if(!save_force_timer)
{
// Save force setting
Setings_SaveForce(force);
};
};
if(save_bmode_timer)
{
save_bmode_timer--;
if(!save_bmode_timer)
{
// Save mode setting
Setings_SaveBrakeMode(bmode);
};
};
}
void Setings_SaveForce(uint8_t value)
{
MEM_Write8b(addr_force, value);
}
uint8_t Setings_GetForce(void)
{
uint8_t val = MEM_Read8b(addr_force);
if(val > 100) return 0;
else return val;
}
void Setings_SaveBrakeMode(brakeMode_t bmode)
{
// Convert and save input mode setting
// Convert and save mode setting
uint8_t val = 0x00;
switch(bmode)
{
case BM_LOCK:
val = 'L';
break;
case BM_KEEP:
val = 'K';
break;
default:
val = 'O';
break;
}
MEM_Write8b(addr_bmode, val);
}
brakeMode_t Setings_GetBrakeMode(void)
{
// Convert and return mode setting
uint8_t val = MEM_Read8b(addr_bmode);
switch(val)
{
case 'L':
return BM_LOCK;
case 'K':
return BM_KEEP;
default:
return BM_OPEN;
}
}
void Setings_SaveInputMode(inputMode_t inmode)
{
// Convert and save input mode setting
uint8_t val = 0x00;
switch(inmode)
{
case IM_POT:
val = 'P';
break;
default:
val = 'B';
break;
}
MEM_Write8b(addr_inmode, val);
}
inputMode_t Setings_GetInputMode(void)
{
// Convert and return input mode setting
uint8_t val = MEM_Read8b(addr_inmode);
switch(val)
{
case 'P':
return IM_POT;
default:
return IM_BUTTONS;
}
}
void Setings_SaveDisplayBrigthLvl(uint8_t value)
{
MEM_Write8b(addr_dsp_bright, value);
}
uint8_t Setings_GetDisplayBrigthLvl(void)
{
uint8_t val = MEM_Read8b(addr_dsp_bright);
if(val > 100) return 100;
else return val;
}
void Setings_SaveDisplayDimmLvl(uint8_t value)
{
MEM_Write8b(addr_dsp_dimm, value);
}
uint8_t Setings_GetDisplayDimmLvl(void)
{
uint8_t val = MEM_Read8b(addr_dsp_dimm);
if(val > 100) return 50;
else return val;
}
void Setings_SaveLockCurrent(uint16_t value)
{
MEM_Write16b(addr_lock_amps, value);
}
uint16_t Setings_GetLockCurrent(void)
{
uint16_t val = MEM_Read16b(addr_lock_amps);
if(val > 6000) return 4500;
else return val;
}
void Setings_SaveDefault(void)
{
Setings_SaveForce(0);
Setings_SaveBrakeMode(BM_OPEN);
Setings_SaveInputMode(IM_BUTTONS);
Setings_SaveDisplayBrigthLvl(100);
Setings_SaveDisplayDimmLvl(50);
Setings_SaveLockCurrent(4500);
}
/**** Private function definitions ****/