You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.5 KiB
101 lines
2.5 KiB
5 years ago
|
#include <freertos/FreeRTOS.h>
|
||
|
#include <freertos/task.h>
|
||
5 years ago
|
#include <freertos/timers.h>
|
||
5 years ago
|
|
||
5 years ago
|
#include "gui.h"
|
||
|
#include "nokia.h"
|
||
|
#include "liquid.h"
|
||
|
#include "drawing.h"
|
||
|
|
||
5 years ago
|
static void gui_thread(void *arg);
|
||
5 years ago
|
static void gui_tick(TimerHandle_t xTimer);
|
||
5 years ago
|
|
||
|
TaskHandle_t hGuiThread = NULL;
|
||
5 years ago
|
static TimerHandle_t hTicker = NULL;
|
||
5 years ago
|
|
||
|
void gui_init() {
|
||
|
printf("GUI init\n");
|
||
|
LCD_setup();
|
||
5 years ago
|
LCD_setContrast(48);
|
||
5 years ago
|
|
||
5 years ago
|
int rv = xTaskCreate(gui_thread, "gui", 4096, NULL, 6, &hGuiThread);
|
||
5 years ago
|
assert (rv == pdPASS);
|
||
5 years ago
|
|
||
|
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);
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* Notification API:
|
||
|
*
|
||
5 years ago
|
* 0b1 - knob CW
|
||
|
* 0b10 - knowb CCW
|
||
|
* 0b100 - button released
|
||
|
* 0b1000 - button pressed
|
||
|
* 0b10000 - ticker event
|
||
5 years ago
|
*
|
||
|
* @param arg
|
||
|
*/
|
||
5 years ago
|
static void __attribute__((noreturn)) gui_thread(void *arg) {
|
||
5 years ago
|
struct Liquid *liquid = Liquid_start();
|
||
5 years ago
|
|
||
|
uint32_t last_wheel_time = 0;
|
||
5 years ago
|
|
||
5 years ago
|
while (1) {
|
||
5 years ago
|
uint32_t value = 0;
|
||
5 years ago
|
xTaskNotifyWait(0, ULONG_MAX, &value, pdMS_TO_TICKS(10));
|
||
|
|
||
|
bool want_repaint = false;
|
||
5 years ago
|
|
||
5 years ago
|
if (value & 0b10000) {
|
||
|
// TICK
|
||
|
want_repaint |= Liquid_handleTick(liquid);
|
||
|
}
|
||
|
|
||
5 years ago
|
if (value & 0b1000) {
|
||
5 years ago
|
want_repaint |= Liquid_handleInput(liquid, InputEvent_Button(1));
|
||
5 years ago
|
} else if (value & 0b100) {
|
||
5 years ago
|
want_repaint |= Liquid_handleInput(liquid, InputEvent_Button(0));
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
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;
|
||
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
last_wheel_time = time;
|
||
5 years ago
|
|
||
5 years ago
|
if (value & 0b01) {
|
||
5 years ago
|
want_repaint |= Liquid_handleInput(liquid, InputEvent_Wheel(increment));
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
if (value & 0b10) {
|
||
5 years ago
|
want_repaint |= Liquid_handleInput(liquid, InputEvent_Wheel(-increment));
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
if (want_repaint) {
|
||
|
Liquid_paint(liquid);
|
||
|
}
|
||
5 years ago
|
}
|
||
|
}
|