Caddy
A 2005 Roborodentia entry with vision and path planning capability
 All Data Structures Files Functions Variables Typedefs Macros Pages
tether_ui.c
1 /*
2  * This file is part of Caddy.
3  *
4  * Caddy is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Caddy is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Caddy. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "tether_ui.h"
18 #include "buttons.h"
19 #include "tweak_data.h"
20 #include "lcd_16x2.h"
21 #include "test_routines.h"
22 
23 // AVRLIB
24 #include "avrlibdefs.h"
25 
26 // avr-libc
27 #include <avr/io.h>
28 #include <avr/interrupt.h>
29 #include <stdint.h>
30 
31 #define WELCOME 0 // Must be 0
32 #define BASE_SPEED_TWEAK 1
33 #define LINE_TRACK_TWEAK 2
34 #define DAMP_TWEAK 3
35 #define TURN_TWEAK 4
36 #define PAN_TILT_TWEAK 5
37 #define TRACTOR_OVERSHOOT_DELAY_TWEAK 6
38 #define u08_TEMP_TWEAK 7
39 #define u16_TEMP_TWEAK 8
40 #define TEST_MODE_TWEAK 9
41 
42 #define NUM_TWEAK_MODES 10
43 
44 #define DELTA_SLOPE_COEF 8
45 #define DELTA_TEMP_2 2
46 
47 static uint8_t tweakMode;
48 
49 #define turnOnRemoteLight() sbi(PORTD, 7)
50 #define turnOffRemoteLight() cbi(PORTD, 7)
51 
52 static inline void updateTweaks(void);
53 static inline void printValues(void);
54 static inline void toggleTweakMode(uint8_t modeIncrement);
55 
56 inline void runTetherUI(void)
57 {
58  turnOnRemoteLight();
59 
60  tweakMode = WELCOME;
61 #if DEBUGGING
62  lcdWriteStr("Welcome, T-mode:", 0, 0);
63  toggleTestMode(0);
64 #endif
65 
66  lcdMode = NAV_LCD_MODE;
67 
68  while (!justReleased(RED_BUTTON))
69  {
71  updateTweaks();
72  printValues();
73 
74  if (bothRightButtonsPressed())
75  {
76  toggleTweakMode(1);
77  }
78  if (bothLeftButtonsPressed())
79  {
80  toggleTweakMode(-1);
81  }
82  }
83 
84  turnOffRemoteLight();
85 
86  // save new tweak values, entered a tweak mode
87  if (tweakMode != WELCOME)
88  {
89  storeTweakValues();
90  }
91 }
92 
96 inline void updateTweaks(void)
97 {
98  switch (tweakMode)
99  {
100  case BASE_SPEED_TWEAK:
101  if (justReleased(L_UP_BUTTON))
102  l_base++;
103  if (justReleased(L_DOWN_BUTTON))
104  l_base--;
105  if (justReleased(R_UP_BUTTON))
106  r_base++;
107  if (justReleased(R_DOWN_BUTTON))
108  r_base--;
109  break;
110 
111  case LINE_TRACK_TWEAK:
112  if (justReleased(L_UP_BUTTON))
113  slopeCoef += DELTA_SLOPE_COEF;
114  if (justReleased(L_DOWN_BUTTON))
115  slopeCoef -= DELTA_SLOPE_COEF;
116  if (justReleased(R_UP_BUTTON))
117  offCoef++;
118  if (justReleased(R_DOWN_BUTTON))
119  offCoef--;
120  break;
121 
122  case DAMP_TWEAK:
123  if (justReleased(L_UP_BUTTON))
124  dampCoef++;
125  if (justReleased(L_DOWN_BUTTON))
126  dampCoef--;
127  if (justReleased(R_UP_BUTTON))
128  lineCenter++;
129  if (justReleased(R_DOWN_BUTTON))
130  lineCenter--;
131  break;
132 
133  case TURN_TWEAK:
134  if (justReleased(L_UP_BUTTON))
135  turnPoint++;
136  if (justReleased(L_DOWN_BUTTON))
137  turnPoint--;
138  if (justReleased(R_UP_BUTTON))
139  turnSubtract++;
140  if (justReleased(R_DOWN_BUTTON))
141  turnSubtract--;
142  break;
143 
144  case PAN_TILT_TWEAK:
145  if (justReleased(L_UP_BUTTON))
146  panOffset++;
147  if (justReleased(L_DOWN_BUTTON))
148  panOffset--;
149  if (justReleased(R_UP_BUTTON))
150  tiltOffset++;
151  if (justReleased(R_DOWN_BUTTON))
152  tiltOffset--;
153  break;
154 
155  case TRACTOR_OVERSHOOT_DELAY_TWEAK:
156  if (justReleased(L_UP_BUTTON))
157  tractorOvershootDelay += 50;
158  if (justReleased(L_DOWN_BUTTON))
159  tractorOvershootDelay -= 50;
160  break;
161 
162  case u08_TEMP_TWEAK:
163  if (justReleased(L_UP_BUTTON))
164  tempTweak1++;
165  if (justReleased(L_DOWN_BUTTON))
166  tempTweak1--;
167  if (justReleased(R_UP_BUTTON))
168  tempTweak2 += DELTA_TEMP_2;
169  if (justReleased(R_DOWN_BUTTON))
170  tempTweak2 -= DELTA_TEMP_2;
171  break;
172 
173  case u16_TEMP_TWEAK:
174  if (justReleased(L_UP_BUTTON))
175  tempTweak3++;
176  if (justReleased(L_DOWN_BUTTON))
177  tempTweak3--;
178  if (justReleased(R_UP_BUTTON))
179  tempTweak4++;
180  if (justReleased(R_DOWN_BUTTON))
181  tempTweak4--;
182  break;
183 
184  case TEST_MODE_TWEAK:
185  if (justReleased(L_UP_BUTTON) || justReleased(R_UP_BUTTON))
186  toggleTestMode(1);
187  if (justReleased(L_DOWN_BUTTON) || justReleased(R_DOWN_BUTTON))
188  toggleTestMode(-1);
189  break;
190  }
191 }
192 
196 inline void printValues(void)
197 {
198  switch (tweakMode)
199  {
200  case BASE_SPEED_TWEAK:
201  lcdPrintHex(l_base, 1, 5);
202  lcdPrintHex(r_base, 1, 14);
203  break;
204  case LINE_TRACK_TWEAK:
205  lcdPrintHex(slopeCoef >> 8, 1, 3);
206  lcdPrintHex(slopeCoef, 1, 5);
207  lcdPrintHex(offCoef >> 8, 1, 11);
208  lcdPrintHex(offCoef, 1, 13);
209  break;
210  case DAMP_TWEAK:
211  lcdPrintHex(dampCoef, 1, 5);
212  lcdPrintHex(lineCenter, 1, 13);
213  break;
214  case TURN_TWEAK:
215  lcdPrintHex(turnPoint, 1, 3);
216  lcdPrintHex(turnSubtract, 1, 12);
217  break;
218  case PAN_TILT_TWEAK:
219  lcdPrintHex(panOffset, 1, 5);
220  lcdPrintHex(tiltOffset, 1, 13);
221  break;
222  case TRACTOR_OVERSHOOT_DELAY_TWEAK:
223  lcdPrintHex(tractorOvershootDelay >> 8, 1, 4);
224  lcdPrintHex(tractorOvershootDelay, 1, 6);
225  break;
226  case u08_TEMP_TWEAK:
227  lcdPrintHex(tempTweak1, 1, 3);
228  lcdPrintHex(tempTweak2, 1, 12);
229  break;
230  case u16_TEMP_TWEAK:
231  lcdPrintHex(tempTweak3 >> 8, 1, 3);
232  lcdPrintHex(tempTweak3, 1, 5);
233  lcdPrintHex(tempTweak4 >> 8, 1, 11);
234  lcdPrintHex(tempTweak4, 1, 13);
235  break;
236  }
237 }
238 
242 inline void toggleTweakMode(uint8_t modeIncrement)
243 {
244  // advance tweak mode (skipping welcome mode)
245  tweakMode += modeIncrement;
246  if (tweakMode == NUM_TWEAK_MODES)
247  {
248  tweakMode = 1;
249  }
250  else if (tweakMode == 0 || tweakMode == 255)
251  {
252  tweakMode = NUM_TWEAK_MODES - 1;
253  }
254 
255  // clear LCD and print labels for new tweak mode
256  switch (tweakMode)
257  {
258  case BASE_SPEED_TWEAK:
259  lcdWriteStr("Base speed tweak", 0, 0);
260  lcdWriteStr("Left: Right: ", 1, 0);
261  break;
262  case LINE_TRACK_TWEAK:
263  lcdWriteStr("Slp & Offst Cnst", 0, 0);
264  lcdWriteStr("sc: oc: ", 1, 0);
265  break;
266  case DAMP_TWEAK:
267  lcdWriteStr("Damping & Center", 0, 0);
268  lcdWriteStr("damp: cntr: ", 1, 0);
269  break;
270  case TURN_TWEAK:
271  lcdWriteStr("Turn tweak ", 0, 0);
272  lcdWriteStr("pt: subt: ", 1, 0);
273  break;
274  case PAN_TILT_TWEAK:
275  lcdWriteStr("Pan,Tilt Offsets", 0, 0);
276  lcdWriteStr("Pan: tilt: ", 1, 0);
277  break;
278  case TRACTOR_OVERSHOOT_DELAY_TWEAK:
279  lcdWriteStr("Tractor Os Delay", 0, 0);
280  lcdWriteStr("dly: ", 1, 0);
281  break;
282  case u08_TEMP_TWEAK:
283  lcdWriteStr("uint8_t Temp Tweaks ", 0, 0);
284  lcdWriteStr("T1: T2: ", 1, 0);
285  break;
286  case u16_TEMP_TWEAK:
287  lcdWriteStr("u16 Temp Tweaks ", 0, 0);
288  lcdWriteStr("T3: T4: ", 1, 0);
289  break;
290  case TEST_MODE_TWEAK:
291  lcdWriteStr("Test mode tweak ", 0, 0);
292  toggleTestMode(0);
293  break;
294  }
295 }