Files
wordclock/src/matrix/render_functions.cpp

267 lines
5.8 KiB
C++

#include <Arduino.h>
#include "wordclock_functions.h"
#include "ledmatrix.h"
#include "udp_logger.h"
extern LEDMatrix led_matrix;
const String clock_chars_as_string = "ESRISTNFUNFVIERTELZEHNZWANZIGHVORPIKACHUNACHHALBMELFUNFMITTERNACHTEINSUWUZWEIDREIFUNVIERSECHSOBACHTSIEBENZWOLFZEHNEUNEUHR";
/**
* @brief control the four minute indicator LEDs
*
* @param minutes minutes to be displayed [0 ... 59]
* @param color 24bit color value
*/
void draw_minute_indicator(uint8_t minutes, uint32_t color)
{
// separate LEDs for minutes in an additional row
{
switch (minutes % 5)
{
case 1:
{
led_matrix.set_min_indicator(0b1000, color);
break;
}
case 2:
{
led_matrix.set_min_indicator(0b1100, color);
break;
}
case 3:
{
led_matrix.set_min_indicator(0b1110, color);
break;
}
case 4:
{
led_matrix.set_min_indicator(0b1111, color);
break;
}
case 0:
default:
{
break;
}
}
}
}
/**
* @brief Draw the given sentence to the word clock
*
* @param message sentence to be displayed
* @param color 24bit color value
* @return int: 0 if successful, -1 if sentence not possible to display
*/
int show_string_on_clock(String message, uint32_t color)
{
String word = "";
int last_letter_clock = 0;
int word_position = 0;
int idx = 0;
// add space on the end of message for splitting
message = message + " ";
// empty the target grid
led_matrix.flush();
while (true)
{
// extract next word from message
word = split(message, ' ', idx);
idx++;
if (word.length() > 0)
{
// find word in clock string
word_position = clock_chars_as_string.indexOf(word, last_letter_clock);
if (word_position >= 0)
{
// word found on clock -> enable leds in targetgrid
for (unsigned int i = 0; i < word.length(); i++)
{
unsigned int x = (word_position + i) % MATRIX_WIDTH;
unsigned int y = (word_position + i) / MATRIX_WIDTH;
led_matrix.grid_add_pixel(x, y, color);
}
// remember end of the word on clock
last_letter_clock = word_position + word.length();
}
else
{
// word is not possible to show on clock
return -1;
}
}
else // end - no more word in message
{
break;
}
}
return 0; // return success
}
/**
* @brief Converts the given time as sentence (String)
*
* @param hours hours of the time value
* @param minutes minutes of the time value
* @return String time as sentence
*/
String time_to_string(uint8_t hours, uint8_t minutes)
{
String message = "ES IST "; // first two words
// show minutes
if (minutes >= 5 && minutes < 10)
{
message += "FUNF NACH ";
}
else if (minutes >= 10 && minutes < 15)
{
message += "ZEHN NACH ";
}
else if (minutes >= 15 && minutes < 20)
{
message += "VIERTEL NACH ";
}
else if (minutes >= 20 && minutes < 25)
{
message += "ZEHN VOR HALB ";
}
else if (minutes >= 25 && minutes < 30)
{
message += "FUNF VOR HALB ";
}
else if (minutes >= 30 && minutes < 35)
{
message += "HALB ";
}
else if (minutes >= 35 && minutes < 40)
{
message += "FUNF NACH HALB ";
}
else if (minutes >= 40 && minutes < 45)
{
message += "ZEHN NACH HALB ";
}
else if (minutes >= 45 && minutes < 50)
{
message += "VIERTEL VOR ";
}
else if (minutes >= 50 && minutes < 55)
{
message += "ZEHN VOR ";
}
else if (minutes >= 55 && minutes < 60)
{
message += "FUNF VOR ";
}
// convert hours to 12h format
if (hours >= 12)
{
hours -= 12;
}
if (minutes >= 20)
{
hours++;
}
if (hours == 12)
{
hours = 0;
}
// show hours
switch (hours)
{
case 0:
message += "ZWOLF ";
break;
case 1:
message += "EIN";
// EIN(S)
if (minutes > 4)
{
message += "S";
}
message += " ";
break;
case 2:
message += "ZWEI ";
break;
case 3:
message += "DREI ";
break;
case 4:
message += "VIER ";
break;
case 5:
message += "FUNF ";
break;
case 6:
message += "SECHS ";
break;
case 7:
message += "SIEBEN ";
break;
case 8:
message += "ACHT ";
break;
case 9:
message += "NEUN ";
break;
case 10:
message += "ZEHN ";
break;
case 11:
message += "ELF ";
break;
}
if (minutes < 5)
{
message += "UHR ";
}
return message;
}
/**
* @brief Splits a string at given character and return specified element
*
* @param s string to split
* @param parser separating character
* @param index index of the element to return
* @return String
*/
String split(String s, char parser, int index)
{
String rs = "";
int parser_cnt = 0;
int r_from_index = 0, r_to_index = -1;
while (index >= parser_cnt)
{
r_from_index = r_to_index + 1;
r_to_index = s.indexOf(parser, r_from_index);
if (index == parser_cnt)
{
if (r_to_index == 0 || r_to_index == -1)
{
return "";
}
return s.substring(r_from_index, r_to_index);
}
else
parser_cnt++;
}
return rs;
}