Skip to content

Commit

Permalink
lib: add method to draw canvas to another canvas
Browse files Browse the repository at this point in the history
main: fix Letris
main: fix buffering in demos
main: add back WiFi scanner app
main: fix ServiceManager not starting services
  • Loading branch information
and3rson committed Mar 8, 2024
1 parent f2bdbf5 commit d77a805
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 65 deletions.
11 changes: 9 additions & 2 deletions firmware/main/src/apps/demos/epilepsy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
EpilepsyApp::EpilepsyApp() : App("Epilepsy") {}

void EpilepsyApp::run() {
lilka::Canvas buffer(canvas->width(), canvas->height());
buffer.begin();
buffer.fillScreen(0);
canvas->drawCanvas(&buffer);
queueDraw();

while (1) {
float time = millis() / 1000.0;
float size = sin(time * PI * 1.5) * LILKA_DISPLAY_WIDTH;
Expand All @@ -20,8 +26,9 @@ void EpilepsyApp::run() {
{(float)canvas->width() / 2 + size * cos(angle + 7 * PI / 4),
(float)canvas->height() / 2 + size * sin(angle + 7 * PI / 4)},
};
canvas->fillTriangle(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1], color);
canvas->fillTriangle(points[0][0], points[0][1], points[2][0], points[2][1], points[3][0], points[3][1], color);
buffer.fillTriangle(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1], color);
buffer.fillTriangle(points[0][0], points[0][1], points[2][0], points[2][1], points[3][0], points[3][1], color);
canvas->drawCanvas(&buffer);
queueDraw();

