Contiki 2.5
menu.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of the copyright holders nor the names of
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \file
32  *
33  * \brief
34  * This file operates the menu flow chart described in the readme
35  * notes. This will create the proper commands needed to control the 1284p.
36  *
37  * \author
38  * Mike Vidales mavida404@gmail.com
39  *
40  */
41 
42 #include <avr/eeprom.h>
43 #include <util/delay.h>
44 #include "menu.h"
45 #include "main.h"
46 #include "lcd.h"
47 #include "key.h"
48 #include "uart.h"
49 #include "sleep.h"
50 #include "temp.h"
51 #include "beep.h"
52 
53 uint8_t sleep_count;
54 uint8_t ping_count;
55 uint8_t ping_response;
56 bool ping_mode;
57 bool timeout_flag;
58 bool temp_flag;
59 bool temp_mode;
60 bool auto_temp=true;
61 
62 /**
63  * \addtogroup lcd
64  * \{
65 */
66 
67 /*---------------------------------------------------------------------------*/
68 
69 /**
70  * \brief This function will convert decimal to ascii.
71  *
72  * \param val Decimal value to convert.
73  * \param str Address location to store converted value.
74 */
75 void
76 dectoascii(uint8_t val, char *str)
77 {
78  *(str+1) = (val % 10) + '0';
79  *str = (val / 10) + '0';
80 }
81 
82 /*---------------------------------------------------------------------------*/
83 
84 /**
85  * \brief This will convert a signed decimal number to ASCII.
86  *
87  * \param n Signed number
88  * \param str Pointer to store converted value.
89  *
90  * \return *p Address of stored conversion.
91 */
92 uint8_t
93 *signed_dectoascii(int16_t n, uint8_t *str)
94 {
95  uint8_t * p = str;
96  uint8_t neg = 0;
97 
98  if(n < 0){
99  neg = 1;
100  n = -n;
101  }
102 
103  *p-- = 0x00;
104 
105  /* Determine the unit of conversion. */
106  if(temp_mode == TEMP_UNIT_CELCIUS){
107  /* Add ASCII C to string. */
108  *p-- = 'C';
109  }
110  else{
111  /* Add ASCII F to string. */
112  *p-- = 'F';
113  }
114 
115  /* Add a space before unit symbol. */
116  *p-- = ' ';
117 
118  /* For zero, just print zero. */
119  if (!n){
120  *p = '0';
121  return p;
122  }
123 
124  while (n){
125  *p-- = (n%10) + '0';
126  n/= 10;
127  }
128 
129  if (neg){
130  *p-- = '-';
131  }
132 
133  return ++p;
134 }
135 
136 /*---------------------------------------------------------------------------*/
137 
138 /**
139  * \brief This will check for DEBUG mode after power up.
140 */
141 void
143 {
144  uint8_t val;
145  if(0xFF == eeprom_read_byte(EEPROM_DEBUG_ADDR)){
146  /* Disable - Reverse logic. */
147  val = 1;
148  menu_debug_mode(&val);
149  }
150  else{
151  /* Enable - Reverse logic. */
152  val = 0;
153  menu_debug_mode(&val);
154  }
155 }
156 
157 /*---------------------------------------------------------------------------*/
158 
159 /**
160  * \brief This will start a sleep operation.
161  *
162  * \param val Used for remembering the new menu to display after a wakeup.
163 */
164 void
165 menu_run_sleep(uint8_t *val)
166 {
167  /* Turn off LED, LCD, ADC, Timer 1, SPI */
168  led_off();
169  lcd_deinit();
170  key_deinit();
171  PRR |= (1 << PRTIM1) | (1 << PRSPI);
172 
173  /* Tell the 1284P to turn off the radio and sleep */
174  sleep_count=0;
175  uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count);
176 
177  /* Turn off UART when transmission is complete */
178  while(!(UCSR0A & (1 << TXC0)));
179  _delay_us(10000); //deinit trash clears done flag on 1284p
180  uart_deinit();
181 
182  /* Go to sleep until button is pushed */
183  sleep_now(0);
184 
185  /* Yawn, waking up, turn on LCD with Raven Logo */
186  lcd_init();
187  lcd_symbol_set(LCD_SYMBOL_RAVEN);
188 
189  /* Disable interrupts before powering everything up */
190  cli();
191  key_init();
192  PRR &= ~((1 << PRTIM1) | (1 << PRSPI));
193  uart_init();
194 
195  /* Enable interrupts, Wake up 1284p and radio */
196  sei();
197  sleep_wakeup();
198 // uart_init();//flush receive buffer
199 
200  /* Wait for buttons up */
201  while (key_state_get() != KEY_NO_KEY)
202  ;
203  if (is_button()){
204  get_button();
205  }
206 }
207 /*---------------------------------------------------------------------------*/
208 
209 /**
210  * \brief This will start a sleep with wakes for temperature measurement and web requests.
211  *
212  * \param val Used for remembering the new menu to display after a wakeup.
213 */
214 void
215 menu_run_doze(uint8_t *val)
216 {
217  /* Turn off LED, LCD */
218  led_off();
219  lcd_deinit();
220 
221  /* Debounce */
222  while (key_state_get() != KEY_NO_KEY) ;
223 
224  /* Stay in doze loop until button is pressed*/
225  while (ENTER_PORT & (1<<ENTER_PIN)) {
226 
227  /* Tell 1284p to sleep for 4 seconds */
228  /* It will ignore the request if TCP/IP sessions are active */
229  /* Alter these timings as desired, or comment out to sleep only the 3290p */
230  sleep_count=4;
231  uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count);
232 
233  /* Wait for transmission complete, then sleep 3290p for 5 seconds */
234  while(!(UCSR0A & (1 << TXC0)));
235 // uart_deinit();
236  sleep_now(sleep_count+1);
237 // uart_init();
238 
239  /* 1284p should be awake by now, update temperature and give it time to process */
240  menu_send_temp();
241  _delay_us(20000);
242  }
243 
244  /* Wake LCD, turn on Raven logo */
245  lcd_init();
246  lcd_symbol_set(LCD_SYMBOL_RAVEN);
247  sleep_wakeup();
248  /* Wait for buttons up */
249  while (key_state_get() != KEY_NO_KEY)
250  ;
251  if (is_button()){
252  get_button();
253  }
254 }
255 
256 /*---------------------------------------------------------------------------*/
257 
258 /**
259  * \brief This will setup a ping request command to the 1284p and reset the ping counter.
260  *
261  * \param val place holder
262 */
263 void
264 menu_ping_request(uint8_t * val)
265 {
266  uint8_t i;
267  ping_mode = true;
268  ping_count = 0;
269  ping_response = 0;
270 
271  /* Initialize the numerical display with dashes */
272  for(i=0; i<4; i++){
273  lcd_single_print_dig(LCD_SEV_SEG_INDEX_MINUS, i);
274  }
275 
276  menu_send_ping();
277 
278  /* Reset the timer for 1 sec resolution between pings. */
279  TCNT1 = 0;
280 }
281 
282 /*---------------------------------------------------------------------------*/
283 
284 /**
285  * \brief This will send the ping request to the 1284p via the serial port.
286  *
287  * \return ping_count The number of ping attempts.
288 */
289 uint8_t
291 {
292  /*
293  * Check for previous ping timeout. If menu_send_ping() was called before receiving
294  * a response, update the LCD.
295  */
296  timeout_flag = true;
297  ping_count++;
298  /* Send the ping command with one byte payload of the current sequence number. */
299  uart_serial_send_frame(SEND_PING, 1, (uint8_t *)&ping_count);
300  return ping_count;
301 }
302 
303 /*---------------------------------------------------------------------------*/
304 
305 /**
306  * \brief This will stop the ping request.
307 */
308 void
310 {
311  ping_mode = false;
312 }
313 
314 /*---------------------------------------------------------------------------*/
315 
316 /**
317  * \brief This will enable or disable the JTAG debug interface to allow for
318  * proper temperature sensor readings.
319  *
320  * \param val Flag to trigger the proper debug mode.
321 */
322 void
323 menu_debug_mode(uint8_t *val)
324 {
325  uint8_t sreg = SREG;
326  cli();
327  if(*val){
328  /* Disable - Could use inline ASM to meet timing requirements. */
329  MCUCR |= (1 << JTD);
330  MCUCR |= (1 << JTD);
331  /* Needed for timing critical JTD disable. */
332  temp_init();
333  /* Store setting in EEPROM. */
334  eeprom_write_byte(EEPROM_DEBUG_ADDR, 0xFF);
335  }
336  else{
337  /* Enable - Could use inline ASM to meet timing requirements. */
338  MCUCR &= ~(1 << JTD);
339  MCUCR &= ~(1 << JTD);
340  /* Store setting in EEPROM. */
341  eeprom_write_byte(EEPROM_DEBUG_ADDR, 0x01);
342  }
343  SREG = sreg;
344 }
345 
346 /*---------------------------------------------------------------------------*/
347 
348 /**
349  * \brief This will display the temperature in degrees F or C.
350  *
351  * \param val Flag to trigger F or C temperature conversion.
352 */
353 void
354 menu_read_temp(uint8_t *val)
355 {
356  if(*val){
357  temp_mode = TEMP_UNIT_CELCIUS;
358  }
359  else{
360  temp_mode = TEMP_UNIT_FAHRENHEIT;
361  }
362 
363  temp_flag = true;
364 
366 }
367 
368 /*---------------------------------------------------------------------------*/
369 
370 /**
371  * \brief This will display the temperature in degrees F or C.
372 */
373 void
375 {
376  int16_t result = temp_get(temp_mode);
377 
378  /* Display the temp result on the lower 4 digit display with the proper symbol. */
379  if(temp_mode == TEMP_UNIT_CELCIUS){
380  lcd_symbol_clr(LCD_SYMBOL_F);
381  lcd_symbol_set(LCD_SYMBOL_C);
382  }
383  else{
384  lcd_symbol_clr(LCD_SYMBOL_C);
385  lcd_symbol_set(LCD_SYMBOL_F);
386  }
387 
388  /* Check for the DEBUG JTAG enable bit and display a CAUTION symbol to the user. */
389  /* CAUTION represents false value. */
390  if(MCUCR & 0x80){
391  lcd_symbol_clr(LCD_SYMBOL_ATT);
392  }
393  else{
394  lcd_symbol_set(LCD_SYMBOL_ATT);
395  }
396 
397  lcd_num_putdec(result, LCD_NUM_PADDING_SPACE);
398 }
399 
400 /*---------------------------------------------------------------------------*/
401 
402 /**
403  * \brief This will clear the temperature displayed in the 4 digit LCD segments.
404 */
405 void
407 {
408  temp_flag = false;
409  lcd_symbol_clr(LCD_SYMBOL_F);
410  lcd_symbol_clr(LCD_SYMBOL_C);
411  lcd_symbol_clr(LCD_SYMBOL_ATT);
412  lcd_num_clr();
413 }
414 
415 /*---------------------------------------------------------------------------*/
416 
417 /**
418  * \brief This will setup the current temperature for transfer to the ATmega1284p via a binary
419  * command transfer.
420  *
421  * \param val This is used to determine sending once or auto based on the timer.
422 */
423 void
424 menu_prepare_temp(uint8_t *val)
425 {
426  if(*val){
427  /* Only send the temp value once. */
428  auto_temp = false;
429  }
430  else{
431  /* Auto send the temp value based on TIMER1 interval. */
432  auto_temp = true;
433  }
434 
435  menu_send_temp();
436 }
437 
438 /*---------------------------------------------------------------------------*/
439 
440 /**
441  * \brief This will stop the auto sending of temperature data.
442 */
443 void
445 {
446  auto_temp = false;
447 }
448 
449 /*---------------------------------------------------------------------------*/
450 
451 /**
452  * \brief This will send the data via the serial port.
453 */
454 #if MEASURE_ADC2
455 extern uint16_t ADC2_reading;
456 #endif
457 void
458 menu_send_temp(void)
459 {
460  int16_t result;
461  uint8_t str[12];
462  uint8_t * p = 0;
463 
464  /* Turn on nose LED for activity indicator */
465  led_on();
466 
467  /* Get the latest temp value. */
468  result = temp_get(temp_mode);
469 
470  /* Convert signed decimal number to ASCII. */
471  p = signed_dectoascii(result, (str + 10));
472 
473  /* Send frame via serial port. */
474  uart_serial_send_frame(SEND_TEMP, 1+strlen((char *)p), p);
475 
476 #if MEASURE_ADC2
477  /* Send ADC2 via serial port. */
478  p = signed_dectoascii(ADC2_reading, (str + 10));
479  str[9]='m';str[10]='V';str[11]=0; //convert degrees to millivolts ;)
480  uart_serial_send_frame(SEND_ADC2, 1+strlen((char *)p), p);
481 #endif
482 
483  led_off();
484 }
485 
486 /** \} */