Contiki 3.x
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 will reliably set or clear the JTD bit of the MCUCR register.
71  *
72  * \param x True to set the JTD bit disabling JTAG.
73 */
74 #define jtd_set(x)\
75 {\
76  __asm__ __volatile__ (\
77  "in __tmp_reg__,__SREG__" "\n\t"\
78  "cli" "\n\t"\
79  "out %1, %0" "\n\t"\
80  "out __SREG__, __tmp_reg__" "\n\t"\
81  "out %1, %0" "\n\t"\
82  : /* no outputs */\
83  : "r" ((uint8_t)(x ? (1<<JTD) : 0)),\
84  "M" (_SFR_IO_ADDR(MCUCR))\
85  : "r0");\
86 }
87 
88 /*---------------------------------------------------------------------------*/
89 
90 /**
91  * \brief This function will convert decimal to ascii.
92  *
93  * \param val Decimal value to convert.
94  * \param str Address location to store converted value.
95 */
96 void
97 dectoascii(uint8_t val, char *str)
98 {
99  *(str+1) = (val % 10) + '0';
100  *str = (val / 10) + '0';
101 }
102 
103 /*---------------------------------------------------------------------------*/
104 
105 /**
106  * \brief This will convert a signed decimal number to ASCII.
107  *
108  * \param n Signed number
109  * \param str Pointer to store converted value.
110  *
111  * \return *p Address of stored conversion.
112 */
113 uint8_t
114 *signed_dectoascii(int16_t n, uint8_t *str)
115 {
116  uint8_t * p = str;
117  uint8_t neg = 0;
118 
119  if(n < 0){
120  neg = 1;
121  n = -n;
122  }
123 
124  *p-- = 0x00;
125 
126  /* Determine the unit of conversion. */
127  if(temp_mode == TEMP_UNIT_CELCIUS){
128  /* Add ASCII C to string. */
129  *p-- = 'C';
130  }
131  else{
132  /* Add ASCII F to string. */
133  *p-- = 'F';
134  }
135 
136  /* Add a space before unit symbol. */
137  *p-- = ' ';
138 
139  /* For zero, just print zero. */
140  if (!n){
141  *p = '0';
142  return p;
143  }
144 
145  while (n){
146  *p-- = (n%10) + '0';
147  n/= 10;
148  }
149 
150  if (neg){
151  *p-- = '-';
152  }
153 
154  return ++p;
155 }
156 
157 /*---------------------------------------------------------------------------*/
158 
159 /**
160  * \brief This will check for DEBUG mode after power up.
161 */
162 void
164 {
165  uint8_t val;
166  if(0xFF == eeprom_read_byte(EEPROM_DEBUG_ADDR)){
167  /* Disable - Reverse logic. */
168  val = 1;
169  menu_debug_mode(&val);
170  }
171  else{
172  /* Enable - Reverse logic. */
173  val = 0;
174  menu_debug_mode(&val);
175  }
176 }
177 
178 /*---------------------------------------------------------------------------*/
179 
180 /**
181  * \brief This will start a sleep operation.
182  *
183  * \param val Used for remembering the new menu to display after a wakeup.
184 */
185 void
186 menu_run_sleep(uint8_t *val)
187 {
188  /* Turn off LED, LCD, ADC, Timer 1, SPI */
189  led_off();
190  lcd_deinit();
191  key_deinit();
192  PRR |= (1 << PRTIM1) | (1 << PRSPI);
193 
194  /* Tell the 1284P to turn off the radio and sleep */
195  sleep_count=0;
196  uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count);
197 
198  /* Turn off UART when transmission is complete */
199  while(!(UCSR0A & (1 << TXC0)));
200  _delay_us(10000); //deinit trash clears done flag on 1284p
201  uart_deinit();
202 
203  /* Go to sleep until button is pushed */
204  sleep_now(0);
205 
206  /* Yawn, waking up, turn on LCD with Raven Logo */
207  lcd_init();
208  lcd_symbol_set(LCD_SYMBOL_RAVEN);
209 
210  /* Disable interrupts before powering everything up */
211  cli();
212  key_init();
213  PRR &= ~((1 << PRTIM1) | (1 << PRSPI));
214  uart_init();
215 
216  /* Enable interrupts, Wake up 1284p and radio */
217  sei();
218  sleep_wakeup();
219 // uart_init();//flush receive buffer
220 
221  /* Wait for buttons up */
222  while (key_state_get() != KEY_NO_KEY)
223  ;
224  if (is_button()){
225  get_button();
226  }
227 }
228 /*---------------------------------------------------------------------------*/
229 
230 /**
231  * \brief This will start a sleep with wakes for temperature measurement and web requests.
232  *
233  * \param val Used for remembering the new menu to display after a wakeup.
234 */
235 void
236 menu_run_doze(uint8_t *val)
237 {
238  /* Turn off LED, LCD */
239  led_off();
240  lcd_deinit();
241 
242  /* Debounce */
243  while (key_state_get() != KEY_NO_KEY) ;
244 
245  /* Stay in doze loop until button is pressed*/
246  while (ENTER_PORT & (1<<ENTER_PIN)) {
247 
248  /* Tell 1284p to sleep for 4 seconds */
249  /* It will ignore the request if TCP/IP sessions are active */
250  /* Alter these timings as desired, or comment out to sleep only the 3290p */
251  sleep_count=4;
252  uart_serial_send_frame(SEND_SLEEP, 1, (uint8_t *)&sleep_count);
253 
254  /* Wait for transmission complete, then sleep 3290p for 5 seconds */
255  while(!(UCSR0A & (1 << TXC0)));
256 // uart_deinit();
257  sleep_now(sleep_count+1);
258 // uart_init();
259 
260  /* 1284p should be awake by now, update temperature and give it time to process */
261  menu_send_temp();
262  _delay_us(20000);
263  }
264 
265  /* Wake LCD, turn on Raven logo */
266  lcd_init();
267  lcd_symbol_set(LCD_SYMBOL_RAVEN);
268  sleep_wakeup();
269  /* Wait for buttons up */
270  while (key_state_get() != KEY_NO_KEY)
271  ;
272  if (is_button()){
273  get_button();
274  }
275 }
276 
277 /*---------------------------------------------------------------------------*/
278 
279 /**
280  * \brief This will setup a ping request command to the 1284p and reset the ping counter.
281  *
282  * \param val place holder
283 */
284 void
285 menu_ping_request(uint8_t * val)
286 {
287  uint8_t i;
288  ping_mode = true;
289  ping_count = 0;
290  ping_response = 0;
291 
292  /* Initialize the numerical display with dashes */
293  for(i=0; i<4; i++){
294  lcd_single_print_dig(LCD_SEV_SEG_INDEX_MINUS, i);
295  }
296 
297  menu_send_ping();
298 
299  /* Reset the timer for 1 sec resolution between pings. */
300  TCNT1 = 0;
301 }
302 
303 /*---------------------------------------------------------------------------*/
304 
305 /**
306  * \brief This will send the ping request to the 1284p via the serial port.
307  *
308  * \return ping_count The number of ping attempts.
309 */
310 uint8_t
312 {
313  /*
314  * Check for previous ping timeout. If menu_send_ping() was called before receiving
315  * a response, update the LCD.
316  */
317  timeout_flag = true;
318  ping_count++;
319  /* Send the ping command with one byte payload of the current sequence number. */
320  uart_serial_send_frame(SEND_PING, 1, (uint8_t *)&ping_count);
321  return ping_count;
322 }
323 
324 /*---------------------------------------------------------------------------*/
325 
326 /**
327  * \brief This will stop the ping request.
328 */
329 void
331 {
332  ping_mode = false;
333 }
334 
335 /*---------------------------------------------------------------------------*/
336 
337 /**
338  * \brief This will enable or disable the JTAG debug interface to allow for
339  * proper temperature sensor readings.
340  *
341  * \param val Flag to trigger the proper debug mode.
342 */
343 void
344 menu_debug_mode(uint8_t *val)
345 {
346  if(val){
347  jtd_set(true);
348  temp_init();
349  /* Store setting in EEPROM. */
350  eeprom_write_byte(EEPROM_DEBUG_ADDR, 0xFF);
351  }
352  else{
353  jtd_set(false);
354  /* Store setting in EEPROM. */
355  eeprom_write_byte(EEPROM_DEBUG_ADDR, 0x01);
356  }
357  //SREG = sreg;
358 }
359 
360 /*---------------------------------------------------------------------------*/
361 
362 /**
363  * \brief This will display the temperature in degrees F or C.
364  *
365  * \param val Flag to trigger F or C temperature conversion.
366 */
367 void
368 menu_read_temp(uint8_t *val)
369 {
370  if(val){
371  temp_mode = TEMP_UNIT_CELCIUS;
372  }
373  else{
374  temp_mode = TEMP_UNIT_FAHRENHEIT;
375  }
376 
377  temp_flag = true;
378 
380 }
381 
382 /*---------------------------------------------------------------------------*/
383 
384 /**
385  * \brief This will display the temperature in degrees F or C.
386 */
387 void
389 {
390  int16_t result = temp_get(temp_mode);
391 
392  /* Display the temp result on the lower 4 digit display with the proper symbol. */
393  if(temp_mode == TEMP_UNIT_CELCIUS){
394  lcd_symbol_clr(LCD_SYMBOL_F);
395  lcd_symbol_set(LCD_SYMBOL_C);
396  }
397  else{
398  lcd_symbol_clr(LCD_SYMBOL_C);
399  lcd_symbol_set(LCD_SYMBOL_F);
400  }
401 
402  /* Check for the DEBUG JTAG enable bit and display a CAUTION symbol to the user. */
403  /* CAUTION represents false value. */
404  if(MCUCR & 0x80){
405  lcd_symbol_clr(LCD_SYMBOL_ATT);
406  }
407  else{
408  lcd_symbol_set(LCD_SYMBOL_ATT);
409  }
410 
411  lcd_num_putdec(result, LCD_NUM_PADDING_SPACE);
412 }
413 
414 /*---------------------------------------------------------------------------*/
415 
416 /**
417  * \brief This will clear the temperature displayed in the 4 digit LCD segments.
418 */
419 void
421 {
422  temp_flag = false;
423  lcd_symbol_clr(LCD_SYMBOL_F);
424  lcd_symbol_clr(LCD_SYMBOL_C);
425  lcd_symbol_clr(LCD_SYMBOL_ATT);
426  lcd_num_clr();
427 }
428 
429 /*---------------------------------------------------------------------------*/
430 
431 /**
432  * \brief This will setup the current temperature for transfer to the ATmega1284p via a binary
433  * command transfer.
434  *
435  * \param val This is used to determine sending once or auto based on the timer.
436 */
437 void
438 menu_prepare_temp(uint8_t *val)
439 {
440  if(*val){
441  /* Only send the temp value once. */
442  auto_temp = false;
443  }
444  else{
445  /* Auto send the temp value based on TIMER1 interval. */
446  auto_temp = true;
447  }
448 
449  menu_send_temp();
450 }
451 
452 /*---------------------------------------------------------------------------*/
453 
454 /**
455  * \brief This will stop the auto sending of temperature data.
456 */
457 void
459 {
460  auto_temp = false;
461 }
462 
463 /*---------------------------------------------------------------------------*/
464 
465 /**
466  * \brief This will send the data via the serial port.
467 */
468 #if MEASURE_ADC2
469 extern uint16_t ADC2_reading;
470 #endif
471 void
472 menu_send_temp(void)
473 {
474  int16_t result;
475  uint8_t str[12];
476  uint8_t * p = 0;
477 
478  /* Turn on nose LED for activity indicator */
479  led_on();
480 
481  /* Get the latest temp value. */
482  result = temp_get(temp_mode);
483 
484  /* Convert signed decimal number to ASCII. */
485  p = signed_dectoascii(result, (str + 10));
486 
487  /* Send frame via serial port. */
488  uart_serial_send_frame(SEND_TEMP, 1+strlen((char *)p), p);
489 
490 #if MEASURE_ADC2
491  /* Send ADC2 via serial port. */
492  p = signed_dectoascii(ADC2_reading, (str + 10));
493  str[9]='m';str[10]='V';str[11]=0; //convert degrees to millivolts ;)
494  uart_serial_send_frame(SEND_ADC2, 1+strlen((char *)p), p);
495 #endif
496 
497  led_off();
498 }
499 
500 /** \} */
void key_init(void)
This will intialize the joystick and the ADC for button readings.
Definition: key.c:57
Interface for the onboard temperature sensor.
int lcd_num_clr(void)
This will clear numbers displayed on the LCD.
Definition: lcd.c:428
void lcd_symbol_clr(lcd_symbol_t symbol)
This will clear any symbol on the Raven LCD.
Definition: lcd.c:566
int lcd_num_putdec(int numb, lcd_padding_t padding)
This will put a DEC value on the LCD that represents the input parameter.
Definition: lcd.c:402
uint8_t * signed_dectoascii(int16_t n, uint8_t *str)
This will convert a signed decimal number to ASCII.
Definition: menu.c:114
void menu_prepare_temp(uint8_t *val)
This will setup the current temperature for transfer to the ATmega1284p via a binary command transfer...
Definition: menu.c:438
void menu_display_temp(void)
This will display the temperature in degrees F or C.
Definition: menu.c:388
This file operates the menu flow chart described in the readme notes.
void menu_run_doze(uint8_t *val)
This will start a sleep with wakes for temperature measurement and web requests.
Definition: menu.c:236
uint16_t ADC2_reading
This will send the data via the serial port.
Definition: temp.c:166
This is the main file for the Raven LCD application.
void sleep_now(int howlong)
Prepares for and executes sleep.
Definition: sleep.c:68
int temp_init(void)
This will initialize the digital IO and adc channel for temperture readings.
Definition: temp.c:113
void uart_init(uint8_t uart)
Initialises the UART controller, configures I/O control and interrupts.
Definition: uart.c:244
void uart_deinit(void)
Turn off UART for sleep mode.
Definition: uart.c:241
key_state_t key_state_get(void)
This function will start the ADC conversion and read the current converstion value to determine the b...
Definition: key.c:174
void dectoascii(uint8_t val, char *str)
This function will convert decimal to ascii.
Definition: menu.c:97
void eeprom_init(void)
This will check for DEBUG mode after power up.
Definition: menu.c:163
void menu_run_sleep(uint8_t *val)
This will start a sleep operation.
Definition: menu.c:186
void menu_stop_temp(void)
This will stop the auto sending of temperature data.
Definition: menu.c:458
This file implements a beep function to emit a beep sound from Raven's speaker. ...
void lcd_single_print_dig(uint8_t numb, uint8_t pos)
This will add the passed in number to any of the four locations of the four digit segment display on ...
Definition: lcd.c:890
void lcd_symbol_set(lcd_symbol_t symbol)
This will enable any of the symbols on the Raven LCD.
Definition: lcd.c:541
void key_deinit(void)
This will disable the ADC used for button readings.
Definition: key.c:80
void menu_debug_mode(uint8_t *val)
This will enable or disable the JTAG debug interface to allow for proper temperature sensor readings...
Definition: menu.c:344
void uart_serial_send_frame(uint8_t cmd, uint8_t payload_length, uint8_t *payload)
This function builds and sends a binary command frame to the ATmega1284p.
Definition: uart.c:295
void menu_clear_temp(void)
This will clear the temperature displayed in the 4 digit LCD segments.
Definition: menu.c:420
This controls the sleep operation for the LCD.
uint8_t menu_send_ping(void)
This will send the ping request to the 1284p via the serial port.
Definition: menu.c:311
void menu_read_temp(uint8_t *val)
This will display the temperature in degrees F or C.
Definition: menu.c:368
void menu_ping_request(uint8_t *val)
This will setup a ping request command to the 1284p and reset the ping counter.
Definition: menu.c:285
Handles the control of the USART for communication with the ATmega1284p for sending commands...
void sleep_wakeup(void)
This will send a wakeup command to ATmega1284p It may already be awake, if not it will respond during...
Definition: sleep.c:122
int16_t temp_get(temp_unit_t unit)
Read current temperature.
Definition: temp.c:170
uint8_t is_button(void)
This will poll run key_task() to determine if a button has been pressed.
Definition: key.c:95
uint8_t get_button(void)
This function will wait for a user to press a button.
Definition: key.c:114
void lcd_deinit(void)
This will disable the LCD operation.
Definition: lcd.c:325
This file provides joystick operations.
#define jtd_set(x)
This will reliably set or clear the JTD bit of the MCUCR register.
Definition: menu.c:74
int lcd_init(void)
This function will initialize the proper settings for the LCD driver.
Definition: lcd.c:277
void menu_stop_ping(void)
This will stop the ping request.
Definition: menu.c:330