From 6f5f9793da3b40c97e0826c4418a99f71fa31426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Thu, 2 Feb 2017 17:21:43 +0100 Subject: Implement --- src/turris-lcd.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/turris-lcd.cpp b/src/turris-lcd.cpp index 034fc94..a3a8de8 100644 --- a/src/turris-lcd.cpp +++ b/src/turris-lcd.cpp @@ -1,17 +1,138 @@ #include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include + using namespace std; +const char omnia_str[] = "Turris Omnia feed:"; + +const int deftimeout = 2; +const char *pipe_path = "/tmp/turris-lcd"; + +void matrix_set(string str, int offset, int line, char (&matrix)[4][20]) { + for (int i = 0; i < 20; i++) { + if (str.length() > offset) { + matrix[line][i] = str[offset]; + offset++; + } else + matrix[line][i] = ' '; + } +} + +void read_pipe(vector &messages, queue &newone, int pipe) { + string msg = ""; + char c; + int ec; + while ((ec = read(pipe, &c, 1)) > 0) { + if (c == '\n') { + newone.push(messages.size()); + messages.push_back(msg); + cout << "Input: " << msg << "\n"; + } else + msg += c; + } + if (ec < 0 && errno != EAGAIN) { + cout << "Input broken! " << strerror(errno) << "\n"; + exit(1); + } +} + int main(int argc, char *argv[]) { + /* + if (argc < 1) { + cout << "Please pass i2c device as first argument\n"; + exit(1); + }*/ // Initialize LCD LiquidCrystal_I2C lcd("/dev/i2c-7", 0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); lcd.begin(20, 4); + char matrix[4][20]; + lcd.on(); lcd.clear(); + // Autoscroll doesn't work as expected for some reason, probably cheap display + + mkfifo(pipe_path, NULL); + errno = 0; // ignore if file exists + int pipe = open(pipe_path, O_RDONLY | O_NONBLOCK); + + vector messages; + queue newone; + messages.push_back(""); + messages.push_back(""); + messages.push_back(""); + + int msgs[3] = {0, 1, 2}; + int offsets[3] = {0, 0, 0}; + int timeout[3] = {deftimeout, deftimeout, deftimeout}; + int number_timeout = 0; + + while (true) { + // Read new lones from pipe + read_pipe(messages, newone, pipe); + + if (number_timeout > 3) { + ostringstream ss; + ss << "In total #"; + ss << (messages.size() - 3); + matrix_set(ss.str(), 0, 0, matrix); + } else + matrix_set(omnia_str, 0, 0, matrix); + number_timeout++; + if (number_timeout > 6) + number_timeout = 0; + + // Update matrix + for (int i = 0; i < 3; i++) { + matrix_set(messages[msgs[i]], offsets[i], i + 1, matrix); + // Update offsets + if (messages[msgs[i]].length() - offsets[i] > 20) + offsets[i]++; + else if (timeout[i] > 0) + timeout[i]--; + else { + offsets[i] = 0; + timeout[i] = deftimeout; + if (newone.empty()) { + int nw; + bool fine = false; + while (!fine) { + nw = rand() % messages.size(); + fine = true; + for (int s = 0; s < 3; s++) { + if (s != i && msgs[s] == nw) + fine = false; + } + } + msgs[i] = nw; + } else { + msgs[i] = newone.front(); + newone.pop(); + } + } + } - lcd.print("Turris Omnia"); + // Render matrix + for (int i = 0; i < 4; i++) { + lcd.setCursor(0, i); + for (int y = 0; y < 20; y++) { + lcd.write(matrix[i][y]); + } + } - return 0; + usleep(600000); + } } -- cgit v1.2.3