001 //#define LCD_ControlModule
002 /****************************************************************************
003  Module
004    LCD_Control.c
005 
006  Revision
007    1.0.1
008 
009  Description
010    This is the driver file for the LCD tester module for LCD_Control 
011    
012 
013  Notes
014 
015  History
016  When           Who     What/Why
017  -------------- ---     --------
018  10/13/15 08:05 jec      began conversion from TemplateService
019  01/16/12 09:58 jec      began conversion from TemplateFSM.c
020 ****************************************************************************/
021 /*----------------------------- Include Files -----------------------------
022 */
023 /* include header files for this state machine as well as any machines at 
024 the
025    next lower level in the hierarchy that are sub-machines to this machine
026 */
027 #include "ES_Configure.h"
028 #include "ES_Framework.h"
029 #include "LCD_Control.h"
030 #include "LCDService.h"
031 #include "LCD_Write.h"
032 
033 // these times assume a 1.000mS/tick timing
034 #define ONE_SEC 976
035 #define HALF_SEC (ONE_SEC/8)
036 #define TWO_SEC (ONE_SEC*2)
037 #define FIVE_SEC (ONE_SEC*5)
038 
039 /*----------------------------- Module Defines ----------------------------
040 */
041 // mask for the number of active bits in the array of messages
042 #define CHOOSE_MSG_MASK 0x01
043 #define UNPAIR_MODE 0
044 #define PAIR_MODE 1
045 /*---------------------------- Module Functions ---------------------------
046 */
047 /* prototypes for private functions for this service.They should be 
048 functions
049    relevant to the behavior of this service
050 */
051 static uint8_t GetNextMessageChar(void);
052 void PrintLine(int16_t data, int mode);
053 
054 /*---------------------------- Module Variables ---------------------------
055 */
056 // with the introduction of Gen2, we need a module level Priority variable
057 static uint8_t MyPriority;
058 static uint8_t WhichMessage;
059 //static char temp_line[17] = "ANAN ZAA________"; //hold the line to be 
060 printed by println function
061 //try empty init
062 static char temp_line[17]="";
063 
064 static uint8_t CurrentIndex = 0; //holds the current char index of the 
065 printed line
066 
067 
068 //fake gyro data to test with
069 static int16_t fakeGyroData=50; //decrement it by 100 every time to test the 
070 signs
071   static bool IsTimerActive = false;
072 
073 //#ifdef LCD_ControlModule
074 //static char TestingMessage[][80]={
075 //  "this is message 0", 
076 //  "This is message 1"
077 //};
078 //#define WHICH_MSG_INDEX [WhichMessage]
079 //#else //This must be the tester version
080 //static char TestingMessage[]=
081 //  "If you can read this message then you have built your hardware 
082 correctly ";
083 //#define WHICH_MSG_INDEX 
084 //#endif
085 /*------------------------------ Module Code ------------------------------
086 */
087 /****************************************************************************
088  Function
089      LCD_ControlInit
090 
091  Parameters
092      uint8_t : the priorty of this service
093 
094  Returns
095      bool, false if error in initialization, true otherwise
096 
097  Description
098      Saves away the priority, and does any 
099      other required initialization for this service
100  Notes
101 
102  Author
103      J. Edward Carryer, 10/13/15, 08:07
104 ****************************************************************************/
105 bool LCD_ControlInit ( uint8_t Priority )
106 {
107   ES_Event ThisEvent;
108 
109   MyPriority = Priority;
110   /********************************************
111    in here you write your initialization code
112    *******************************************/
113 	if(IsTimerActive == false){
114 		printf("enter timer not active\n\r");
115 		PrintLine(0, UNPAIR_MODE); //line_to_print is unused
116 		ES_Timer_InitTimer(LCD_PUMP_TIMER, 100); 
117 		IsTimerActive = true;
118 	}
119 	
120   // post the initial transition event
121   ThisEvent.EventType = ES_INIT;
122   if (ES_PostToService( MyPriority, ThisEvent) == true)
123   {
124 		printf("LCD_Control init done\n\r");
125       return true;
126   }else
127   {
128       return false;
129   }
130 }
131 
132 /****************************************************************************
133  Function
134      PostLCD_Control
135 
136  Parameters
137      EF_Event ThisEvent ,the event to post to the queue
138 
139  Returns
140      bool false if the Enqueue operation failed, true otherwise
141 
142  Description
143      Posts an event to this state machine's queue
144  Notes
145 
146  Author
147      J. Edward Carryer, 10/13/15, 08:10
148 ****************************************************************************/
149 bool PostLCD_Control( ES_Event ThisEvent )
150 {
151   return ES_PostToService( MyPriority, ThisEvent);
152 }
153 
154 /****************************************************************************
155  Function
156     LCD_ControlRun
157 
158  Parameters
159    ES_Event : the event to process
160 
161  Returns
162    ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
163 
164  Description
165    Run function for the LCD character pumping service
166  Notes
167    
168  Author
169    J. Edward Carryer, 10/13/15, 08:11
170 ****************************************************************************/
171 ES_Event LCD_ControlRun( ES_Event ThisEvent )
172 {
173   ES_Event ReturnEvent;
174   ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
175 
176 // when we get a keystroke, we read the timer to get a random number
177 // to use to choose which message to print and then start printing  
178   if(ThisEvent.EventType == ES_NEW_KEY){
179 			printf("LCD_Control.c newkey\n\r");
180 			if(IsTimerActive == false){
181 				printf("enter timer not active\n\r");
182 		
183 				//WhichMessage = (ES_Timer_GetTime() & CHOOSE_MSG_MASK);
184 				//char* line_to_print = "HI GEORGE";
185 				PrintLine((int16_t) ThisEvent.EventParam, PAIR_MODE); //line_to_print is 
186 				unused
187 				
188 				//change the data to print,update data, can be replaced by a query
189 				fakeGyroData=fakeGyroData-2000;
190 				
191 				
192 				ES_Timer_InitTimer(LCD_PUMP_TIMER, 2); //2ms is really fast,practically 
193 				instant, barely noticeable
194 				IsTimerActive = true;
195 			}
196   }
197 	
198 	else if(ThisEvent.EventType == PRINT_UNPAIR){
199 			printf("LCD_Control.c unpair received\n\r");
200 			if(IsTimerActive == false){
201 				printf("enter timer not active\n\r");
202 				PrintLine(0, UNPAIR_MODE); //line_to_print is unused
203 				ES_Timer_InitTimer(LCD_PUMP_TIMER, 100); 
204 				IsTimerActive = true;
205 			}
206 	}
207 	
208   else if(ThisEvent.EventType == ES_TIMEOUT){
209 		//printf("LCD_Control.c timeout\n\r");
210     ES_Event ThisEvent;
211     ThisEvent.EventType = ES_LCD_PUTCHAR;
212     ThisEvent.EventParam = GetNextMessageChar();
213     PostLCDService(ThisEvent);
214     //ES_Timer_InitTimer(LCD_PUMP_TIMER, HALF_SEC);
215 		if (CurrentIndex != 23) {
216 			ES_Timer_InitTimer(LCD_PUMP_TIMER, 2); //23 = 15+8, 8 for shifting into 
217 			the shift register
218 		}
219 			else {
220 				CurrentIndex = 0;
221 				IsTimerActive=false; //timer not active after last character is printed
222 				
223 			}
224   } 
225 	
226   return ReturnEvent;
227 }
228 
229 /***************************************************************************
230  private functions
231  ***************************************************************************/
232 /****************************************************************************
233  Function
234     GetNextMessageChar
235 
236  Parameters
237    nothing
238 
239  Returns
240    the next charcter to be printed
241 
242  Description
243    steps through the message array and wraps around when it gets to the end.
244  Notes
245    
246  Author
247    J. Edward Carryer, 10/13/15, 08:16
248 ****************************************************************************/
249 static uint8_t GetNextMessageChar(void){
250   //static uint8_t CurrentIndex = 0;
251   //char CurrentChar = TestingMessage WHICH_MSG_INDEX[CurrentIndex++];
252   
253   //if (TestingMessage WHICH_MSG_INDEX[CurrentIndex] == 0){
254   //  CurrentIndex=0;
255   //}
256 	char CurrentChar = temp_line[CurrentIndex%16];
257 	CurrentIndex++;
258 	//if (CurrentIndex == 15)
259 	//	CurrentIndex = 0;
260   return CurrentChar;
261 }
262 
263 /****************************************************************************
264  Function
265     PrintLine
266 
267  Parameters
268    char* LineToPrint
269 
270  Returns
271    the next charcter to be printed
272 
273  Description
274    Loads the line to be printed into the temporary line string.
275  Notes
276    
277  Author
278    J. Edward Carryer, 10/13/15, 08:16
279 ****************************************************************************/
280 void PrintLine(int16_t data, int mode){
281 	//LineToPrint is unused    
282 	//sprintf(temp_line,"%-16s", LineToPrint);
283 	if(mode == UNPAIR_MODE){
284 			sprintf(temp_line, "Farmer Unpaired ");
285 	}
286 	else if(mode == PAIR_MODE){
287 		sprintf(temp_line,"Gyro Z: %5d   ", data);
288 	}
289   return;
290 }
291 
292 /*------------------------------- Footnotes -------------------------------
293 */
294 /*------------------------------ End of file ------------------------------
295 */
296