discoverpixy
screen_pixytest.c
Go to the documentation of this file.
1 /**************************************************************************************************************************************
2 * Project: discoverpixy
3 * Website: https://github.com/t-moe/discoverpixy
4 * Authors: Aaron Schmocker, Timo Lang
5 * Institution: BFH Bern University of Applied Sciences
6 * File: common/app/screen_pixytest.c
7 *
8 * Version History:
9 * Date Autor Email SHA Changes
10 * 2015-04-27 timolang@gmail.com cf72baa Introduced a Screen (sub) module and divided app into multiple screens.
11 * 2015-05-02 timolang@gmail.com 3281616 Added some more touch functions. Improved pixy test. Drag the Image around!
12 * 2015-05-09 timolang@gmail.com 0b5173e Added reference tracking.
13 * 2015-05-15 timolang@gmail.com 27c09ba Redesigned main menu. Moved stuff from pixytest to a new helper file and to the new "photo mode"-screen.
14 * 2015-05-25 timolang@gmail.com 6a61769 Reimplemented pixytest screen. Added a lot of Test-Buttons.
15 * 2015-06-01 aaron@duckpond.ch caa7b5c Added IRQ for user button, fixed some touchproblems.
16 * 2015-06-03 timolang@gmail.com 74aa186 Made pixy_test screen working again. Had to disable pixy_service. Recalibrated initial touch values.
17 * 2015-06-07 timolang@gmail.com c87220d Renamed pixy_helper to pixy_frame. Updated docu of appliaction. added doxygen comments to pixy_{frame,control}.h
18 *
19 **************************************************************************************************************************************/
20 
21 #include "screen_pixytest.h"
22 #include "button.h"
23 #include "numupdown.h"
24 #include "tft.h"
25 #include "touch.h"
26 #include "pixy.h"
27 #include "system.h"
28 #include "pixy_frame.h"
29 
30 static volatile enum {detecting, idle, update_servos, update_ledcolor, update_ledcurrent} state; //Current state of the screen state machine
31 
33 
39 static uint16_t servo_x;
40 static uint16_t servo_y;
41 
47 static uint32_t led_color;
48 
49 static uint32_t led_maxcurrent;
51 
52 
53 static void b_back_cb(void* button)
54 {
56 }
57 
58 static void b_servos_center_cb(void* button)
59 {
60  if (state == idle) {
61  servo_x = 500;
62  servo_y = 500;
64  }
65 }
66 
67 static void b_servos_topleft_cb(void* button)
68 {
69  if (state == idle) {
70  servo_x = 0;
71  servo_y = 0;
73  }
74 }
75 
76 static void b_servos_topright_cb(void* button)
77 {
78  if (state == idle) {
79  servo_x = 1000;
80  servo_y = 0;
82  }
83 }
84 
85 static void b_servos_bottomleft_cb(void* button)
86 {
87  if (state == idle) {
88  servo_x = 0;
89  servo_y = 1000;
91  }
92 }
93 
94 static void b_servos_bottomright_cb(void* button)
95 {
96  if (state == idle) {
97  servo_x = 1000;
98  servo_y = 1000;
100  }
101 }
102 
103 static void b_led_off_cb(void* button)
104 {
105  if (state == idle) {
106  led_color = 0x000000;
108  }
109 }
110 
111 static void b_led_white_cb(void* button)
112 {
113  if (state == idle) {
114  led_color = 0xFFFFFF;
116  }
117 }
118 
119 static void b_led_red_cb(void* button)
120 {
121  if (state == idle) {
122  led_color = 0xFF0000;
124  }
125 }
126 
127 static void b_led_green_cb(void* button)
128 {
129  if (state == idle) {
130  led_color = 0x00FF00;
132  }
133 }
134 
135 static void b_led_blue_cb(void* button)
136 {
137  if (state == idle) {
138  led_color = 0x0000FF;
140  }
141 }
142 
143 static void n_led_powerlimit_cb(void* numupdown, int16_t value)
144 {
145  if (state == idle) {
146  led_maxcurrent = value;
148  }
149 }
150 
151 static void enter(void* screen)
152 {
153  tft_clear(WHITE);
154 
155  //Back button
156  b_back.base.x1 = 10; //Start X of Button
157  b_back.base.y1 = 210; //Start Y of Button
158  b_back.base.x2 = AUTO; //Auto Calculate X2 with String Width
159  b_back.base.y2 = AUTO; //Auto Calculate Y2 with String Height
160  b_back.txtcolor = WHITE; //Set foreground color
161  b_back.bgcolor = HEX(0xAE1010); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible)
162  b_back.font = 0; //Select Font
163  b_back.text = "Back"; //Set Text (For formatted strings take sprintf)
164  b_back.callback = b_back_cb; //Call b_back_cb as Callback
165  gui_button_add(&b_back); //Register Button (and run the callback from now on)
166 
167 
168  //Servo stuff
169 #define SERVO_BUTTON_Y 10
170 #define SERVO_BUTTON_SPACING 5
171  tft_print_line(5, SERVO_BUTTON_Y, BLACK, TRANSPARENT, 0, "Servos:");
172 
173  b_servos_center.base.x1 = 55;
174  b_servos_center.base.y1 = SERVO_BUTTON_Y - 3;
175  b_servos_center.base.x2 = AUTO;
176  b_servos_center.base.y2 = AUTO;
177  b_servos_center.txtcolor = WHITE;
178  b_servos_center.bgcolor = HEX(0xAE1010);
179  b_servos_center.font = 0;
180  b_servos_center.text = "Center";
181  b_servos_center.callback = b_servos_center_cb;
182  gui_button_add(&b_servos_center);
183 
184  b_servos_topleft.base.x1 = b_servos_center.base.x2 + SERVO_BUTTON_SPACING;
185  b_servos_topleft.base.y1 = SERVO_BUTTON_Y - 3;
186  b_servos_topleft.base.x2 = AUTO;
187  b_servos_topleft.base.y2 = AUTO;
188  b_servos_topleft.txtcolor = WHITE;
189  b_servos_topleft.bgcolor = HEX(0xAE1010);
190  b_servos_topleft.font = 0;
191  b_servos_topleft.text = "ToLe";
192  b_servos_topleft.callback = b_servos_topleft_cb;
193  gui_button_add(&b_servos_topleft);
194 
195  b_servos_topright.base.x1 = b_servos_topleft.base.x2 + SERVO_BUTTON_SPACING;
196  b_servos_topright.base.y1 = SERVO_BUTTON_Y - 3;
197  b_servos_topright.base.x2 = AUTO;
198  b_servos_topright.base.y2 = AUTO;
199  b_servos_topright.txtcolor = WHITE;
200  b_servos_topright.bgcolor = HEX(0xAE1010);
201  b_servos_topright.font = 0;
202  b_servos_topright.text = "ToRi";
203  b_servos_topright.callback = b_servos_topright_cb;
204  gui_button_add(&b_servos_topright);
205 
206  b_servos_bottomleft.base.x1 = b_servos_topright.base.x2 + SERVO_BUTTON_SPACING;
207  b_servos_bottomleft.base.y1 = SERVO_BUTTON_Y - 3;
208  b_servos_bottomleft.base.x2 = AUTO;
209  b_servos_bottomleft.base.y2 = AUTO;
210  b_servos_bottomleft.txtcolor = WHITE;
211  b_servos_bottomleft.bgcolor = HEX(0xAE1010);
212  b_servos_bottomleft.font = 0;
213  b_servos_bottomleft.text = "BoLe";
214  b_servos_bottomleft.callback = b_servos_bottomleft_cb;
215  gui_button_add(&b_servos_bottomleft);
216 
217  b_servos_bottomright.base.x1 = b_servos_bottomleft.base.x2 + SERVO_BUTTON_SPACING;
218  b_servos_bottomright.base.y1 = SERVO_BUTTON_Y - 3;
219  b_servos_bottomright.base.x2 = AUTO;
220  b_servos_bottomright.base.y2 = AUTO;
221  b_servos_bottomright.txtcolor = WHITE;
222  b_servos_bottomright.bgcolor = HEX(0xAE1010);
223  b_servos_bottomright.font = 0;
224  b_servos_bottomright.text = "BoRi";
225  b_servos_bottomright.callback = b_servos_bottomright_cb;
226  gui_button_add(&b_servos_bottomright);
227 
228  //Led Color stuff
229 #define LED_COLOR_BUTTON_Y 35
230 #define LED_COLOR_BUTTON_SPACING 5
231  tft_print_line(5, LED_COLOR_BUTTON_Y, BLACK, TRANSPARENT, 0, "Led Color:");
232 
233  b_led_off.base.x1 = 85;
234  b_led_off.base.y1 = LED_COLOR_BUTTON_Y - 3;
235  b_led_off.base.x2 = AUTO;
236  b_led_off.base.y2 = AUTO;
237  b_led_off.txtcolor = WHITE;
238  b_led_off.bgcolor = BLACK;
239  b_led_off.font = 0;
240  b_led_off.text = "Off";
241  b_led_off.callback = b_led_off_cb;
242  gui_button_add(&b_led_off);
243 
244  b_led_white.base.x1 = b_led_off.base.x2 + LED_COLOR_BUTTON_SPACING;
245  b_led_white.base.y1 = LED_COLOR_BUTTON_Y - 3;
246  b_led_white.base.x2 = AUTO;
247  b_led_white.base.y2 = AUTO;
248  b_led_white.txtcolor = BLACK;
249  b_led_white.bgcolor = HEX(0xEEEEEE);
250  b_led_white.font = 0;
251  b_led_white.text = "White";
252  b_led_white.callback = b_led_white_cb;
253  gui_button_add(&b_led_white);
254 
255  b_led_red.base.x1 = b_led_white.base.x2 + LED_COLOR_BUTTON_SPACING;
256  b_led_red.base.y1 = LED_COLOR_BUTTON_Y - 3;
257  b_led_red.base.x2 = AUTO;
258  b_led_red.base.y2 = AUTO;
259  b_led_red.txtcolor = WHITE;
260  b_led_red.bgcolor = HEX(0xEE0000);
261  b_led_red.font = 0;
262  b_led_red.text = "Red";
263  b_led_red.callback = b_led_red_cb;
264  gui_button_add(&b_led_red);
265 
266  b_led_green.base.x1 = b_led_red.base.x2 + LED_COLOR_BUTTON_SPACING;
267  b_led_green.base.y1 = LED_COLOR_BUTTON_Y - 3;
268  b_led_green.base.x2 = AUTO;
269  b_led_green.base.y2 = AUTO;
270  b_led_green.txtcolor = WHITE;
271  b_led_green.bgcolor = HEX(0x00EE00);
272  b_led_green.font = 0;
273  b_led_green.text = "Green";
274  b_led_green.callback = b_led_green_cb;
275  gui_button_add(&b_led_green);
276 
277  b_led_blue.base.x1 = b_led_green.base.x2 + LED_COLOR_BUTTON_SPACING;
278  b_led_blue.base.y1 = LED_COLOR_BUTTON_Y - 3;
279  b_led_blue.base.x2 = AUTO;
280  b_led_blue.base.y2 = AUTO;
281  b_led_blue.txtcolor = WHITE;
282  b_led_blue.bgcolor = HEX(0x0000EE);
283  b_led_blue.font = 0;
284  b_led_blue.text = "Blue";
285  b_led_blue.callback = b_led_blue_cb;
286  gui_button_add(&b_led_blue);
287 
288  //Led MaxPower stuff
289 #define LED_POWER_BUTTON_Y 70
290  tft_print_line(5, LED_POWER_BUTTON_Y, BLACK, TRANSPARENT, 0, "Led Maximum Current:");
291 
292  //Num up down test
293  n_led_powerlimit.x = 160;
294  n_led_powerlimit.y = LED_POWER_BUTTON_Y - 7;
295  n_led_powerlimit.fgcolor = WHITE;
296  n_led_powerlimit.value = 10;
297  n_led_powerlimit.max = 40;
298  n_led_powerlimit.min = 0;
299  n_led_powerlimit.callback = n_led_powerlimit_cb;
300  gui_numupdown_add(&n_led_powerlimit);
301 
302 
303 
304  state = detecting;
305 }
306 
307 static void leave(void* screen)
308 {
309  gui_button_remove(&b_back);
310  gui_button_remove(&b_servos_center);
311  gui_button_remove(&b_servos_topleft);
312  gui_button_remove(&b_servos_topright);
313  gui_button_remove(&b_servos_bottomleft);
314  gui_button_remove(&b_servos_bottomright);
315  gui_button_remove(&b_led_off);
316  gui_button_remove(&b_led_white);
317  gui_button_remove(&b_led_red);
318  gui_button_remove(&b_led_green);
319  gui_button_remove(&b_led_blue);
320  gui_numupdown_remove(&n_led_powerlimit);
321 
322 }
323 
324 
325 static void update(void* screen)
326 {
327  switch (state) {
328  case detecting: //Detecting State: Where we try to connect to the pixy
329  if (pixy_init() == 0) { //Pixy connection ok
330  int32_t response;
331  int return_value;
332  return_value = pixy_command("stop", END_OUT_ARGS, &response, END_IN_ARGS);
334 
335  state = idle; //Go to next state
336  }
337 
338  break;
339 
340  case idle:
341  pixy_service();
342  break;
343 
344  case update_servos:
347  state = idle;
348  break;
349 
350  case update_ledcolor: {
351  int32_t response;
352  int return_value;
353  return_value = pixy_command("led_set", INT32(led_color), END_OUT_ARGS, &response, END_IN_ARGS);
354  state = idle;
355  }
356  break;
357 
358  case update_ledcurrent:
360  state = idle;
361  break;
362  }
363 
364 }
365 
366 
368  enter,
369  leave,
370  update
371 };
372 
373 
375 {
376  return &screen;
377 }
static void b_back_cb(void *button)
const char * text
The label of the button.
Definition: button.h:61
static void b_led_blue_cb(void *button)
uint16_t txtcolor
The 16-bit text color.
Definition: button.h:59
#define AUTO
Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height...
Definition: button.h:65
#define SERVO_BUTTON_Y
uint16_t x
The x-Coordinate of the Top-Left Starting Point.
Definition: numupdown.h:49
uint16_t y1
Top Left Y-Coordinate of Area.
Definition: touch.h:75
static BUTTON_STRUCT b_led_green
static void b_servos_bottomleft_cb(void *button)
bool gui_button_add(BUTTON_STRUCT *button)
Definition: button.c:133
void tft_print_line(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char *text)
Definition: tft.c:98
static void b_led_green_cb(void *button)
#define INT32(v)
Definition: pixydefs.h:65
uint16_t x1
Top Left X-Coordinate of Area.
Definition: touch.h:74
static uint16_t servo_x
static void b_servos_topright_cb(void *button)
#define TRANSPARENT
Definition: tft.h:66
static void b_led_white_cb(void *button)
static SCREEN_STRUCT screen
uint16_t bgcolor
The 16-bit background color of the button.
Definition: button.h:57
TOUCH_AREA_STRUCT base
Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct...
Definition: button.h:56
#define LED_COLOR_BUTTON_Y
static BUTTON_STRUCT b_servos_bottomleft
NUMUPDOWN_CALLBACK callback
Callback which is executed when the value changes.
Definition: numupdown.h:55
#define HEX(h)
Definition: tft.h:60
uint16_t y2
Bottom Right Y-Coordinate of Area.
Definition: touch.h:77
static void b_led_off_cb(void *button)
int pixy_service()
uint16_t y
The y-Coordinate of the Top-Left Starting Point.
Definition: numupdown.h:50
static uint32_t led_maxcurrent
static void b_servos_topleft_cb(void *button)
int16_t max
The maximum possible value (inclusive)
Definition: numupdown.h:54
SCREEN_STRUCT * get_screen_pixytest()
int pixy_command(const char *name,...)
Send a command to Pixy.
static BUTTON_STRUCT b_led_white
void tft_clear(uint16_t color)
Definition: tft.c:45
void gui_numupdown_remove(NUMUPDOWN_STRUCT *numupdown)
Definition: numupdown.c:136
uint16_t x2
Bottom Right X-Coordinate of Area.
Definition: touch.h:76
void gui_button_remove(BUTTON_STRUCT *button)
Definition: button.c:184
static void b_led_red_cb(void *button)
int16_t min
The minimum possible value (inclusive)
Definition: numupdown.h:53
int pixy_led_set_max_current(uint32_t current)
Set pixy LED maximum current.
#define WHITE
Definition: tft.h:53
static void leave(void *screen)
static BUTTON_STRUCT b_servos_topleft
static NUMUPDOWN_STRUCT n_led_powerlimit
int pixy_init()
Creates a connection with Pixy and listens for Pixy messages.
static BUTTON_STRUCT b_back
#define END_OUT_ARGS
Definition: pixydefs.h:89
static enum @1 state
static void b_servos_bottomright_cb(void *button)
int pixy_rcs_set_position(uint8_t channel, uint16_t position)
Set pixy servo axis position.
uint16_t fgcolor
The 16-bit color of the value-text.
Definition: numupdown.h:51
static void update(void *screen)
static void b_servos_center_cb(void *button)
static BUTTON_STRUCT b_led_blue
#define LED_COLOR_BUTTON_SPACING
#define SERVO_BUTTON_SPACING
static BUTTON_STRUCT b_led_red
#define LED_POWER_BUTTON_Y
static void n_led_powerlimit_cb(void *numupdown, int16_t value)
static BUTTON_STRUCT b_servos_topright
int16_t value
The current/default value.
Definition: numupdown.h:52
static void enter(void *screen)
BUTTON_CALLBACK callback
Callback which is executed when the button is pressed.
Definition: button.h:58
static BUTTON_STRUCT b_servos_center
static BUTTON_STRUCT b_servos_bottomright
static uint32_t led_color
bool gui_screen_back()
Definition: screen.c:85
uint8_t font
The number of the font to use.
Definition: button.h:60
static BUTTON_STRUCT b_led_off
#define END_IN_ARGS
Definition: pixydefs.h:90
#define BLACK
Definition: tft.h:54
bool gui_numupdown_add(NUMUPDOWN_STRUCT *numupdown)
Definition: numupdown.c:80
static uint16_t servo_y