voltage-protector/Core/Src/display.c

110 lines
3.1 KiB
C
Raw Permalink Normal View History

2023-03-10 20:19:41 +01:00
#include "display.h"
#include <stddef.h>
#include <stdio.h>
#include "main.h"
#include "ssd1306.h"
#include "ssd1306_conf.h"
2023-04-11 19:56:38 +02:00
#define xstr(a) str(a)
#define str(a) #a
2023-03-10 20:19:41 +01:00
2023-04-11 19:56:38 +02:00
static const char *err_msg(ErrReason err) {
switch (err) {
case ERR_NONE:
return "UNKNOWN ERR";
case ERR_BAT_GT_CHARGE:
return "VBAT > VPSU";
case ERR_BAT_LT_DISCHARGE:
return "VBAT < VLOAD";
case ERR_VBAT_TOO_LOW:
return "VBAT < " xstr(V_MIN_VOLT);
case ERR_VCDC_TOO_LOW:
if (state == STATE_CHARGE || state == STATE_ERR_CHARGE ||
state == STATE_SETUP_CHARGE) {
return "VPSU < " xstr(V_MIN_VOLT);
} else {
return "VLOAD < " xstr(V_MIN_VOLT);
}
case ERR_OVERVOLTAGE:
return "OVERVOLTAGE";
case ERR_UNDERVOLTAGE:
return "UNDERVOLTAGE";
2023-03-10 20:19:41 +01:00
}
2023-04-11 19:56:38 +02:00
return "UNKNOWN ERROR";
2023-03-10 20:19:41 +01:00
}
void display_init() {
ssd1306_Init();
2023-04-11 19:56:38 +02:00
display_update_voltage(v_thresh);
2023-03-10 20:19:41 +01:00
ssd1306_WriteChar('V', Font_16x26, White);
display_update_state();
ssd1306_UpdateScreen();
}
2023-04-11 19:56:38 +02:00
void display_update_voltage(int32_t v) {
2023-03-10 20:19:41 +01:00
char voltage_buf[7];
2023-04-11 19:56:38 +02:00
char *ptr = voltage_buf;
ptr += snprintf(ptr, 4, "%3d", v / 100);
snprintf(ptr, 4, ".%02d", v % 100);
2023-03-10 20:19:41 +01:00
ssd1306_SetCursor(0, 38);
ssd1306_WriteString(voltage_buf, Font_16x26, White);
}
void display_update_state() {
switch (state) {
case STATE_SETUP_CHARGE:
2023-04-11 19:56:38 +02:00
display_update_voltage(v_thresh);
2023-03-10 20:19:41 +01:00
ssd1306_FillRectangle(0, 0, SSD1306_WIDTH - 1, 30, Black);
ssd1306_SetCursor(0, 0);
ssd1306_WriteString("Charge", Font_6x8, White);
ssd1306_SetCursor(0, 10);
ssd1306_WriteString("Press to close relay", Font_6x8, White);
break;
case STATE_SETUP_DISCHARGE:
2023-04-11 19:56:38 +02:00
display_update_voltage(v_thresh);
2023-03-10 20:19:41 +01:00
ssd1306_FillRectangle(0, 0, SSD1306_WIDTH - 1, 30, Black);
ssd1306_SetCursor(0, 0);
ssd1306_WriteString("Discharge", Font_6x8, White);
ssd1306_SetCursor(0, 10);
ssd1306_WriteString("Press to close relay", Font_6x8, White);
break;
case STATE_CHARGE:
2023-04-11 19:56:38 +02:00
display_update_voltage(v_bat);
2023-03-10 20:19:41 +01:00
ssd1306_FillRectangle(0, 0, SSD1306_WIDTH - 1, 30, Black);
ssd1306_SetCursor(0, 0);
ssd1306_WriteString("CHARGING", Font_11x18, White);
break;
case STATE_DISCHARGE:
2023-04-11 19:56:38 +02:00
display_update_voltage(v_bat);
2023-03-10 20:19:41 +01:00
ssd1306_FillRectangle(0, 0, SSD1306_WIDTH - 1, 30, Black);
ssd1306_SetCursor(0, 0);
ssd1306_WriteString("DISCHARGING", Font_11x18, White);
break;
case STATE_ERR_CHARGE:
2023-04-11 19:56:38 +02:00
display_update_voltage(v_bat);
2023-03-10 20:19:41 +01:00
ssd1306_FillRectangle(0, 0, SSD1306_WIDTH - 1, 30, White);
2023-04-11 19:56:38 +02:00
ssd1306_FillRectangle(3, 3, SSD1306_WIDTH - 1 - 3, 30 - 3, Black);
2023-03-10 20:19:41 +01:00
ssd1306_SetCursor(10, 5);
2023-04-11 19:56:38 +02:00
ssd1306_WriteString("ERROR:", Font_6x8, White);
2023-03-10 20:19:41 +01:00
ssd1306_SetCursor(10, 15);
2023-04-11 19:56:38 +02:00
ssd1306_WriteString(err_msg(error), Font_6x8, White);
2023-03-10 20:19:41 +01:00
break;
case STATE_ERR_DISCHARGE:
2023-04-11 19:56:38 +02:00
display_update_voltage(v_bat);
2023-03-10 20:19:41 +01:00
ssd1306_FillRectangle(0, 0, SSD1306_WIDTH - 1, 30, White);
2023-04-11 19:56:38 +02:00
ssd1306_FillRectangle(3, 3, SSD1306_WIDTH - 1 - 3, 30 - 3, Black);
2023-03-10 20:19:41 +01:00
ssd1306_SetCursor(10, 5);
2023-04-11 19:56:38 +02:00
ssd1306_WriteString("ERROR:", Font_6x8, White);
2023-03-10 20:19:41 +01:00
ssd1306_SetCursor(10, 15);
2023-04-11 19:56:38 +02:00
ssd1306_WriteString(err_msg(error), Font_6x8, White);
2023-03-10 20:19:41 +01:00
break;
}
}
void display_update() {
display_update_state();
ssd1306_UpdateScreen();
}