if (lilka::controller.getState().a.justPressed) {
Expand Down
31 changes: 16 additions & 15 deletions firmware/main/src/apps/demos/letris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define BLOCK_SIZE 10
#define FIELD_COLS 10
#define FIELD_ROWS 28
#define FIELD_ROWS 25

#define X_OFFSET LILKA_DISPLAY_WIDTH / 2 - FIELD_COLS *BLOCK_SIZE / 2

Expand All @@ -30,7 +30,7 @@ class Shape {
int shapeData[4][4];
uint16_t color;

Shape(lilka::Canvas *canvas) : canvas(canvas) {}
Shape() {}

void reset() {
// Генерує нову фігуру. Нові фігури відображаються в вікні попереднього перегляду
Expand All @@ -51,7 +51,7 @@ class Shape {
y = 0;
}

void draw(bool drawEmptyBlocks = false) {
void draw(lilka::Canvas *canvas, bool drawEmptyBlocks = false) {
for (int yy = 0; yy < 4; yy++) {
for (int xx = 0; xx < 4; xx++) {
if (this->shapeData[yy][xx]) {
Expand Down Expand Up @@ -99,21 +99,18 @@ class Shape {
}
}
}

private:
lilka::Canvas *canvas;
};

class Field {
public:
Field(lilka::Canvas *canvas) : canvas(canvas) {
Field() {
for (int y = 0; y < FIELD_ROWS; y++) {
for (int x = 0; x < FIELD_COLS; x++) {
this->blocks[y][x] = 0;
}
}
}
void draw() {
void draw(lilka::Canvas *canvas) {
// Малює поле
for (int y = 0; y < FIELD_ROWS; y++) {
for (int x = 0; x < FIELD_COLS; x++) {
Expand Down Expand Up @@ -169,16 +166,15 @@ class Field {

private:
uint16_t blocks[FIELD_ROWS][FIELD_COLS]; // Black color means no block
lilka::Canvas *canvas;
};

LetrisApp::LetrisApp() : App("Letris") {}

void LetrisApp::run() {
// Створюємо поле та фігуру
Field field(canvas);
Shape shape(canvas);
Shape nextShape(canvas);
Field field;
Shape shape;
Shape nextShape;
nextShape.reset();

// Вітання
Expand All @@ -198,10 +194,15 @@ void LetrisApp::run() {
);
}
}
queueDraw();
}

// Очищаємо екран
canvas->fillScreen(canvas->color565(32, 32, 32));
queueDraw();
// Ми робимо це двічі, щоб очистити обидва буфери (основний та задній)
canvas->fillScreen(canvas->color565(32, 32, 32));
queueDraw();

// Головний цикл гри
while (1) {
Expand Down Expand Up @@ -248,9 +249,9 @@ void LetrisApp::run() {
// Рухаємо фігуру горизонтально
shape.move(dx, 0);
// Малюємо поле та фігуру
field.draw();
shape.draw();
nextShape.draw(true);
field.draw(canvas);
shape.draw(canvas);
nextShape.draw(canvas, true);
queueDraw();
// Відображаємо зміни на екрані
// lilka::display.renderCanvas(canvas);
Expand Down
21 changes: 14 additions & 7 deletions firmware/main/src/apps/demos/lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
DemoLines::DemoLines() : App("Лінії") {}

void DemoLines::run() {
canvas->fillScreen(0);
lilka::Canvas buffer(canvas->width(), canvas->height());
buffer.begin();
buffer.fillScreen(0);
canvas->drawCanvas(&buffer);
queueDraw();

while (1) {
int x1 = random(0, canvas->width());
int y1 = random(0, canvas->height());
int x2 = random(0, canvas->width());
int y2 = random(0, canvas->height());
uint16_t color = random(0, 0xFFFF);
canvas->drawLine(x1, y1, x2, y2, color);
for (int i = 0; i < 64; i++) {
int x1 = random(0, canvas->width());
int y1 = random(0, canvas->height());
int x2 = random(0, canvas->width());
int y2 = random(0, canvas->height());
uint16_t color = random(0, 0xFFFF);
buffer.drawLine(x1, y1, x2, y2, color);
}
canvas->drawCanvas(&buffer);
queueDraw();
if (lilka::controller.getState().a.justPressed) {
stop();
Expand Down
28 changes: 19 additions & 9 deletions firmware/main/src/apps/demos/scan_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,42 @@
ScanI2CApp::ScanI2CApp() : App("I2C Scanner") {}

void ScanI2CApp::run() {
lilka::Canvas buffer(canvas->width(), canvas->height());
buffer.begin();
buffer.fillScreen(0);

Wire.begin(9, 10, 100000);

canvas->fillScreen(canvas->color565(0, 0, 0));
canvas->setTextBound(4, 0, LILKA_DISPLAY_WIDTH - 8, LILKA_DISPLAY_HEIGHT);
canvas->setCursor(4, 48);
canvas->println("Starting I2C scan...");
buffer.fillScreen(canvas->color565(0, 0, 0));
buffer.setTextBound(4, 0, LILKA_DISPLAY_WIDTH - 8, LILKA_DISPLAY_HEIGHT);
buffer.setCursor(4, 48);
buffer.println("Starting I2C scan...");
canvas->drawCanvas(&buffer);
queueDraw();

uint8_t found = 0;
for (uint16_t address = 1; address <= 127; address++) {
// Wire.requestFrom(addr, 0, true);
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
canvas->printf("%02X", address);
buffer.printf("%02X", address);
// lilka::display
found++;
} else {
canvas->print(".");
buffer.print(".");
}
if (address % 16 == 0 || address == 127) {
canvas->println();
buffer.println();
}
if (address % 32 == 0 || address == 127) {
canvas->drawCanvas(&buffer);
queueDraw();
}
}

canvas->println("I2C scan done.");
canvas->printf("Found %d devices.", found);
buffer.println("I2C scan done.");
buffer.printf("Found %d devices.", found);
canvas->drawCanvas(&buffer);
queueDraw();

Wire.end();
Expand Down
41 changes: 27 additions & 14 deletions firmware/main/src/apps/demos/user_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,60 @@ UserSPIApp::UserSPIApp() : App("SPI") {}

void UserSPIApp::run() {
#ifdef SPI2_NUM
canvas->fillScreen(canvas->color565(0, 0, 0));
canvas->setTextBound(4, 0, LILKA_DISPLAY_WIDTH - 8, LILKA_DISPLAY_HEIGHT);
canvas->setCursor(4, 48);
canvas->println("SPI2 begin");
lilka::Canvas buffer(canvas->width(), canvas->height());
buffer.begin();
buffer.fillScreen(0);

buffer.fillScreen(buffer.color565(0, 0, 0));
buffer.setTextBound(4, 0, LILKA_DISPLAY_WIDTH - 8, LILKA_DISPLAY_HEIGHT);
buffer.setCursor(4, 48);
buffer.println("SPI2 begin");
canvas->drawCanvas(&buffer);
queueDraw();

lilka::SPI2.begin(45, 48, 47);

vTaskDelay(500 / portTICK_PERIOD_MS);

canvas->println("beginTransaction");
canvas->println(" SPI mode: 0");
buffer.println("beginTransaction");
buffer.println(" SPI mode: 0");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
canvas->println(" write: 0xAA");
buffer.println(" write: 0xAA");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.write(0xAA);
canvas->println("endTransaction");
buffer.println("endTransaction");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.endTransaction();

vTaskDelay(10 / portTICK_PERIOD_MS);

canvas->println("beginTransaction");
canvas->println(" SPI mode: 3");
buffer.println("beginTransaction");
buffer.println(" SPI mode: 3");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
canvas->println(" SPI mode: 0xAA");
buffer.println(" SPI mode: 0xAA");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.write(0xAA);
canvas->println("endTransaction");
buffer.println("endTransaction");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.endTransaction();

vTaskDelay(500 / portTICK_PERIOD_MS);

canvas->println("SPI2 end");
buffer.println("SPI2 end");
canvas->drawCanvas(&buffer);
queueDraw();
lilka::SPI2.end();

canvas->println("Press A to exit");
buffer.println("Press A to exit");
canvas->drawCanvas(&buffer);
queueDraw();
while (!lilka::controller.getState().a.justPressed) {
taskYIELD();
Expand Down
12 changes: 9 additions & 3 deletions firmware/main/src/apps/demos/wifi_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
WifiScanApp::WifiScanApp() : App("WiFi Scanner") {}

void WifiScanApp::run() {
canvas->fillScreen(canvas->color565(0, 0, 0));
canvas->setCursor(4, 150);
lilka::Canvas buffer(canvas->width(), canvas->height());
buffer.begin();
buffer.fillScreen(0);

buffer.fillScreen(buffer.color565(0, 0, 0));
buffer.setCursor(4, 150);
canvas->drawCanvas(&buffer);
queueDraw();

WiFi.mode(WIFI_STA);
WiFi.disconnect();

canvas->println("Скануємо мережі WiFi...");
buffer.println("Скануємо мережі WiFi...");
canvas->drawCanvas(&buffer);
queueDraw();

int16_t count = WiFi.scanNetworks(false);
Expand Down
6 changes: 3 additions & 3 deletions firmware/main/src/apps/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "demos/letris.h"
#include "demos/user_spi.h"
#include "demos/scan_i2c.h"
#include "demos/wifi_scan.h"
#include "lua/luarunner.h"

#include "icons/demos.h"
Expand Down Expand Up @@ -62,13 +63,12 @@ void LauncherApp::run() {

void LauncherApp::appsMenu() {
String titles[] = {
// "Лінії", "Шайба", "М'ячик", "Епілепсія", "Летріс", "Тест SPI", "I2C-сканер", "<< Назад",
"Лінії", "Шайба", "М'ячик", "Епілепсія", "Летріс", "Тест SPI", "I2C-сканер", "<< Назад",
"Лінії", "Шайба", "М'ячик", "Епілепсія", "Летріс", "Тест SPI", "I2C-сканер", "WiFi-сканер", "<< Назад",
};
// vector of functions
APP_CLASS_LIST classes = {
APP_CLASS(DemoLines), APP_CLASS(DiskApp), APP_CLASS(BallApp), APP_CLASS(EpilepsyApp),
APP_CLASS(LetrisApp), APP_CLASS(UserSPIApp), APP_CLASS(ScanI2CApp),
APP_CLASS(LetrisApp), APP_CLASS(UserSPIApp), APP_CLASS(ScanI2CApp), APP_CLASS(WifiScanApp),
};
int count = sizeof(titles) / sizeof(titles[0]);
lilka::Menu menu("Демо");
Expand Down
1 change: 0 additions & 1 deletion firmware/main/src/apps/lua/lualilka_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#include <lua.hpp>
#include <lilka.h>

// int luaopen_lilka_console(lua_State* L);
int lualilka_console_register(lua_State* L);
1 change: 0 additions & 1 deletion firmware/main/src/apps/lua/lualilka_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#include <lilka.h>
#include <lua.hpp>

// int luaopen_lilka_controller(lua_State* L);
int lualilka_controller_register(lua_State* L);
1 change: 0 additions & 1 deletion firmware/main/src/apps/lua/lualilka_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#include <lua.hpp>
#include <lilka.h>

// int luaopen_lilka_display(lua_State *L);
int lualilka_display_register(lua_State *L);
1 change: 0 additions & 1 deletion firmware/main/src/apps/lua/lualilka_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#include <lua.hpp>
#include <lilka.h>

// int luaopen_lilka_math(lua_State *L);
int lualilka_math_register(lua_State* L);
1 change: 0 additions & 1 deletion firmware/main/src/apps/lua/lualilka_resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#include <lilka.h>
#include <lua.hpp>

// int luaopen_lilka_resources(lua_State* L);
int lualilka_resources_register(lua_State* L);
1 change: 0 additions & 1 deletion firmware/main/src/apps/lua/lualilka_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#include <lua.hpp>
#include <Arduino.h>

// int luaopen_lilka_util(lua_State* L);
int lualilka_util_register(lua_State* L);
10 changes: 5 additions & 5 deletions firmware/main/src/apps/statusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ void StatusBarApp::run() {
canvas->fillScreen(lilka::display.color565(0, 0, 0));
canvas->setTextColor(lilka::display.color565(255, 255, 255), lilka::display.color565(0, 0, 0));
canvas->setFont(FONT_9x15);
canvas->setCursor(32, 20);
canvas->setCursor(32, 18);
canvas->print("Time: " + String(counter++));
if (networkService->getNetworkState() == NETWORK_STATE_OFFLINE) {
canvas->draw16bitRGBBitmapWithTranColor(136, 0, wifi_offline, 0, 24, 24);
canvas->draw16bitRGBBitmapWithTranColor(144, 0, wifi_offline, 0, 24, 24);
} else if (networkService->getNetworkState() == NETWORK_STATE_CONNECTING) {
canvas->draw16bitRGBBitmapWithTranColor(136, 0, wifi_connecting, 0, 24, 24);
canvas->draw16bitRGBBitmapWithTranColor(144, 0, wifi_connecting, 0, 24, 24);
} else {
canvas->draw16bitRGBBitmapWithTranColor(136, 0, icons[networkService->getSignalStrength()], 0, 24, 24);
canvas->draw16bitRGBBitmapWithTranColor(144, 0, icons[networkService->getSignalStrength()], 0, 24, 24);
}
canvas->draw16bitRGBBitmapWithTranColor(136 + 24, 0, battery, 0, 32, 24);
canvas->draw16bitRGBBitmapWithTranColor(144 + 8 + 24, 0, battery, 0, 32, 24);
// canvas->draw16bitRGBBitmapWithTranColor(160, 0, wifi, 24, 24, 0);
// canvas->draw16bitRGBBitmapWithTranColor(160 + 24, 0, battery, 24, 24, 0);
queueDraw();
Expand Down
1 change: 1 addition & 0 deletions firmware/main/src/servicemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ ServiceManager *ServiceManager::getInstance() {

void ServiceManager::addService(Service *service) {
services.push_back(service);
service->start();
}
Loading

0 comments on commit d77a805

Please sign in to comment.