parent
17a228faa7
commit
202b9e49f7
@ -1,45 +0,0 @@ |
|||||||
#include <malloc.h> |
|
||||||
#include <inttypes.h> |
|
||||||
#include "graphics/nokia.h" |
|
||||||
#include "graphics/drawing.h" |
|
||||||
#include "scenes.h" |
|
||||||
#include "liquid.h" |
|
||||||
|
|
||||||
struct CarScene { |
|
||||||
struct Scene base; |
|
||||||
int32_t pos; |
|
||||||
}; |
|
||||||
|
|
||||||
static struct SceneEvent 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 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) onInput; |
|
||||||
scene->base.paint = (Scene_paint_t) paint; |
|
||||||
return (struct Scene *) scene; |
|
||||||
} |
|
@ -1,93 +0,0 @@ |
|||||||
#include <malloc.h> |
|
||||||
#include <stdio.h> |
|
||||||
#include <graphics/bitmaps.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 DemoScene { |
|
||||||
struct Scene base; |
|
||||||
int32_t pos; |
|
||||||
uint32_t timer_phase; |
|
||||||
uint32_t timer_prescale; |
|
||||||
}; |
|
||||||
|
|
||||||
static struct SceneEvent onInput(struct DemoScene *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 onTick(struct DemoScene *self, uint32_t millis) { |
|
||||||
// top text anim
|
|
||||||
self->timer_prescale += millis; |
|
||||||
|
|
||||||
if (self->timer_prescale == 1000) { |
|
||||||
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 paint(struct DemoScene *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, 3, 1, BLACK, (struct TextStyle) {.size = FONT_BOLD}); |
|
||||||
|
|
||||||
LCD_setRect(0, 12, 83, 31, true, BLACK); |
|
||||||
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, WHITE, (struct TextStyle) {.size = FONT_DOUBLE}); |
|
||||||
|
|
||||||
LCD_setStr("×↑↓←→⏰⌛☸⏎🌡°🔙▶◀", 2, 33, BLACK); |
|
||||||
} |
|
||||||
|
|
||||||
struct Scene *NewScene_Demo(void) { |
|
||||||
struct DemoScene *scene = calloc(1, sizeof(struct DemoScene)); |
|
||||||
|
|
||||||
scene->base.onInput = (Scene_onInput_t) onInput; |
|
||||||
scene->base.paint = (Scene_paint_t) paint; |
|
||||||
scene->base.onTick = (Scene_onTick_t) onTick; |
|
||||||
scene->base.onInput = (Scene_onInput_t) onInput; |
|
||||||
return (struct Scene *) scene; |
|
||||||
} |
|
@ -0,0 +1,112 @@ |
|||||||
|
#include <malloc.h> |
||||||
|
#include <string.h> |
||||||
|
#include <math.h> |
||||||
|
#include <stdio.h> |
||||||
|
#include <analog.h> |
||||||
|
#include <firehazard.h> |
||||||
|
|
||||||
|
#include "liquid.h" |
||||||
|
#include "graphics/drawing.h" |
||||||
|
#include "graphics/display_spec.h" |
||||||
|
|
||||||
|
#include "scenes.h" |
||||||
|
#include "scene_menu.h" |
||||||
|
|
||||||
|
struct Priv { |
||||||
|
Scene_paint_t basePaint; |
||||||
|
}; |
||||||
|
|
||||||
|
static struct SceneEvent onSelect(struct MenuScene *self) { |
||||||
|
if (self->selected == 0) { |
||||||
|
struct NumberSceneOpts *opts = calloc(1, sizeof(struct NumberSceneOpts)); |
||||||
|
strcpy(opts->unit, "°C"); |
||||||
|
strcpy(opts->label, "Setpoint"); |
||||||
|
opts->value = (int) roundf(fire_get_setpoint(false)); |
||||||
|
opts->min = 0; |
||||||
|
opts->max = 350; |
||||||
|
return SceneEvent_OpenChild(NewScene_Number(opts), 0); |
||||||
|
} |
||||||
|
|
||||||
|
if (self->selected == 1) { |
||||||
|
fire_enable(!fire_enabled()); |
||||||
|
return SceneEvent_Repaint(); |
||||||
|
} |
||||||
|
|
||||||
|
return SceneEvent_None(); |
||||||
|
} |
||||||
|
|
||||||
|
static void priv_deinit(struct MenuScene *self) |
||||||
|
{ |
||||||
|
free(self->extra); |
||||||
|
self->extra = NULL; |
||||||
|
} |
||||||
|
|
||||||
|
static struct SceneEvent onChildReturn(struct MenuScene *self, uint32_t tag, int32_t status, void *data) |
||||||
|
{ |
||||||
|
if (tag == 0) { |
||||||
|
fire_setlevel((float) status); |
||||||
|
} |
||||||
|
|
||||||
|
return SceneEvent_Repaint(); |
||||||
|
} |
||||||
|
|
||||||
|
#define XLINE 38 |
||||||
|
|
||||||
|
void print_labels(struct MenuItem *items) { |
||||||
|
snprintf(items[0].label, MENUITEM_LABEL_LEN, "▶%.0f°C", fire_get_setpoint(false)); |
||||||
|
if (fire_enabled()) { |
||||||
|
strncpy(items[1].label, "▶STOP", MENUITEM_LABEL_LEN); |
||||||
|
} else { |
||||||
|
strncpy(items[1].label, "▶START", MENUITEM_LABEL_LEN); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void paint(struct MenuScene *self) |
||||||
|
{ |
||||||
|
struct Priv *priv = self->extra; |
||||||
|
|
||||||
|
print_labels(self->items); |
||||||
|
|
||||||
|
LCD_clearDisplay(0); |
||||||
|
|
||||||
|
LCD_setLine(XLINE-1, 0, XLINE-1, LCD_HEIGHT, BLACK); |
||||||
|
|
||||||
|
char buf[10]; |
||||||
|
sprintf(buf, "%.0f", analog_read()); |
||||||
|
// TODO centering
|
||||||
|
LCD_setStrEx(buf, 0, 0, BLACK, (struct TextStyle) {.size = FONT_DOUBLE}); |
||||||
|
LCD_setStrEx("°C", 0, 17, BLACK, (struct TextStyle) {.size = FONT_BOLD}); |
||||||
|
|
||||||
|
if (fire_enabled()) { |
||||||
|
strcpy(buf, "RUN"); |
||||||
|
} else { |
||||||
|
strcpy(buf, "Off"); |
||||||
|
} |
||||||
|
LCD_setStrEx(buf, 0, 30, BLACK, (struct TextStyle) {.size = FONT_BOLD}); |
||||||
|
|
||||||
|
priv->basePaint((struct Scene *) self); |
||||||
|
} |
||||||
|
|
||||||
|
struct Scene *NewScene_MenuTest() { |
||||||
|
struct MenuItem *items = calloc(2, sizeof(struct MenuItem)); |
||||||
|
|
||||||
|
print_labels(items); |
||||||
|
|
||||||
|
struct MenuScene * scene = NewScene_Menu(items, 2); |
||||||
|
scene->onSelect = onSelect; |
||||||
|
|
||||||
|
scene->x = XLINE; |
||||||
|
scene->ncols = 8; |
||||||
|
|
||||||
|
// private data added by the subclass
|
||||||
|
struct Priv *priv = calloc(1, sizeof(struct Priv)); |
||||||
|
priv->basePaint = scene->base.paint; |
||||||
|
scene->base.paint = (Scene_paint_t) paint; |
||||||
|
scene->extra = priv; |
||||||
|
scene->extra_deinit = (Scene_deinit_t) priv_deinit; |
||||||
|
|
||||||
|
// add a child return handler (base does not use this)
|
||||||
|
scene->base.onChildReturn = (Scene_onChildReturn_t) onChildReturn; |
||||||
|
|
||||||
|
return (struct Scene *) scene; |
||||||
|
} |
@ -1,93 +0,0 @@ |
|||||||
#include <malloc.h> |
|
||||||
#include <graphics/bitmaps.h> |
|
||||||
#include <sys/param.h> |
|
||||||
#include <string.h> |
|
||||||
#include <stdio.h> |
|
||||||
|
|
||||||
#include "liquid.h" |
|
||||||
#include "graphics/drawing.h" |
|
||||||
|
|
||||||
#include "scenes.h" |
|
||||||
#include "scene_menu.h" |
|
||||||
|
|
||||||
struct Priv { |
|
||||||
int32_t number; |
|
||||||
}; |
|
||||||
|
|
||||||
static struct SceneEvent onSelect(struct MenuScene *self) { |
|
||||||
struct Priv *priv = self->extra; |
|
||||||
|
|
||||||
if (self->selected == 0) { |
|
||||||
return SceneEvent_Close(0, NULL); |
|
||||||
} |
|
||||||
|
|
||||||
if (self->selected == 8) { |
|
||||||
return SceneEvent_OpenChild(NewScene_Car(), 0); |
|
||||||
} |
|
||||||
if (self->selected == 9) { |
|
||||||
return SceneEvent_OpenChild(NewScene_Demo(), 0); |
|
||||||
} |
|
||||||
if (self->selected == 2) { |
|
||||||
struct NumberSceneOpts *opts = calloc(1, sizeof(struct NumberSceneOpts)); |
|
||||||
strcpy(opts->unit, "°C"); |
|
||||||
strcpy(opts->label, "Setpoint"); |
|
||||||
opts->value = priv->number; |
|
||||||
opts->min = 0; |
|
||||||
opts->max = 350; |
|
||||||
return SceneEvent_OpenChild(NewScene_Number(opts), 99); |
|
||||||
} |
|
||||||
if (self->selected == 5) { |
|
||||||
self->items[5].tag++; |
|
||||||
snprintf(self->items[5].label, MENUITEM_LABEL_LEN, "Clicked %dx", self->items[5].tag); |
|
||||||
return SceneEvent_Repaint(); |
|
||||||
} |
|
||||||
|
|
||||||
return SceneEvent_None(); |
|
||||||
} |
|
||||||
|
|
||||||
static void priv_deinit(struct MenuScene *self) |
|
||||||
{ |
|
||||||
free(self->extra); |
|
||||||
self->extra = NULL; |
|
||||||
} |
|
||||||
|
|
||||||
static struct SceneEvent onChildReturn(struct MenuScene *self, uint32_t tag, int32_t status, void *data) |
|
||||||
{ |
|
||||||
struct Priv *priv = self->extra; |
|
||||||
if (tag == 99) { |
|
||||||
priv->number = status; |
|
||||||
snprintf(self->items[2].label, MENUITEM_LABEL_LEN, "Set=%d°C", priv->number); |
|
||||||
} |
|
||||||
|
|
||||||
// this should be unreachable
|
|
||||||
return SceneEvent_None(); |
|
||||||
} |
|
||||||
|
|
||||||
struct Scene *NewScene_MenuTest() { |
|
||||||
struct MenuItem *items = calloc(10, sizeof(struct MenuItem)); |
|
||||||
|
|
||||||
strncpy(items[0].label, "🔙Back", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[1].label, "▶#1", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[2].label, "Set=0°C", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[3].label, "▶#3", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[4].label, "▶#4", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[5].label, "Clicked 0x", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[6].label, "▶#6", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[7].label, "▶#7", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[8].label, "Car", MENUITEM_LABEL_LEN); |
|
||||||
strncpy(items[9].label, "Demo Scene", MENUITEM_LABEL_LEN); |
|
||||||
|
|
||||||
struct MenuScene * scene = NewScene_Menu(items, 10); |
|
||||||
scene->onSelect = onSelect; |
|
||||||
|
|
||||||
// private data added by the subclass
|
|
||||||
struct Priv *priv = calloc(1, sizeof(struct Priv)); |
|
||||||
priv->number = 0; |
|
||||||
scene->extra = priv; |
|
||||||
scene->extra_deinit = (Scene_deinit_t) priv_deinit; |
|
||||||
|
|
||||||
// add a child return handler (base does not use this)
|
|
||||||
scene->base.onChildReturn = (Scene_onChildReturn_t) onChildReturn; |
|
||||||
|
|
||||||
return (struct Scene *) scene; |
|
||||||
} |
|
Loading…
Reference in new issue