gadget-tester/ui.c
2019-11-22 11:12:54 +01:00

178 lines
3.9 KiB
C

/*
* (c) danielinux 2019
*
* GPLv.2
*
* See LICENSE for details
*/
#include <stdint.h>
#include "system.h"
#include "display.h"
#include "systick.h"
#include "button.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ui.h"
/* Uncomment for device initialization */
//#define DEVICE_INITIALIZATION
static const char welcome_message0[]= " Smells like ";
static const char welcome_message1[]= "fresh electrons";
static const char welcome_message2[]= " and coffee. ";
static uint32_t last_action = 0;
static uint32_t offset = 0;
#define ST_LOCKED 0
#define ST_PIN_ENTRY 1
#define ST_UNLOCKED 2
#define MAX_PASSWORD 32
static int fsm_state = ST_LOCKED;
static int sleeping = 0;
static char password[MAX_PASSWORD] = {};
static int pin_idx = 0;
#define SLEEP_TIME (5000)
static void ui_sleep(void)
{
display_scroll(NULL, 0);
display_clear(NULL);
sleeping = 1;
}
static int ui_wakeup(void)
{
last_action = jiffies;
if (sleeping) {
sleeping = 0;
return 1;
}
return 0;
}
void ui_msg(const char *txt)
{
ui_wakeup();
display_scroll(NULL, 0);
display_clear(NULL);
display_text(6, txt);
}
static uint8_t display_cur_v = 0;
static uint8_t menu_selection = 0;
static void ui_display_menu_refresh(void)
{
const struct display_menu *menu = CurrentMenu;
uint8_t vline;
int i;
display_scroll(NULL, display_cur_v);
display_clear(NULL);
for (i = menu->entry_n - 1; i >= 0; i--) {
vline = i + 5;
if (vline > 7)
vline -= 8;
if (i == menu_selection)
display_text_inverse(vline, menu->entry[i].title);
else
display_text(vline, menu->entry[i].title);
}
WFI();
}
void ui_display_menu(const struct display_menu *menu)
{
uint8_t i;
display_clear(NULL);
menu_selection = 0;
display_cur_v = 0;
CurrentMenu = (struct display_menu *)menu;
ui_display_menu_refresh();
}
void ui_button_press(uint8_t b, int hold)
{
if (b == '*') {
int n = menu_selection;
menu_selection = 0;
if (CurrentMenu->entry[n].action) {
display_cur_v = 0;
display_clear(NULL);
display_scroll(NULL, display_cur_v);
CurrentMenu->entry[n].action(
CurrentMenu->entry[n].arg);
}
return;
}
if (b == '-') {
if (menu_selection > 0) {
menu_selection--;
if ((display_cur_v / 8) - (menu_selection > 2)) {
int s;
s = display_cur_v + 8;
if (s > 0x3f)
s = 0;
display_cur_v = (uint8_t)s;
display_scroll(NULL, display_cur_v);
}
}
}
if (b == '+') {
if (menu_selection < CurrentMenu->entry_n - 1) {
menu_selection++;
if ((menu_selection - display_cur_v / 8) > 2) {
int s;
s = display_cur_v - 8;
if (s < 0)
s = 0x40 - 8;
display_cur_v = (uint8_t)s;
display_scroll(NULL, display_cur_v);
}
}
}
ui_display_menu_refresh();
}
void ui_init(void)
{
uint32_t now;
int i;
#if 0
display_scroll(NULL, 0x3f);
display_text(0, welcome_message0);
display_text(1, welcome_message1);
display_text(2, welcome_message2);
now = jiffies;
for (i = 0x3f; i >= 0x20; i--) {
display_scroll(NULL, i);
while ((jiffies - now) < 20)
WFI();
now = jiffies;
}
for (i = display_getcontrast(NULL); i >= 0; i--) {
display_setcontrast(NULL, i);
while ((jiffies - now) < 10)
WFI();
now = jiffies;
}
#endif
display_scroll(NULL, display_cur_v);
display_clear(NULL);
display_setcontrast(NULL, 0xcf);
ui_display_menu(CurrentMenu);
}
void ui_keepalive(uint32_t timeslice)
{
}