code splitting & refactors; more efficient font drawing
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#include "scenes.h"
|
||||
#include "liquid.h"
|
||||
#include <malloc.h>
|
||||
#include "graphics/nokia.h"
|
||||
#include "graphics/drawing.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;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "scenes.h"
|
||||
#include "liquid.h"
|
||||
#include "analog.h"
|
||||
#include "graphics/nokia.h"
|
||||
#include "graphics/drawing.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;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* 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
|
||||
Reference in New Issue
Block a user