aboutsummaryrefslogtreecommitdiff
path: root/src/turris-lcd.cpp
blob: a3a8de82c0b8c7bd5b12bb979213f23bc677662b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <LiquidCrystal_I2C.h>
#include <unistd.h>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cerrno>
#include <queue>
#include <sstream>


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<string> &messages, queue<int> &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<string> messages;
	queue<int> 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();
				}
			}
		}

		// 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]);
			}
		}

		usleep(600000);
	}
}