code splitting & refactors; more efficient font drawing

This commit is contained in:
2020-01-05 13:29:06 +01:00
parent 1a2cc21fbb
commit 6db58e14a7
20 changed files with 869 additions and 761 deletions
+100
View File
@@ -0,0 +1,100 @@
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/timers.h>
#include "gui.h"
#include "nokia.h"
#include "liquid.h"
#include "drawing.h"
static void gui_thread(void *arg);
static void gui_tick(TimerHandle_t xTimer);
TaskHandle_t hGuiThread = NULL;
static TimerHandle_t hTicker = NULL;
void gui_init() {
printf("GUI init\n");
LCD_setup();
LCD_setContrast(48);
int rv = xTaskCreate(gui_thread, "gui", 4096, NULL, 6, &hGuiThread);
assert (rv == pdPASS);
hTicker = xTimerCreate(
"gui_tick", /* name */
pdMS_TO_TICKS(10), /* period/time */
pdTRUE, /* auto reload */
(void*)0, /* timer ID */
gui_tick); /* callback */
assert(hTicker != NULL);
xTimerStart(hTicker, portMAX_DELAY);
}
static void gui_tick(TimerHandle_t xTimer) {
xTaskNotify(hGuiThread, 0b10000, eSetBits);
}
/**
* Notification API:
*
* 0b1 - knob CW
* 0b10 - knowb CCW
* 0b100 - button released
* 0b1000 - button pressed
* 0b10000 - ticker event
*
* @param arg
*/
static void __attribute__((noreturn)) gui_thread(void *arg) {
struct Liquid *liquid = Liquid_start();
uint32_t last_wheel_time = 0;
while (1) {
uint32_t value = 0;
xTaskNotifyWait(0, ULONG_MAX, &value, pdMS_TO_TICKS(10));
bool want_repaint = false;
if (value & 0b10000) {
// TICK
want_repaint |= Liquid_handleTick(liquid);
}
if (value & 0b1000) {
want_repaint |= Liquid_handleInput(liquid, InputEvent_Button(1));
} else if (value & 0b100) {
want_repaint |= Liquid_handleInput(liquid, InputEvent_Button(0));
}
if (value & 0b11) {
uint32_t time = xTaskGetTickCount();
uint32_t increment = 1;
if (last_wheel_time != 0) {
uint32_t ela = time - last_wheel_time;
if (ela < pdMS_TO_TICKS(20)) {
increment = 25;
} else if (ela < pdMS_TO_TICKS(35)) {
increment = 10;
} else if (ela < pdMS_TO_TICKS(75)) {
increment = 5;
}
}
last_wheel_time = time;
if (value & 0b01) {
want_repaint |= Liquid_handleInput(liquid, InputEvent_Wheel(increment));
}
if (value & 0b10) {
want_repaint |= Liquid_handleInput(liquid, InputEvent_Wheel(-increment));
}
}
if (want_repaint) {
Liquid_paint(liquid);
}
}
}
+10
View File
@@ -0,0 +1,10 @@
#ifndef GUI_H
#define GUI_H
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
extern TaskHandle_t hGuiThread;
void gui_init();
#endif //GUI_H
+51
View File
@@ -0,0 +1,51 @@
/**
* Input event structural enum
*
* Created on 2020/01/05.
*/
#ifndef LIQUID_INPUT_EVENT_H
#define LIQUID_INPUT_EVENT_H
enum InputEvent_Kind {
InputEventKind_Wheel,
InputEventKind_Button,
};
struct InputEvent {
enum InputEvent_Kind kind;
union {
struct {
// Wheel delta
int32_t delta;
} wheel;
struct {
// Button state
bool state;
} button;
};
};
static inline struct InputEvent InputEvent_Wheel(int32_t delta)
{
return (struct InputEvent) {
.kind = InputEventKind_Wheel,
.wheel = {.delta = delta}
};
}
/**
* Button event (push, release)
*
* @param state - pushed
* @return event
*/
static inline struct InputEvent InputEvent_Button(bool state)
{
return (struct InputEvent) {
.kind = InputEventKind_Button,
.button = {.state = state},
};
}
#endif //LIQUID_INPUT_EVENT_H
+6 -4
View File
@@ -1,13 +1,15 @@
#include "liquid.h"
#include "rom/queue.h"
#include "scenes.h"
#include <malloc.h>
#include <assert.h>
#include <esp_log.h>
#include <nokia.h>
#include <rom/queue.h>
#include "liquid.h"
#include "nokia.h"
static const char *TAG = "Liquid";
extern struct Scene *NewScene_Root(void);
struct RunningScene {
struct Scene *scene;
SLIST_ENTRY(RunningScene) next;
+6 -223
View File
@@ -4,234 +4,17 @@
* Created on 2020/01/03.
*/
#ifndef REFLOWER_LIQUID_H
#define REFLOWER_LIQUID_H
#ifndef LIQUID_H
#define LIQUID_H
#include <stdint.h>
#include <stdbool.h>
#include "input_event.h"
#include "scene_event.h"
#include "scene_type.h"
struct Liquid;
enum InputEvent_Kind {
InputEventKind_Wheel,
InputEventKind_Button,
};
struct InputEvent {
enum InputEvent_Kind kind;
union {
struct {
// Wheel delta
int32_t delta;
} wheel;
struct {
// Button state
bool state;
} button;
};
};
static inline struct InputEvent InputEvent_Wheel(int32_t delta)
{
return (struct InputEvent) {
.kind = InputEventKind_Wheel,
.wheel = {.delta = delta}
};
}
/**
* Button event (push, release)
*
* @param state - pushed
* @return event
*/
static inline struct InputEvent InputEvent_Button(bool state)
{
return (struct InputEvent) {
.kind = InputEventKind_Button,
.button = {.state = state},
};
}
enum SceneEvent_Kind {
SceneEventKind_Close,
SceneEventKind_OpenChild,
SceneEventKind_RequestRepaint,
SceneEventKind_None,
};
// forward declaration
struct Scene;
/**
* Scene event, returned from some scene methods.
*
* Use the constructor functions to create this struct.
*/
struct SceneEvent {
/** Event kind enum */
enum SceneEvent_Kind kind;
union {
/* data for Close event kind */
struct {
// Status code
int32_t status;
// Return data on heap
void *data;
} close;
/* Data for Open event kind */
struct {
// Scene (initialized, with options loaded in through the constructor)
struct Scene *scene;
// Tag used by parent to identify the open child
uint32_t tag;
} open;
};
};
/**
* Create empty (null object) scene event.
*
* @return event
*/
static inline struct SceneEvent SceneEvent_None(void)
{
return (struct SceneEvent) {
.kind = SceneEventKind_None,
};
}
/**
* Request scene repaint
*
* @return event
*/
static inline struct SceneEvent SceneEvent_Repaint(void)
{
return (struct SceneEvent) {
.kind = SceneEventKind_RequestRepaint
};
}
/**
* Request a sub-scene to be opened
*
* @param child - child scene
* @param tag - scene tag
* @return event
*/
static inline struct SceneEvent SceneEvent_OpenChild(struct Scene *child, uint32_t tag) {
return (struct SceneEvent) {
.kind = SceneEventKind_OpenChild,
.open = { .scene = child, .tag=tag },
};
}
/**
* Close this scene, returning to parent.
*
* @param status - status number for the parent
* @param data - heap-allocated data for parent, can be NULL; e.g. user input as string.
* @return event
*/
static inline struct SceneEvent SceneEvent_Close(int32_t status, void *data)
{
return (struct SceneEvent) {
.kind = SceneEventKind_Close,
.close = {.status = status, .data=data},
};
}
/**
* Scene::onInput fp type - handle user input
*
* @param scene - self
* @param event - the input event
* @return follow-up scene event, can be SceneEvent_None() if not used.
*/
typedef struct SceneEvent (*Scene_onInput_t)(struct Scene *scene, struct InputEvent event);
/**
* Scene::onChildReturn fp type
*
* @param scene - self
* @param tag - child's tag
* @param status - status code returned from the child
* @param data - data returned from the child, must be heap-allocated if not NULL.
* @return follow-up scene event, can be SceneEvent_None() if not used.
*/
typedef struct SceneEvent (*Scene_onChildReturn_t)(struct Scene *scene, uint32_t tag, int32_t status, void *data);
/**
* Scene::onTick fp type
*
* @param scene - self
* @return follow-up scene event, can be SceneEvent_None() if not used.
*/
typedef struct SceneEvent (*Scene_onTick_t)(struct Scene *scene);
/**
* Scene::paint fp type
*
* @param scene - self
*/
typedef void (*Scene_paint_t)(struct Scene *scene);
/**
* Scene::free fp type.
* Release internally allocated resources.
* This is called by the GUI engine when the scene is closed.
*
* @param scene - self
*/
typedef void (*Scene_free_t)(struct Scene *scene);
/**
* Scene instance in the framework
*/
struct Scene {
/**
* Tag given to the scene by its parent to identify its close event.
*/
uint32_t tag;
/**
* Handle input event.
* Can return a follow-up scene event, e.g. repaint request.
* Nullable field.
*/
Scene_onInput_t onInput;
/**
* Child scene closed, handle its return value and data, if any.
* Can return a follow-up scene event.
* In any case, the scene will be re-painted, if it stays open and on top.
* Nullable field.
*/
Scene_onChildReturn_t onChildReturn;
/**
* Handle a periodic tick (10ms).
* Can return a follow-up scene event, e.g. repaint request.
* Nullable field.
*/
Scene_onTick_t onTick;
/**
* Draw the scene to the LCD buffer.
* DO NOT write to the display yet, it will be done by the GUI engine.
*
* MANDATORY FIELD
*/
Scene_paint_t paint;
/**
* Release internally allocated resources, if any. Called on close.
* Nullable field.
*/
Scene_free_t free;
};
/** return 1 if repaint requested */
bool Liquid_handleInput(struct Liquid *container, struct InputEvent event);
@@ -244,4 +27,4 @@ void Liquid_paint(struct Liquid *container);
/** Initialize the GUI system with a root scene */
struct Liquid *Liquid_start(void);
#endif //REFLOWER_LIQUID_H
#endif //LIQUID_H
-43
View File
@@ -1,43 +0,0 @@
#include "scenes.h"
#include "liquid.h"
#include "../graphics/nokia.h"
#include <malloc.h>
struct CarScene {
struct Scene base;
int32_t pos;
};
static struct SceneEvent Car_onInput(struct CarScene *self, struct InputEvent event) {
switch (event.kind) {
case InputEventKind_Wheel:
self->pos += event.wheel.delta;
if (self->pos < 0) self->pos = 0;
if (self->pos > LCD_WIDTH-21) self->pos = LCD_WIDTH-21;
return SceneEvent_Repaint();
case InputEventKind_Button:
if (event.button.state) {
return SceneEvent_Close(0, NULL);
}
// fall through
default:
return SceneEvent_None();
}
}
static void Car_paint(struct CarScene *self)
{
LCD_clearDisplay(0);
LCD_setRect(self->pos, LCD_HEIGHT/2-10, self->pos+20,LCD_HEIGHT/2+10,0,1);
LCD_updateDisplay();
}
struct Scene *NewScene_Car(void) {
struct CarScene *scene = calloc(1, sizeof(struct CarScene));
if (!scene) return NULL;
scene->base.onInput = (Scene_onInput_t) Car_onInput;
scene->base.paint = (Scene_paint_t) Car_paint;
return (struct Scene *) scene;
}
+99
View File
@@ -0,0 +1,99 @@
/**
* Scene event structural enum
*
* Created on 2020/01/05.
*/
#ifndef LIQUID_SCENE_EVENT_H
#define LIQUID_SCENE_EVENT_H
// forward declaration
struct Scene;
enum SceneEvent_Kind {
SceneEventKind_Close,
SceneEventKind_OpenChild,
SceneEventKind_RequestRepaint,
SceneEventKind_None,
};
/**
* Scene event, returned from some scene methods.
*
* Use the constructor functions to create this struct.
*/
struct SceneEvent {
/** Event kind enum */
enum SceneEvent_Kind kind;
union {
/* data for Close event kind */
struct {
// Status code
int32_t status;
// Return data on heap
void *data;
} close;
/* Data for Open event kind */
struct {
// Scene (initialized, with options loaded in through the constructor)
struct Scene *scene;
// Tag used by parent to identify the open child
uint32_t tag;
} open;
};
};
/**
* Create empty (null object) scene event.
*
* @return event
*/
static inline struct SceneEvent SceneEvent_None(void)
{
return (struct SceneEvent) {
.kind = SceneEventKind_None,
};
}
/**
* Request scene repaint
*
* @return event
*/
static inline struct SceneEvent SceneEvent_Repaint(void)
{
return (struct SceneEvent) {
.kind = SceneEventKind_RequestRepaint
};
}
/**
* Request a sub-scene to be opened
*
* @param child - child scene
* @param tag - scene tag
* @return event
*/
static inline struct SceneEvent SceneEvent_OpenChild(struct Scene *child, uint32_t tag) {
return (struct SceneEvent) {
.kind = SceneEventKind_OpenChild,
.open = { .scene = child, .tag=tag },
};
}
/**
* Close this scene, returning to parent.
*
* @param status - status number for the parent
* @param data - heap-allocated data for parent, can be NULL; e.g. user input as string.
* @return event
*/
static inline struct SceneEvent SceneEvent_Close(int32_t status, void *data)
{
return (struct SceneEvent) {
.kind = SceneEventKind_Close,
.close = {.status = status, .data=data},
};
}
#endif //LIQUID_SCENE_EVENT_H
-88
View File
@@ -1,88 +0,0 @@
#include "scenes.h"
#include "liquid.h"
#include "../graphics/nokia.h"
#include "../analog.h"
#include <malloc.h>
#include <stdio.h>
/**
* The struct is allocated bigger than 'Scene' to accommodate private fields.
* Since the base struct is located at the beginning, it can be cast and passed around as Scene.
*/
struct RootScene {
struct Scene base;
int32_t pos;
uint32_t timer_phase;
uint32_t timer_prescale;
};
static struct SceneEvent Root_onInput(struct RootScene *self, struct InputEvent event) {
switch (event.kind) {
case InputEventKind_Wheel:
self->pos += event.wheel.delta;
break;
case InputEventKind_Button:
if (event.button.state) {
return SceneEvent_OpenChild(NewScene_Car(), 0);
}
break;
}
return SceneEvent_Repaint();
}
static struct SceneEvent Root_onTick(struct RootScene *self) {
self->timer_prescale += 1;
if (self->timer_prescale == 100) {
self->timer_prescale = 0;
self->timer_phase += 1;
self->timer_phase = self->timer_phase & 0b11; // 0..3
return SceneEvent_Repaint();
}
return SceneEvent_None();
}
static void Root_paint(struct RootScene *self)
{
LCD_clearDisplay(0);
const char *header = "???";
switch (self->timer_phase) {
case 0:
header = "Drink water";
break;
case 1:
header = " Drink water";
break;
case 2:
header = " Drink water";
break;
case 3:
header = " Drink water";
break;
}
LCD_setStrEx(header, 0, 3, 1, 3);
LCD_setRect(0, 12, 83, 31, 1, 1);
char buf[10];
// sprintf(buf, "%3d", priv->pos);
// LCD_setStr(buf, 2, 15, 0);
sprintf(buf, "🌡%.0f°C", analog_read());
LCD_setStrEx(buf, 2, 14, 0, 2);
LCD_setStr("×↑↓←→⏰⌛☸⏎🌡°🔙▶◀", 2, 33, 1);
LCD_updateDisplay();
}
struct Scene *NewScene_Root(void) {
struct RootScene *scene = calloc(1, sizeof(struct RootScene));
scene->base.onInput = (Scene_onInput_t) Root_onInput;
scene->base.paint = (Scene_paint_t) Root_paint;
scene->base.onTick = (Scene_onTick_t) Root_onTick;
return (struct Scene *) scene;
}
+105
View File
@@ -0,0 +1,105 @@
/**
* Scene struct
*
* Created on 2020/01/05.
*/
#ifndef LIQUID_SCENE_TYPE_H
#define LIQUID_SCENE_TYPE_H
#include <stdint.h>
struct Scene;
struct InputEvent;
/**
* Scene::onInput fp type - handle user input
*
* @param scene - self
* @param event - the input event
* @return follow-up scene event, can be SceneEvent_None() if not used.
*/
typedef struct SceneEvent (*Scene_onInput_t)(struct Scene *scene, struct InputEvent event);
/**
* Scene::onChildReturn fp type
*
* @param scene - self
* @param tag - child's tag
* @param status - status code returned from the child
* @param data - data returned from the child, must be heap-allocated if not NULL.
* @return follow-up scene event, can be SceneEvent_None() if not used.
*/
typedef struct SceneEvent (*Scene_onChildReturn_t)(struct Scene *scene, uint32_t tag, int32_t status, void *data);
/**
* Scene::onTick fp type
*
* @param scene - self
* @return follow-up scene event, can be SceneEvent_None() if not used.
*/
typedef struct SceneEvent (*Scene_onTick_t)(struct Scene *scene);
/**
* Scene::paint fp type
*
* @param scene - self
*/
typedef void (*Scene_paint_t)(struct Scene *scene);
/**
* Scene::free fp type.
* Release internally allocated resources.
* This is called by the GUI engine when the scene is closed.
*
* @param scene - self
*/
typedef void (*Scene_free_t)(struct Scene *scene);
/**
* Scene instance in the framework
*/
struct Scene {
/**
* Tag given to the scene by its parent to identify its close event.
*/
uint32_t tag;
/**
* Handle input event.
* Can return a follow-up scene event, e.g. repaint request.
* Nullable field.
*/
Scene_onInput_t onInput;
/**
* Child scene closed, handle its return value and data, if any.
* Can return a follow-up scene event.
* In any case, the scene will be re-painted, if it stays open and on top.
* Nullable field.
*/
Scene_onChildReturn_t onChildReturn;
/**
* Handle a periodic tick (10ms).
* Can return a follow-up scene event, e.g. repaint request.
* Nullable field.
*/
Scene_onTick_t onTick;
/**
* Draw the scene to the LCD buffer.
* DO NOT write to the display yet, it will be done by the GUI engine.
*
* MANDATORY FIELD
*/
Scene_paint_t paint;
/**
* Release internally allocated resources, if any. Called on close.
* Nullable field.
*/
Scene_free_t free;
};
#endif //LIQUID_SCENE_TYPE_H
-13
View File
@@ -1,13 +0,0 @@
/**
* TODO file description
*
* Created on 2020/01/03.
*/
#ifndef REFLOWER_SCENES_H
#define REFLOWER_SCENES_H
struct Scene *NewScene_Root(void);
struct Scene *NewScene_Car(void);
#endif //REFLOWER_SCENES_H