PIC24 Support Libraries
app_softmenu.c
1 // .. "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
2 // All rights reserved.
3 // (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
4 // (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
5 // (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
6 //
7 // Permission to use, copy, modify, and distribute this software and its
8 // documentation for any purpose, without fee, and without written agreement is
9 // hereby granted, provided that the above copyright notice, the following
10 // two paragraphs and the authors appear in all copies of this software.
11 //
12 // IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
13 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
14 // OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
15 // HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 //
17 // THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
20 // ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
21 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
22 //
23 // Please maintain this header in its entirety when copying/modifying
24 // these files.
25 //
26 // *******************************
27 // app_softmenu.c - soft menu demo
28 // *******************************
29 // ESOS application program to implement a simple soft menu on the LCD44780.
30 //
31 
32 
33 // INCLUDEs go here (First include the main esos.h file)
34 // After that, the user can include what they need
35 #include "esos.h"
36 #include "esos_lcd44780.h"
37 #include "string.h"
38 
39 #ifdef __linux
40 
41 #include "esos_pc.h"
42 #include "esos_pc_stdio.h"
43 // INCLUDE these so that printf() and our PC hacks work
44 #include <stdio.h>
45 #include <sys/select.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 #else
50 
51 #include "esos_pic24.h"
52 #include "esos_pic24_rs232.h"
53 
54 #endif
55 
56 // DEFINEs go here
57 #ifndef __linux
58 
59 // These defines should be replaced by hardware-appropriate
60 // definitions.
61 #define CONFIG_LED3() printf("Called CONFIG_LED3()\n");
62 uint8_t LED3 = FALSE; // LED3 is initially "off"
63 #define CONFIG_SW1() printf("called CONFIG_SW1()");
64 #define CONFIG_SW2() printf("called CONFIG_SW2()");
65 uint8_t SW1 = FALSE;
66 #define SW1_PRESSED() ( SW1 == 0 )
67 #define SW1_RELEASED() ( SW1 == 1 )
68 uint8_t SW2 = FALSE;
69 #define SW2_PRESSED() ( SW2 == 0 )
70 #define SW2_RELEASED() ( SW2 == 1 )
71 
72 #else
73 
74 #define CONFIG_LED3() printf("Called CONFIG_LED3()\n");
75 uint8_t LED3 = FALSE; // LED3 is initially "off"
76 #define CONFIG_SW1() printf("called CONFIG_SW1()");
77 #define CONFIG_SW2() printf("called CONFIG_SW2()");
78 
79 #endif
80 
81 // PROTOTYPEs go here
82 
83 // GLOBALs go here
84 // Generally, the user-created semaphores will be defined/allocated here
85 typedef struct {
86  char ac_line1[8];
87  char ac_line2[8];
88 } menu_item_t;
89 
90 typedef struct {
91  uint8_t u8_numItems;
92  uint8_t u8_choice;
93  menu_item_t ast_items[];
94 } menu_t;
95 
96 menu_t main_menu = {
97  .u8_numItems = 3,
98  .u8_choice = 0,
99  .ast_items = {
100  { "Embedded",
101  "Systems" },
102  { "J.W.",
103  "Bruce" },
104  { "Drew",
105  "Taylor" },
106  }
107 };
108 
109 #ifdef __linux
110 /*
111  * Simulate the timer ISR found on a MCU
112  * The PC doesn't have a timer ISR, so this task will periodically
113  * call the timer services callback instead.
114  * USED ONLY FOR DEVELOPMENT AND TESTING ON PC.
115  * Real MCU hardware doesn't need this task
116  */
117 ESOS_USER_TASK( __simulated_isr ) {
118  ESOS_TASK_BEGIN();
119  while (TRUE) {
120  // call the ESOS timer services callback just like a real H/W ISR would
121  __esos_tmrSvcsExecute();
123 
124  } // endof while(TRUE)
125  ESOS_TASK_END();
126 } // end child_task
127 #endif
128 
129 /************************************************************************
130  * User supplied functions
131  ************************************************************************
132  */
133 
134 /*
135  * An ESOS task to mimic the heartbeat LED found
136  * in the PIC24 support library code used in Chapters 8-13.
137  *
138  * Toggle LED1, wait 250ms, repeat forever.
139  *
140  * \note Since this heartbeat is performed in an ESOS task,
141  * a flashing LED indicates that the ESOS scheduler is still
142  * running properly. If the LED quits flashing, the ESOS
143  * scheduler is no longer rotating through the runnable task
144  * list. The most likely reason is that some task has ceased
145  * "yielding" the processor, and is caught in some deadlock
146  * or otherwise infinite loop.
147  * \hideinitializer
148  */
149 ESOS_USER_TASK(heartbeat_LED) {
150  ESOS_TASK_BEGIN();
151  while (TRUE) {
152  LED3 = !LED3;
153 
154 #ifdef __linux
155  if (LED3) {
156  printf("\a");
157  fflush(stdout);
158  }
159 #endif
160 
161  ESOS_TASK_WAIT_TICKS( 500 );
162  } // endof while(TRUE)
163  ESOS_TASK_END();
164 } // end heartbeat_LED task
165 
166 /*
167  * An ESOS task to update the LCD44780 structure based on the selected
168  * menu choice.
169  * \hideinitializer
170  */
171 ESOS_USER_TASK ( soft_menu_task ) {
172  int i;
173 
174  ESOS_TASK_BEGIN();
175 
176  while (TRUE) {
177  esos_lcd44780_clearScreen();
178  for (i = 0; i < strlen(main_menu.ast_items[main_menu.u8_choice].ac_line1); i++) {
179  esos_lcd44780_writeChar(0,i,main_menu.ast_items[main_menu.u8_choice].ac_line1[i]);
180  }
181  for (i = 0; i < strlen(main_menu.ast_items[main_menu.u8_choice].ac_line2); i++) {
182  esos_lcd44780_writeChar(1,i,main_menu.ast_items[main_menu.u8_choice].ac_line2[i]);
183  }
184 
185  ESOS_TASK_YIELD();
186  }
187 
188  ESOS_TASK_END();
189 }
190 
191 /*
192  * An ESOS task to update the menu choice based on switch activity.
193  * \hideinitializer
194  */
195 ESOS_USER_TASK ( selection_handler ) {
196  ESOS_TASK_BEGIN();
197 
198  while ( TRUE ) {
199  if (SW1_PRESSED()) {
200  ESOS_TASK_WAIT_UNTIL(SW1_RELEASED());
201  if (main_menu.u8_choice == 0) {
202  main_menu.u8_choice = 2;
203  } else {
204  main_menu.u8_choice = main_menu.u8_choice - 1;
205  }
206  } else if (SW2_PRESSED()) {
207  ESOS_TASK_WAIT_UNTIL(SW2_RELEASED());
208  if (main_menu.u8_choice == 2) {
209  main_menu.u8_choice = 0;
210  } else {
211  main_menu.u8_choice = main_menu.u8_choice + 1;
212  }
213  }
214 
215  ESOS_TASK_YIELD();
216  }
217 
218  ESOS_TASK_END();
219 }
220 
221 /****************************************************
222  * user_init()
223  ****************************************************
224  */
225 void user_init(void) {
226 
227  // Call the hardware-provided routines to print the
228  // HELLO_MSG to the screen. Must use this call because
229  // the ESOS communications subsystems is not yet fully
230  // initialized, since this call is in user_init()
231  //
232  // In general, users should call hardware-specific
233  // function like this.
234 
235  __esos_unsafe_PutString( HELLO_MSG );
236 
237 #ifdef __linux
238  // register our little ESOS task to mimic MCU's TIMER T1 IRQ which kicks off
239  // the ESOS S/W timers when they expire
240  esos_RegisterTask( __simulated_isr );
241 #endif
242 
243  // configure our hardware as needed by the tasks
244  CONFIG_LED3();
245  CONFIG_SW1();
246  CONFIG_SW2();
247  esos_lcd44780_configDisplay();
248  esos_lcd44780_init();
249 
250  // user_init() should register at least one user task
251  esos_RegisterTask( heartbeat_LED );
252  esos_RegisterTask( soft_menu_task );
253  esos_RegisterTask( selection_handler );
254 
255 } // end user_init()
This file contains macros, prototypes, and definitions for Microchip PIC24 Family specific communicat...
ESOS_TASK_HANDLE esos_RegisterTask(uint8_t(*taskname)(ESOS_TASK_HANDLE pstTask))
Definition: esos.c:86
#define ESOS_TASK_YIELD()
Definition: esos_task.h:590
void user_init(void)
This is the master include file for implementing ESOS on Microchip PIC24 MCUs.
#define ESOS_TASK_WAIT_TICKS(u32_duration)
Definition: esos_task.h:376
#define ESOS_TASK_END()
Definition: esos_task.h:272
#define ESOS_TASK_WAIT_UNTIL(condition)
Definition: esos_task.h:336
#define ESOS_TASK_BEGIN()
Definition: esos_task.h:260
ESOS_USER_TASK(CANFactory)
Definition: esos_ecan.c:70
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194