-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCircuitOS.ino
188 lines (146 loc) · 4.29 KB
/
CircuitOS.ino
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <Arduino.h>
#include <CircuitOS.h>
#include <UI/Image.h>
#include <UI/Screen.h>
#include <CircuitOS.h>
#include <Util/Vector.h>
#include <UI/LinearLayout.h>
#include <UI/GridLayout.h>
#include <UI/ScrollLayout.h>
#include <Input/Input.h>
#include <Bitmaps/Bitmaps.hpp>
#include <Input/InputGPIO.h>
#include <Loop/LoopManager.h>
#define BTN_A 32
#define BTN_B 34
#define BTN_C 39
#define BTN_D 36
Display display(128, 128, 18, 4);
Screen mainScreen(display);
LinearLayout screenLayout(&mainScreen, HORIZONTAL);
LinearLayout btnLayout(&screenLayout, VERTICAL);
Image imageR(&btnLayout, 18, 18);
Image imageL(&btnLayout, 18, 18);
Image imageY(&btnLayout, 18, 18);
Image imageN(&btnLayout, 18, 18);
ScrollLayout mainScroll(&screenLayout);
GridLayout grid(&mainScroll, 2);
#define ELEMENTS 7
uint selected = 0;
Color colors[ELEMENTS] = { TFT_GREEN, TFT_PURPLE, TFT_WHITE, TFT_YELLOW, TFT_BLUE, TFT_LIGHTGREY, TFT_OLIVE };
Vector<Image*> gridImages;
Input* input = nullptr;
void setUI();
void scroll();
void selectElement(uint element);
void btnRPress(){
selectElement((selected + 1) % ELEMENTS);
}
void btnLPress(){
if(selected == 0){
selectElement(ELEMENTS - 1);
}else{
selectElement(selected - 1);
}
}
void setup(){
Serial.begin(115200);
input = new InputGPIO();
setUI();
mainScreen.draw();
mainScreen.commit();
input->setBtnReleaseCallback(BTN_A, btnRPress);
input->setBtnPressCallback(BTN_B, btnLPress);
LoopManager::addListener(input);
}
void loop(){
LoopManager::loop();
}
void selectElement(uint element){
gridImages[selected]->setBorderWidth(0);
gridImages[selected]->draw();
gridImages[element]->setBorderWidth(3);
gridImages[element]->draw();
selected = element;
scroll();
mainScreen.commit();
}
void scroll(){
Image* gridImage = gridImages[selected];
uint elStart = grid.getPadding() + (selected/2) * (gridImage->getHeight() + grid.getGutter());
uint elEnd = elStart + gridImage->getHeight();
uint screenStart = mainScroll.getScrollY();
uint screenEnd = screenStart + display.getHeight();
uint newScroll = mainScroll.getScrollY();
Serial.println("Element end / screen stop: " + String(elEnd) + " / " + String(screenEnd));
if(elStart < screenStart){
newScroll = elStart;
if(newScroll == grid.getPadding()){
newScroll = 0;
}else{
newScroll -= grid.getGutter() / 2;
}
}else if(elEnd > screenEnd){
newScroll += elEnd - screenEnd;
Serial.println("A / B: " + String(newScroll + display.getHeight()) + " / " + String(grid.getHeight() - grid.getPadding()));
if(newScroll + display.getHeight() == grid.getHeight() - grid.getPadding()){
newScroll += grid.getPadding();
}else{
newScroll += grid.getGutter() / 2;
}
}
if(newScroll == mainScroll.getScrollY()){
return;
}
if(newScroll != mainScroll.getScrollY()){
mainScroll.setScroll(0, newScroll);
mainScroll.clear();
mainScroll.draw();
}
}
void setUI(){
display.clear(TFT_GREEN);
/** Buttons */
imageR.getSprite()->clear(TFT_BLACK);
imageL.getSprite()->clear(TFT_BLACK);
imageY.getSprite()->clear(TFT_BLACK);
imageN.getSprite()->clear(TFT_BLACK);
imageR.getSprite()->drawIcon(arrowRight, 0, 0, 18, 18, 1);
imageL.getSprite()->drawIcon(arrowRight, 0, 0, 18, 18, 1);
imageY.getSprite()->drawIcon(yes, 0, 0, 18, 18, 1);
imageN.getSprite()->drawIcon(cross, 0, 0, 18, 18, 1);
imageL.getSprite()->rotate(2);
btnLayout.setWHType(FIXED, PARENT);
btnLayout.setWidth(28);
btnLayout.setPadding(5);
btnLayout.setGutter(15);
//btnLayout.setBorder(1, TFT_RED);
btnLayout.addChild(&imageR);
btnLayout.addChild(&imageL);
btnLayout.addChild(&imageY);
btnLayout.addChild(&imageN);
/** Grid */
for(int i = 0; i < ELEMENTS; i++){
Image* gridImage = new Image(&grid, 35, 35);
gridImages.push_back(gridImage);
gridImage->getSprite()->clear(colors[i]);
gridImage->setBorderColor(TFT_RED);
grid.addChild(gridImage);
}
gridImages.front()->setBorderWidth(3);
grid.setWHType(PARENT, CHILDREN);
grid.setPadding(10).setGutter(10);
/** Layout */
mainScroll.setWHType(FIXED, PARENT);
mainScroll.setWidth(100);
//mainScroll.setBorder(1, TFT_RED);
mainScroll.addChild(&grid);
screenLayout.setWHType(PARENT, PARENT);
screenLayout.addChild(&mainScroll).addChild(&btnLayout);
screenLayout.reflow();
mainScroll.reflow();
grid.reflow();
btnLayout.reflow();
mainScreen.addChild(&screenLayout);
mainScreen.repos();
}