aboutsummaryrefslogtreecommitdiffstats
path: root/source/sharp.cpp
blob: 4b7aa94bb46ed77d721f139639eb4f63d656a465 (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
/**
 * @file sharp.cpp
 * @brief Handles all drawing to the display.
 *
 * Copyright (C) 2019 Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "sharp.hpp"
#include "rtc.hpp"

constexpr unsigned int SHARP_SCK = 12;
constexpr unsigned int SHARP_MOSI = 13;
constexpr unsigned int SHARP_SS = 14;

Adafruit_SharpMem Sharp::display(SHARP_SCK, SHARP_MOSI, SHARP_SS, SHARP_WIDTH,
	SHARP_HEIGHT);
TaskHandle_t Sharp::taskHandle;
std::vector<Widget *> Sharp::widgets;

int Sharp::topY = 0;
int Sharp::scrollVelocity = 0;

void Sharp::begin(void)
{
	widgets.reserve(10);
	display.begin();
	display.clearDisplay();
	display.setTextSize(3);
	display.setTextColor(BLACK, WHITE);

	xTaskCreate(updateTask, "sharp", 512, nullptr, TASK_PRIO_LOW,
		&taskHandle);
}

void Sharp::sendInput(int ypos)
{
	if (ypos < 0 || ypos > SHARP_HEIGHT)
		return;

	int y = topY;
	for (unsigned int i = 0; i < widgets.size(); i++) {
		y += widgets[i]->getHeight();
		if (ypos < y) {
			if (widgets[i]->onPress()) {
				delete widgets[i];
				widgets.erase(widgets.begin() + i);
				display.clearDisplay();
			}
			break;
		}

		y += 3;
		if (y >= SHARP_HEIGHT)
			break;
	}
}

void Sharp::updateTask([[maybe_unused]] void *arg)
{
	static unsigned int counter = 0;
	while (1) {
		if (counter++ == 3) {
			counter = 0;

			auto y = topY;
			for (auto& w : widgets) {
				w->render(display, y);
				y += w->getHeight();
    				display.drawFastHLine(0, y + 1, SHARP_WIDTH, BLACK);
				y += 3;
				if (y >= SHARP_HEIGHT)
					break;
			}

			display.refresh();
		} else {
			topY += scrollVelocity * 20;
			if (scrollVelocity != 0)
				display.clearDisplay();
			if (topY > 0)
				topY = 0;
		}

		delay(50);
	}
}