discoverpixy
screen_photomode.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_photomode.c
7 *
8 * Version History:
9 * Date Autor Email SHA Changes
10 * 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.
11 * 2015-05-16 timolang@gmail.com 62006e0 Documented pixy_helper and implemented/finished photo-mode screens! Snap some shots!
12 * 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
13 *
14 **************************************************************************************************************************************/
15 
16 #include "screen_photomode.h"
17 #include "screen_photomode_save.h"
18 #include "button.h"
19 #include "tft.h"
20 #include "touch.h"
21 #include "pixy.h"
22 #include "system.h"
23 #include "pixy_frame.h"
24 
25 static bool pixy_connected = false; //Whether or not the pixy cam is currently connected
26 
27 static BUTTON_STRUCT b_back; //Button to navigate back
28 static BUTTON_STRUCT b_save; //Button to save the current image
29 static TOUCH_AREA_STRUCT a_area; //Touch Area, where the frame is drawn. Used to drag the image around
30 static bool subMenu = false; //Whether or not we left the current screen for a submenu
31 
32 //Callback for when the user presses the "back" button
33 static void b_back_cb(void* button)
34 {
35  subMenu = false; //we're not entering a submenu
36  gui_screen_back(); //navigate back to the previous screen
37 }
38 
39 //Callback for when the user presses the "save" button
40 static void b_save_cb(void* button)
41 {
42  subMenu = true; //we're entering a submenu
43  gui_screen_navigate(get_screen_photomodesave()); //navigate to the save screen
44 }
45 
46 static POINT_STRUCT pixy_pos; //The current position of pixy's servos
47 static POINT_STRUCT old_pos; //The last touch position on the screen
48 
49 //Callback for when the user drags the image around
50 static void touchCB(void* touchArea, TOUCH_ACTION triggeredAction)
51 {
52  POINT_STRUCT p = touch_get_last_point(); //get the last touched point
53 
54  switch (triggeredAction) {
55  case PEN_ENTER:
56  case PEN_DOWN:
57  old_pos = p; //If the user "newly" enters the touch area, we set the "last" position to the current
58  break;
59 
60  case PEN_MOVE: { //the user is moving around, he entered the screen a while ago (old_pos is set)
61  int16_t deltaX = p.x - old_pos.x; //Calculate x difference between last and current touch
62  int16_t deltaY = p.y - old_pos.y; //Calculate y difference between last and current touch
63  old_pos = p; //store the current touch point for the next time
64 
65  //printf("%d %d\n",deltaX,deltaY);
66  if (pixy_connected) {
67  //Calculate new servo coordinates. 2 is just a proportional factor
68  int16_t new_x = pixy_pos.x + deltaX * 2;
69  int16_t new_y = pixy_pos.y - deltaY * 2;
70 
71  //check limits
72  if (new_x < 0) {
73  new_x = 0;
74  }
75 
76  if (new_x > 1000) {
77  new_x = 1000;
78  }
79 
80  if (new_y < 0) {
81  new_y = 0;
82  }
83 
84  if (new_y > 1000) {
85  new_y = 1000;
86  }
87 
88  //set pixy_pos so that the main routine can send it to the servos
89  pixy_pos.x = new_x;
90  pixy_pos.y = new_y;
91  }
92  }
93  break;
94 
95  case PEN_UP:
96  case PEN_LEAVE:
97  //printf("Leave/up\n");
98  break;
99 
100  default:
101  break;
102  }
103 
104 }
105 
106 //Callback for when the screen is entered/loaded
107 static void enter(void* screen)
108 {
109  tft_clear(WHITE);
110 
111  tft_print_line(5, 5, BLACK, TRANSPARENT, 0, "Drag the image around and ");
112 
113  //Back button
114  b_back.base.x1 = 5; //Start X of Button
115  b_back.base.y1 = 19; //Start Y of Button
116  b_back.base.x2 = AUTO; //Auto Calculate X2 with String Width
117  b_back.base.y2 = AUTO; //Auto Calculate Y2 with String Height
118  b_back.txtcolor = WHITE; //Set foreground color
119  b_back.bgcolor = HEX(0xAE1010); //Set background color (Don't take 255 or 0 on at least one channel, to make shadows possible)
120  b_back.font = 0; //Select Font
121  b_back.text = "Back"; //Set Text (For formatted strings take sprintf)
122  b_back.callback = b_back_cb; //Call b_back_cb as Callback
123  gui_button_add(&b_back); //Register Button (and run the callback from now on)
124 
125  //Save button
126  b_save.base.x1 = 190;
127  b_save.base.y1 = 3;
128  b_save.base.x2 = AUTO;
129  b_save.base.y2 = AUTO;
130  b_save.txtcolor = WHITE;
131  b_save.bgcolor = HEX(0x1010AE);
132  b_save.font = 0;
133  b_save.text = "Save it!";
134  b_save.callback = b_save_cb;
135  gui_button_add(&b_save);
136 
137  //Frame Coordinates: topleft = (1,40); bottomright = (318,238)
138  //Leave a 10px border for the area
139 
140  //Area to drag the image around
142  a_area.x1 = 11;
143  a_area.y1 = 50;
144  a_area.x2 = 308;
145  a_area.y2 = 228;
146  a_area.callback = touchCB;
147  touch_register_area(&a_area);
148 
149  //Pixy stuff
150  pixy_connected = (pixy_init() == 0); //try to connect to pixy
151 
152  if (pixy_connected && !subMenu) { //pixy is connected, but we are not coming from a submenu
153  pixy_pos.x = pixy_pos.y = 500; //reset servo positions to center
154  }
155 }
156 
157 //Callback for when the screen is left/unloaded
158 static void leave(void* screen)
159 {
160  //remove buttons and touch area.
161  gui_button_remove(&b_back);
162  gui_button_remove(&b_save);
163  touch_unregister_area(&a_area);
164 }
165 
166 //Callback for when the screen should be updated
167 //This is the main loop of the screen. This method will be called repeatedly
168 static void update(void* screen)
169 {
170  //Note: The only way to detect that pixy has been disconnected is if a command fails. There's no pixy_is_connected method yet :'(
171 
172  if (!pixy_connected) { //Pixy not connected
173  pixy_close(); //Ensure that all pixy resources are freed (failsafe)
174 
175  if (pixy_init() == 0) { //try to connect to pixy
176  pixy_connected = true;
177 
178  if (!subMenu) { //we're not coming from a submenu
179  pixy_pos.x = pixy_pos.y = 500; //reset servo positions to center
180  }
181 
182  printf("pixy (re)initialized\n");
183  }
184  }
185 
186  if (pixy_connected) { //If we are connected (now)
187  pixy_service(); //Handle pending pixy events (e.g. color info retrival)
188 
189  pixy_render_full_frame(1, 40); //render the pixy video at point (1,40)
190 
191  //set the servo positions to the coordinates form the touch interrupt
192  pixy_rcs_set_position(0, pixy_pos.x);
193  pixy_rcs_set_position(1, pixy_pos.y);
194  }
195 }
196 
197 //Declare screen callbacks
199  enter,
200  leave,
201  update
202 };
203 
204 
206 {
207  return &screen;
208 }
TOUCH_ACTION
Definition: touch.h:52
bool touch_register_area(TOUCH_AREA_STRUCT *area)
Definition: touch.c:181
const char * text
The label of the button.
Definition: button.h:61
Receive an event when the pen moves inside the region (pen is down)
Definition: touch.h:58
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
Receive an event when the pen goes down inside the region.
Definition: touch.h:54
Receive an event when the pen goes up inside the region.
Definition: touch.h:55
SCREEN_STRUCT * get_screen_photomodesave()
uint16_t y1
Top Left Y-Coordinate of Area.
Definition: touch.h:75
uint16_t y
The Y-Coordinate of the point.
Definition: touch.h:88
static void touchCB(void *touchArea, TOUCH_ACTION triggeredAction)
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
uint16_t x
The X-Coordinate of the point.
Definition: touch.h:87
static bool subMenu
uint16_t x1
Top Left X-Coordinate of Area.
Definition: touch.h:74
#define TRANSPARENT
Definition: tft.h:66
static BUTTON_STRUCT b_back
uint16_t bgcolor
The 16-bit background color of the button.
Definition: button.h:57
static POINT_STRUCT pixy_pos
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
static SCREEN_STRUCT screen
void touch_unregister_area(TOUCH_AREA_STRUCT *area)
Definition: touch.c:195
static void b_save_cb(void *button)
#define HEX(h)
Definition: tft.h:60
uint16_t y2
Bottom Right Y-Coordinate of Area.
Definition: touch.h:77
static void leave(void *screen)
int pixy_service()
static BUTTON_STRUCT b_save
TOUCH_CALLBACK callback
Callback which is executed when an event occurred in this Area.
Definition: touch.h:78
void tft_clear(uint16_t color)
Definition: tft.c:45
static void b_back_cb(void *button)
uint16_t x2
Bottom Right X-Coordinate of Area.
Definition: touch.h:76
static TOUCH_AREA_STRUCT a_area
void gui_button_remove(BUTTON_STRUCT *button)
Definition: button.c:184
Receive an event when the pen enters the region (pen was down before)
Definition: touch.h:56
bool gui_screen_navigate(SCREEN_STRUCT *screen)
Definition: screen.c:74
static void enter(void *screen)
SCREEN_STRUCT * get_screen_photomode()
#define WHITE
Definition: tft.h:53
POINT_STRUCT touch_get_last_point()
Definition: touch.c:211
static POINT_STRUCT old_pos
int pixy_init()
Creates a connection with Pixy and listens for Pixy messages.
TOUCH_ACTION hookedActions
Actions to listen to.
Definition: touch.h:73
static bool pixy_connected
int pixy_rcs_set_position(uint8_t channel, uint16_t position)
Set pixy servo axis position.
static void update(void *screen)
void pixy_close()
Terminates connection with Pixy.
BUTTON_CALLBACK callback
Callback which is executed when the button is pressed.
Definition: button.h:58
int pixy_render_full_frame(uint16_t x, uint16_t y)
Definition: pixy_frame.c:25
bool gui_screen_back()
Definition: screen.c:85
uint8_t font
The number of the font to use.
Definition: button.h:60
#define BLACK
Definition: tft.h:54
Receive an event when the pen leaves the region (pen was inside region before)
Definition: touch.h:57