Files
pti-ledy/ledy.ino
T

154 lines
4.0 KiB
Arduino

#include <Adafruit_NeoPixel.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>
#include "config.h"
#include "index.h"
#include "fonts.h"
#include "lowLevel.h"
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
TextNode text_nodes[MAX_TEXT_NODES_COUNT];
Cursor cursor;
unsigned short ever_created_text_nodes = 0;
unsigned short getTextNodeY2(TextNode *node)
{
return node->pos_y + node->characterSize.height - 1;
}
unsigned short getTextNodeX2(TextNode *node)
{
if (node->character_count == 0) return node->pos_x;
return node->pos_x + (node->characterSize.width * node->character_count) + (node->character_count - 1) - 1;
}
void addNewTextNode
(
char text[TEXT_MAX_LENGTH + 1], uint32_t color = 0x00010101, unsigned short pos_x = 0, unsigned short pos_y = 0,
unsigned char scroll_slowness = 1, bool is_scrolling = true, bool is_small = true
)
{
unsigned char text_length = strlen(text);
if (text_length == 0){return;}
for (unsigned char i = 0; i < MAX_TEXT_NODES_COUNT; i++)
{
if (text_nodes[i].is_deleted)
{
strncpy(text_nodes[i].content, text, TEXT_MAX_LENGTH);
text_nodes[i].color = color;
text_nodes[i].pos_x = pos_x;
text_nodes[i].pos_y = pos_y;
text_nodes[i].characterSize.height = is_small ? SMALL_TEXT_HEIGHT : MEDIUM_TEXT_HEIGHT;
text_nodes[i].characterSize.width = is_small ? SMALL_TEXT_WIDTH : MEDIUM_TEXT_WIDTH;
text_nodes[i].character_count = text_length;
text_nodes[i].scroll_slowness = scroll_slowness;
text_nodes[i].is_scrolling = is_scrolling;
text_nodes[i].is_deleted = false;
ever_created_text_nodes++;
break;
}
}
}
void drawTextNodes(bool reset_cursor = true)
{
for (unsigned char i = 0; i < MAX_TEXT_NODES_COUNT; i++)
{
if (!text_nodes[i].is_deleted)
{
const unsigned short POS_X2 = getTextNodeX2(&(text_nodes)[i]);
if (POS_X2 > PANEL_MAX_X)
{
text_nodes[i].pos_x -= POS_X2 - PANEL_MAX_X;
}
if (reset_cursor)
{
cursor.x = text_nodes[i].pos_x;
cursor.y = text_nodes[i].pos_y;
}
for (unsigned char j = 0; j < text_nodes[i].character_count; j++)
{
char ch = text_nodes[i].content[j];
if (ch < '!' || ch > '~')
{
ch = ' ';
}
drawCharacter(font7x5[ch - ' '], text_nodes[i].characterSize.height, text_nodes[i].characterSize.width, text_nodes[i].color, &cursor);
}
}
}
}
void scrollAllScrollableTexts(bool split_scroll_mode = false)
{
for (unsigned char i = 0; i < MAX_TEXT_NODES_COUNT; i++)
{
if (!text_nodes[i].is_deleted && text_nodes[i].is_scrolling)
{
unsigned short x1 = text_nodes[i].pos_x;
unsigned short y1 = text_nodes[i].pos_y;
unsigned short x2 = getTextNodeX2(&text_nodes[i]);
unsigned short y2 = getTextNodeY2(&text_nodes[i]);
if (split_scroll_mode || text_nodes[i].pos_y < 7)
{
if (x2 == 0)
{
text_nodes[i].is_deleted = true;
continue;
}
shiftGivenRectangleLeft(x1, y1, x2, y2, text_nodes[i].scroll_slowness);
text_nodes[i].pos_x -= text_nodes[i].scroll_slowness;
}
else
{
if (x1 > PANEL_MAX_X)
{
text_nodes[i].is_deleted = true;
continue;
}
shiftGivenRectangleRight(x1, y1, x2, y2, text_nodes[i].scroll_slowness);
text_nodes[i].pos_x += text_nodes[i].scroll_slowness;
}
}
}
}
void setup()
{
Serial.begin(115200);
Serial.println("----------------------------------------------------");
pixels.begin();
pixels.clear();
addNewTextNode("NET", 0xFF050505);
addNewTextNode("AWAIT", 0xFF050505, 0, 9);
drawTextNodes();
pixels.show();
start_server();
}
void loop()
{
scrollAllScrollableTexts();
pixels.show();
handle_server();
if (text_nodes[0].is_deleted && text_nodes[1].is_deleted)
{
addNewTextNode("test", 0xFF121212, 100);
drawTextNodes();
}
}