Add device labels

This commit is contained in:
Sem van der Hoeven
2023-12-10 00:17:08 +01:00
parent cbab423f9b
commit c4df0a2302

View File

@@ -31,6 +31,8 @@ Program to create a car monitor display using:
#define FLAG_INIT_UPDATE_TEXT_POS 0x01 #define FLAG_INIT_UPDATE_TEXT_POS 0x01
#define FLAG_INIT_CLEAR_POS 0x02 #define FLAG_INIT_CLEAR_POS 0x02
#define FLAG_INIT_UPDATE_PERCENT_POS 0x03 #define FLAG_INIT_UPDATE_PERCENT_POS 0x03
#define FLAG_DEVICE_LABEL_UPDATE_POS 0x04
#define FLAG_DEVICE_LABEL_SHOULD_UPDATE_POS 0x05
#define LOGO_MIN_STEP 20 #define LOGO_MIN_STEP 20
#define LOGO_MAX_POS_Y 124 #define LOGO_MAX_POS_Y 124
@@ -38,25 +40,50 @@ Program to create a car monitor display using:
#define LOGO_TEXT_WIDTH 16 #define LOGO_TEXT_WIDTH 16
#define RAM_AMOUNT_KB 96
#define DEV_CPU_RAM_TEXT_LENGTH 3
#define DEV_DISPLAY_TEXT_LENGTH 7
enum DeviceLabel
{
DEV_CPU = 0,
DEV_RAM = 1,
DEV_DISPLAY = 2,
DEV_DONE = 3
};
/* extern fonts */
extern uint8_t BigFont[]; extern uint8_t BigFont[];
extern uint8_t SmallFont[]; extern uint8_t SmallFont[];
extern uint8_t OCR_A_Extended_M[]; extern uint8_t OCR_A_Extended_M[];
/* display strings */
char init_text[] PROGMEM = "Initialising...";
char device_labels[3][10] PROGMEM = {"CPU ", "RAM ", "DISPLAY"};
char cpu_text[] PROGMEM = "ATSAM3X8E";
char display_size_text[] PROGMEM = "480x320";
/* initialising... variables */
char should_clear = 1; char should_clear = 1;
char led_state = LOW; char led_state = LOW;
char init_flag = 0; char init_flag = 0;
char init_text_i = -1; char init_text_i = -1;
int init_text_x = 100; int init_text_x = 100;
char init_text[] PROGMEM = "Initialising...";
char text_temp[2] = {'a', '\0'}; char text_temp[2] = {'a', '\0'};
int init_percent = 0; int init_percent = 0;
/* logo variables */
char logo_text[] PROGMEM = "DS3 OBD2 display"; char logo_text[] PROGMEM = "DS3 OBD2 display";
int logo_pos_y = 0; int logo_pos_y = 0;
int last_logo_pos_y = 0; int last_logo_pos_y = 0;
int logo_pos_i = 0; int logo_pos_i = 0;
char logo_flag = 0; char logo_flag = 0;
/* device info variables */
char init_device_info = 0; /* wether we are drawing CPU (0), RAM (1) or DISPLAY (2) */
char ram_kb_amount = 0; /* counts up to 96 to show the KB of ram */
char device_label_i = -1; /* counter in the current device label */
void on_init_enter(); void on_init_enter();
void on_init_run(); void on_init_run();
void on_init_exit(); void on_init_exit();
@@ -92,80 +119,94 @@ UTFT display(ILI9486, 38, 39, 40, 41);
void on_init_enter() void on_init_enter()
{ {
display.clrScr(); display.clrScr();
Timer0.attachInterrupt(update_init_text); Timer0.attachInterrupt(update_init_text);
Timer0.start(MS(200)); Timer0.start(MS(200));
// TODO change to update when initializing bluetooth.. Timer2.attachInterrupt(update_device_info);
Timer1.attachInterrupt(update_percent_test); Timer2.start(MS(100));
Timer1.start(MS(60));
logo_flag |= (1 << FLAG_LOGO_UPDATE); // TODO change to update when initializing bluetooth..
Timer1.attachInterrupt(update_percent_test);
Timer1.start(MS(60));
Serial.println("Entering init state!"); logo_flag |= (1 << FLAG_LOGO_UPDATE);
Serial.println("Entering init state!");
} }
void on_init_run() void on_init_run()
{ {
if (init_flag & (1 << FLAG_INIT_CLEAR_POS)) if (init_flag & (1 << FLAG_INIT_CLEAR_POS))
{
init_flag &= ~(1 << FLAG_INIT_CLEAR_POS);
display.setColor(VGA_BLACK);
/*clear only initializing text*/
display.print(" ",(display.getDisplayXSize() / 2) - (INIT_TEXT_WIDTH * display.getFontXsize() / 2) - (INIT_PERCENTAGE_WIDTH/2),display.getDisplayYSize() / 2 + 50);
}
if (init_flag & (1 << FLAG_INIT_UPDATE_TEXT_POS))
{
init_flag &= ~(1 << FLAG_INIT_UPDATE_TEXT_POS);
display.setColor(VGA_AQUA);
int x_position = (display.getDisplayXSize() / 2 - (INIT_TEXT_WIDTH * display.getFontXsize() / 2)) + (init_text_i * display.getFontXsize()) - (INIT_PERCENTAGE_WIDTH/2);
// Serial.println(x_position);
text_temp[0] = init_text[init_text_i];
display.print(text_temp, x_position, display.getDisplayYSize() / 2 + 50); // print as string with one character
}
if (init_flag & (1 << FLAG_INIT_UPDATE_PERCENT_POS))
{
init_flag &= ~(1 << FLAG_INIT_UPDATE_PERCENT_POS);
int percent_x_pos = (display.getDisplayXSize() / 2 - (INIT_TEXT_WIDTH * display.getFontXsize() / 2)) + (INIT_TEXT_WIDTH * display.getFontXsize()) - (INIT_PERCENTAGE_WIDTH/2);
display.setColor(VGA_BLACK);
/*clear text region for percent*/
display.print(" ", percent_x_pos, display.getDisplayYSize() / 2 + 50);
display.setColor(VGA_FUCHSIA);
char *percent_text = (char *)malloc((INIT_PERCENTAGE_WIDTH-1) * sizeof(char));
sprintf(percent_text, "%d", init_percent);
display.print(percent_text, percent_x_pos, display.getDisplayYSize() / 2 + 50);
display.print("%", percent_x_pos + (INIT_PERCENTAGE_WIDTH-1) * display.getFontXsize(), display.getDisplayYSize() / 2 + 50);
free(percent_text);
}
logo_pos_i+= 3;
last_logo_pos_y = logo_pos_y;
logo_pos_y = logo_step(logo_pos_i);
if (logo_flag & (1 << FLAG_LOGO_UPDATE))
{
if (logo_pos_y >= LOGO_MAX_POS_Y)
{ {
logo_flag &= ~(1 << FLAG_LOGO_UPDATE); init_flag &= ~(1 << FLAG_INIT_CLEAR_POS);
display.setColor(VGA_BLACK);
/*clear only initializing text*/
display.print(" ", (display.getDisplayXSize() / 2) - (INIT_TEXT_WIDTH * display.getFontXsize() / 2) - (INIT_PERCENTAGE_WIDTH / 2), display.getDisplayYSize() / 2 + 50);
} }
if (logo_pos_i > LOGO_MIN_STEP) if (init_flag & (1 << FLAG_INIT_UPDATE_TEXT_POS))
{ {
display.setColor(VGA_BLACK); init_flag &= ~(1 << FLAG_INIT_UPDATE_TEXT_POS);
display.print(logo_text, CENTER, last_logo_pos_y); display.setColor(VGA_AQUA);
display.setColor(VGA_AQUA); int x_position = (display.getDisplayXSize() / 2 - (INIT_TEXT_WIDTH * display.getFontXsize() / 2)) + (init_text_i * display.getFontXsize()) - (INIT_PERCENTAGE_WIDTH / 2);
display.print(logo_text, CENTER, logo_pos_y); // Serial.println(x_position);
text_temp[0] = init_text[init_text_i];
display.print(text_temp, x_position, display.getDisplayYSize() / 2 + 50); // print as string with one character
}
if (init_flag & (1 << FLAG_INIT_UPDATE_PERCENT_POS))
{
init_flag &= ~(1 << FLAG_INIT_UPDATE_PERCENT_POS);
int percent_x_pos = (display.getDisplayXSize() / 2 - (INIT_TEXT_WIDTH * display.getFontXsize() / 2)) + (INIT_TEXT_WIDTH * display.getFontXsize()) - (INIT_PERCENTAGE_WIDTH / 2);
display.setColor(VGA_BLACK);
/*clear text region for percent*/
display.print(" ", percent_x_pos, display.getDisplayYSize() / 2 + 50);
display.setColor(VGA_FUCHSIA);
char *percent_text = (char *)malloc((INIT_PERCENTAGE_WIDTH - 1) * sizeof(char));
sprintf(percent_text, "%d", init_percent);
display.print(percent_text, percent_x_pos, display.getDisplayYSize() / 2 + 50);
display.print("%", percent_x_pos + (INIT_PERCENTAGE_WIDTH - 1) * display.getFontXsize(), display.getDisplayYSize() / 2 + 50);
free(percent_text);
} }
} if (init_flag & (1 << FLAG_DEVICE_LABEL_UPDATE_POS))
{
init_flag &= ~(1 << FLAG_DEVICE_LABEL_UPDATE_POS);
text_temp[0] = device_labels[init_device_info][device_label_i];
display.setColor(VGA_AQUA);
int x_position = 10 + (device_label_i * display.getFontXsize());
display.print(text_temp, x_position, 10 + (init_device_info * display.getFontYsize() + 3));
}
logo_pos_i += 3;
last_logo_pos_y = logo_pos_y;
logo_pos_y = logo_step(logo_pos_i);
if (logo_flag & (1 << FLAG_LOGO_UPDATE))
{
if (logo_pos_y >= LOGO_MAX_POS_Y)
{
logo_flag &= ~(1 << FLAG_LOGO_UPDATE);
}
if (logo_pos_i > LOGO_MIN_STEP)
{
display.setColor(VGA_BLACK);
display.print(logo_text, CENTER, last_logo_pos_y);
display.setColor(VGA_AQUA);
display.print(logo_text, CENTER, logo_pos_y);
}
}
} }
void on_init_exit() void on_init_exit()
{ {
Serial.println("Exiting init state!"); Serial.println("Exiting init state!");
display.clrScr(); display.clrScr();
Timer0.stop(); Timer0.detachInterrupt();
Timer0.stop();
Timer2.detachInterrupt();
Timer2.stop();
} }
/****************************/ /****************************/
@@ -174,85 +215,119 @@ void on_init_exit()
void on_main_enter() void on_main_enter()
{ {
Serial.println("Entering main loop"); Serial.println("Entering main loop");
} }
void on_main_run() void on_main_run()
{ {
delay(500); delay(500);
Serial.println("main!"); Serial.println("main!");
} }
void update_init_text() void update_init_text()
{ {
init_text_i++; init_text_i++;
if (init_text_i == 15) if (init_text_i == 15)
{ {
init_text_i = 0; init_text_i = 0;
init_flag |= (1 << FLAG_INIT_CLEAR_POS); init_flag |= (1 << FLAG_INIT_CLEAR_POS);
} }
init_flag |= (1 << FLAG_INIT_UPDATE_TEXT_POS); init_flag |= (1 << FLAG_INIT_UPDATE_TEXT_POS);
} }
void update_percent_test() void update_percent_test()
{ {
init_percent++; init_percent++;
if (init_percent > 100) if (init_percent > 100)
{ {
init_percent = 0; init_percent = 0;
} }
init_flag |= (1 << FLAG_INIT_UPDATE_PERCENT_POS); init_flag |= (1 << FLAG_INIT_UPDATE_PERCENT_POS);
}
void update_device_info()
{
switch (init_device_info)
{
case DEV_CPU:
device_label_i++;
if (device_label_i > DEV_CPU_RAM_TEXT_LENGTH)
{
init_device_info++;
device_label_i = 0;
}
break;
case DEV_RAM:
device_label_i++;
if (device_label_i > DEV_CPU_RAM_TEXT_LENGTH)
{
init_device_info++;
device_label_i = 0;
}
break;
case DEV_DISPLAY:
device_label_i++;
if (device_label_i > DEV_DISPLAY_TEXT_LENGTH)
{
device_label_i = 0;
}
break;
default:
break;
}
init_flag |= (1 << FLAG_DEVICE_LABEL_UPDATE_POS);
} }
void setup() void setup()
{ {
/* TODO change for TRNG (section 42 of datasheet)*/ /* TODO change for TRNG (section 42 of datasheet)*/
randomSeed(analogRead(0)); randomSeed(analogRead(0));
#if (DEBUG == 1) #if (DEBUG == 1)
Serial.begin(9600); Serial.begin(9600);
#endif #endif
/* Serial1 is bluetooth module, pin 18 and 19. */ /* Serial1 is bluetooth module, pin 18 and 19. */
Serial1.begin(115200); Serial1.begin(115200);
/* Init display */ /* Init display */
display.InitLCD(); display.InitLCD();
display.setFont(OCR_A_Extended_M); display.setFont(OCR_A_Extended_M);
#if (DEBUG == 1) #if (DEBUG == 1)
Serial.println("Starting"); Serial.println("Starting");
#endif #endif
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
statemachine_register_state(&init_state, STATE_INIT); statemachine_register_state(&init_state, STATE_INIT);
statemachine_register_state(&main_state, STATE_CAR_INFO); statemachine_register_state(&main_state, STATE_CAR_INFO);
statemachine_init(); statemachine_init();
} }
void loop() void loop()
{ {
statemachine_loop(); statemachine_loop();
// Serial.println("Running"); // Serial.println("Running");
// delay(10); // delay(10);
// while (Serial1.available()) // while (Serial1.available())
// { // {
// char rec = Serial1.read(); // char rec = Serial1.read();
// #if (DEBUG == 1) // #if (DEBUG == 1)
// Serial.println(rec); // Serial.println(rec);
// #endif // #endif
// } // }
// if (should_clear) // if (should_clear)
// { // {
// display.clrScr(); // display.clrScr();
// display.setColor(255, 0, 166); // display.setColor(255, 0, 166);
// display.fillRect(LCD_W / 2 - 200, LCD_H / 2 - 100, LCD_W / 2 + 200, LCD_H / 2 + 100); // display.fillRect(LCD_W / 2 - 200, LCD_H / 2 - 100, LCD_W / 2 + 200, LCD_H / 2 + 100);
// display.setColor(0, 247, 255); // display.setColor(0, 247, 255);
// display.print("OBD2 display yeet", CENTER, LCD_H / 2); // display.print("OBD2 display yeet", CENTER, LCD_H / 2);
// delay(10); // delay(10);
// should_clear = 0; // should_clear = 0;
// } // }
} }