-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInterface.cpp
239 lines (216 loc) · 6.3 KB
/
UserInterface.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* **************************************************************** */
/* ____ ______ */
/* Stepper /\###\ /#:+_+:\ */
/* \ \##+\/:+:/_\ :\ */
/* \ \#:+:+:+: : . \ */
/* By: Simon Ameye \ \:+:___: . \ */
/* \ \ :\_/. /\ \ */
/* \ \______/\ \___\ */
/* Please star my Github ! \/_____/ \/___/ */
/* https://github.com/simon-ameye/ */
/* **************************************************************** */
#include "UserInterface.hpp"
static void print_debug(std::string str)
{
#ifdef PRINT_DEBUG
std::cout << "User_interface : " << str << std::endl;
#endif
}
static std::string float_mass_to_str(float a_value)
{
std::ostringstream out;
out.precision(2);
out << std::fixed << a_value << "g";
return out.str();
}
UserInterface::UserInterface(void)
{
print_debug("constructor call");
uint16_t buffer[PicoExplorer::WIDTH * PicoExplorer::HEIGHT];
this->_pico_explorer = new PicoExplorer(buffer);
PicoExplorer *px = this->_pico_explorer;
px->init();
px->set_font(&font8);
px->set_pen(0, 0, 0);
px->clear();
px->set_pen(255, 255, 255);
px->text("initializing..." , Point(000, 10), 220, 3);
px->update();
print_debug("constructor return");
}
UserInterface::~UserInterface(void)
{
print_debug("destructor call");
delete this->_pico_explorer;
print_debug("destructor return");
}
char UserInterface::show_menu(std::string str_A, std::string str_B, std::string str_X, std::string str_Y, float coffee_mass_g)
{
print_debug("show menu call");
PicoExplorer *px = this->_pico_explorer;
print_debug("show menu : set rectangles");
Rect rectA( 0, 0, 120, 120);
Rect rectB( 0, 119, 120, 120);
Rect rectX(119, 0, 120, 120);
Rect rectY(119, 119, 120, 120);
print_debug("show menu : draw rectangles");
px->set_pen(100, 100, 0);
px->rectangle(rectA);
px->set_pen( 0, 100, 100);
px->rectangle(rectB);
px->set_pen(100, 0, 100);
px->rectangle(rectX);
px->set_pen(100, 100, 100);
px->rectangle(rectY);
px->set_pen(255, 255, 255);
print_debug("show menu : draw text");
std::string strAA = str_A + " " + float_mass_to_str(coffee_mass_g);
px->text(strAA, Point(0 , 50), 220, 2);
px->text(str_B, Point(0 , 170), 220, 2);
px->text(str_X, Point(120, 50), 220, 2);
px->text(str_Y, Point(120, 170), 220, 2);
px->update();
print_debug("show menu : loop");
char button;
while (1)
{
if (button = this->button_clicked())
return (button);
}
print_debug("show menu return");
return (0);
}
float UserInterface::get_float(float init_value, float step, std::string prt, std::string unit)
{
print_debug("get float call");
//std::cout << "width : " << PicoExplorer::WIDTH << "height : " << PicoExplorer::HEIGHT << std::endl;
float res;
res = init_value;
bool pressed = 0;
bool waspressdebefore = 0;
int i = 0;
PicoExplorer *px = this->_pico_explorer;
sleep_ms(300);
Rect rectangle(0, 0, 239, 239);
std::string str;
print_debug("get float : loop");
while (1)
{
px->set_pen(i * 2 % 160, i * 4 % 160, i * 8 % 160);
px->rectangle(rectangle);
px->set_pen(255, 255, 255);
str = float_mass_to_str(res);
px->text(prt , Point( 0, 10), 220, 4);
px->text(str , Point( 0, 90), 220, 9);
px->text("+" , Point(200, 30), 220, 10);
px->text("-" , Point(200, 140), 220, 10);
px->text("ok", Point( 0, 170), 220, 4);
px->update();
if (pressed == 1 && waspressdebefore == 0)
{
sleep_ms(300);
waspressdebefore = 1;
}
pressed = 1;
i++;
if(this->_button_click() == 'X')
res += step;
else if(this->_button_click() == 'Y')
res -= step;
else if(this->_button_click() == 'B')
{
this->button_clicked();
break ;
}
else
{
i--;
pressed = 0;
waspressdebefore = 0;
}
}
print_debug("get float return");
return (res);
}
void UserInterface::show_message_validate(std::string str)
{
print_debug("show message validate call");
//std::cout << "string : " << str << std::endl;
PicoExplorer *px = this->_pico_explorer;
Rect rectangle(0, 0, 239, 239);
px->set_pen(0, 0, 0);
px->rectangle(rectangle);
px->set_pen(255, 255, 255);
px->text(str , Point( 0, 0), 220, 4);
px->text("ok", Point( 0, 170), 220, 4);
px->update();
print_debug("show message validate : loop");
while (1)
{
if(this->button_clicked() == 'B')
break ;
}
print_debug("show message validate return");
}
void UserInterface::show_message_pass(std::string str)
{
print_debug("show message pass call");
PicoExplorer *px = this->_pico_explorer;
Rect rectangle(0, 0, 239, 239);
px->set_pen(0, 0, 0);
px->rectangle(rectangle);
px->set_pen(255, 255, 255);
px->text(str , Point(000, 90), 220, 4);
px->update();
print_debug("show message pass return");
}
void UserInterface::print_coffee_mass(float mass, std::string str)
{
print_debug("print coffee mass call");
//std::stringstream mass_strvalstream;
//std::string mass_str;
//mass_strvalstream << std::fixed << std::setprecision(2) << mass << "g";
//mass_str = mass_strvalstream.str();
PicoExplorer *px = this->_pico_explorer;
Rect rectangle(0, 0, 239, 239);
px->set_pen(0, 0, 0);
px->rectangle(rectangle);
px->set_pen(255, 255, 255);
px->text(str , Point(0, 0), 220, 4);
px->text(float_mass_to_str(mass) , Point(0, 90), 220, 5);
px->update();
print_debug("print coffee mass return");
}
char UserInterface::_button_click(void)
{
//print_debug("button click call");
PicoExplorer *px = this->_pico_explorer;
if (px->is_pressed(px->X))
return('X');
else if(px->is_pressed(px->Y))
return('Y');
else if(px->is_pressed(px->A))
return('A');
else if(px->is_pressed(px->B))
return('B');
//print_debug("button click return");
return (0);
}
char UserInterface::button_clicked(void)
{
//print_debug("button clicked call");
char button = this->_button_click();
while (this->_button_click());
//print_debug("button clicked return");
return (button);
}
void UserInterface::play_note(int frequency, float duty)
{
if (duty == 0)
{
this->_pico_explorer->set_tone(0, 0);
return ;
}
this->_pico_explorer->set_audio_pin(_pico_explorer->GP0);
this->_pico_explorer->set_tone(frequency, 0.5);
}