OSDN Git Service

Remove all samples. They will work well and useful for reference. But there is no...
[nxt-jsp/etrobo-atk.git] / nxtOSEK / ecrobot / c / ecrobot_base.c
1 /*****************************************************************************
2  * FILE: ecrobot_base.c
3  *
4  * COPYRIGHT 2008 Takashi Chikamasa <takashic@cybernet.co.jp>
5  *
6  * <About leJOS NXJ>
7  *  leJOS NXJ is a full firmware replacement of LEGO Mindstorms NXT and 
8  *  designed for Java programming environment for the NXT 
9  *  ( For more detailed information, please see: http://lejos.sourceforge.net/ )
10  *  In the leJOS NXJ distribution, C source files for NXT platform layer is also
11  *  included besides with the Java VM. The platform C source code is well
12  *  structured, comprehensive, and achieved higher performance than the LEGO's
13  *  one. Therefore, leJOS NXJ (platform) is also the best GCC based C/C++  
14  *  development platform for NXT.
15  *
16  *  The contents of this file are subject to the Mozilla Public License
17  *  Version 1.0 (the "License"); you may not use this file except in
18  *  compliance with the License. You may obtain a copy of the License at
19  *  http://www.mozilla.org/MPL/
20  *
21  *  Software distributed under the License is distributed on an "AS IS"
22  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
23  *  License for the specific language governing rights and limitations
24  *  under the License.
25  *
26  *  The Original Code is TinyVM code, first released March 6, 2000,
27  *  later released as leJOS on September 23, 2000.
28  *
29  *  The Initial Developer of the Original Code is Jose H. Solorzano.
30  *
31  *  Contributor(s): see leJOS NXJ ACKNOWLEDGEMENTS .
32  *
33  *
34  * <About TOPPERS OSEK>
35  *  TOPPERS OSEK is an open source OSEK kernel and developed by TOPPERS project.
36  *  TOPPERS(Toyohashi OPen Platform for Embedded Real-time Systems) has been managed 
37  *  by a Non Profit Organization founded in Sep. 2003 and has been led by Professor
38  *  Hiroaki Takada of Nagoya University in Japan. 
39  *
40  *  TOPPERS OSEK program is covered by the TOPPERS License as published
41  *  by the TOPPERS PROJECT (http://www.toppers.jp/en/index.html).
42  *
43  *****************************************************************************/
44
45 #include <string.h>
46
47 #include "ecrobot_base.h"
48 #include "ecrobot_private.h"
49 #include "ecrobot_interface.h"
50
51 #ifdef NXT_JSP
52 extern void lejos_osek_stop(void);
53 #endif
54
55 /* C++ constructor/destructor table */
56 extern void (*__ctor_begin[])(void);
57 extern void (*__ctor_end)(void);
58 extern void (*__dtor_begin[])(void);
59 extern void (*__dtor_end)(void);
60
61 /* declarations for splash screen BMP file */
62 #ifdef NXT_JSP
63 EXTERNAL_BMP_DATA(nxtjsp_splash);
64 #else
65 EXTERNAL_BMP_DATA(nxtosek_splash);
66 #endif
67
68 /* used for getting the address of a constant data */
69 const UINT exec_addr[1] = {0};
70
71 static CHAR status_bar[MAX_STATUS_BAR_LENGTH];
72 static SINT status_bar_position = 0;
73
74 /* flag to notify RTOS is started */
75 static volatile SINT OS_flag;
76
77 void init_OS_flag(void)
78 {
79         OS_flag = 0;
80 }
81
82 void set_OS_flag(void)
83 {
84         OS_flag = 1;
85 }
86
87 SINT get_OS_flag(void)
88 {
89         return OS_flag;
90 }
91
92 /*
93  * execute C++ constructors
94  */
95 void cpp_constructor(void)
96 {
97     SINT i;
98
99     /* call all global object constructors */
100     for (i=0;__ctor_begin[i] != __ctor_end;i++)
101       (*__ctor_begin[i])();
102 }
103
104 /*
105  * execute C++ destructors
106  */
107 void cpp_destructor(void)
108 {
109     SINT i;
110
111     /* call all global object destructors */
112     for (i=0;__dtor_begin[i] != __dtor_end;i++)
113       (*__dtor_begin[i])();
114 }
115
116 /*
117  * return where the program is executed
118  */
119 SINT execution_mode(void)
120 {
121         if (exec_addr < (volatile UINT *)0x200000)
122         {
123                 return EXECUTED_FROM_FLASH;
124         }
125         return EXECUTED_FROM_SRAM;
126 }
127
128 /*
129  * display application splash screen which is designed in
130  * ecrobot_splash.bmp file
131  */
132 void show_splash_screen(void)
133 {
134         S32 row, column;
135         S32 index;
136         static U8 lcd[NXT_LCD_DEPTH*NXT_LCD_WIDTH];
137         
138         /* convert bmp file data to an array data for LCD */
139 #ifdef NXT_JSP
140         ecrobot_bmp2lcd(BMP_DATA_START(nxtjsp_splash), lcd, 100, 64);
141 #else
142         ecrobot_bmp2lcd(BMP_DATA_START(nxtosek_splash), lcd, 100, 64);
143 #endif
144         
145         for (column = 0; column < NXT_LCD_WIDTH; column++)
146         {
147                 for (row = 0; row < NXT_LCD_DEPTH; row++)
148                 {
149                         index = row * NXT_LCD_WIDTH + column;
150                         lcd[index] = ~lcd[index];
151                 }
152                 display_clear(0);
153                 display_bitmap_copy(lcd, 100, 8, 0, 0);
154                 display_update();
155                 systick_wait_ms(800/100);
156         }
157         
158         if (execution_mode() == EXECUTED_FROM_SRAM)
159         {
160                 /* flip back to the original data for re-start */
161                 for (column = 0; column < NXT_LCD_WIDTH; column++)
162                 {
163                         for (row = 0; row < NXT_LCD_DEPTH; row++)
164                         {
165                                 index = row * NXT_LCD_WIDTH + column;
166                                 lcd[index] = ~lcd[index];
167                         }
168                 }
169         }
170         systick_wait_ms(200);
171 }
172
173 /*
174  * display application main screen
175  */
176 void show_main_screen(void)
177 {
178         display_clear(0);
179         display_goto_xy(0, 5);
180         display_string("================");
181         display_goto_xy(0, 6);
182         display_string("<STP  ENTR  RUN>");
183         display_goto_xy(0, 7);
184         display_string("      EXIT      ");
185         display_update();
186 }
187
188 /*
189  * display status bar in the main screen
190  * 
191  * clear: 1(clear status bar), 0(not clear status bar)
192  */
193 void display_status_bar(SINT clear)
194 {
195         if (clear)
196         {
197                 memset(status_bar, ' ', MAX_STATUS_BAR_LENGTH);
198                 status_bar_position = 0;
199         }
200
201         display_goto_xy(0, 3);
202         display_string("BATT:");
203         display_unsigned(ecrobot_get_battery_voltage()/100, 0);
204         display_goto_xy(0, 4);
205         display_string(status_bar);
206         display_update();
207 }
208
209 void show_bd_addr(U8 *bd_addr)
210 {
211         SINT i;
212
213         if (!get_OS_flag())
214         {
215                 display_goto_xy(0, 0);
216                 display_string("BD_ADDR:");
217                 display_goto_xy(0, 1);
218                 for (i = 0; i < 7; i++)
219                 {
220                         display_hex(bd_addr[i], 2);
221                 }
222                 display_update();
223         }
224 }
225
226 void show_bd_addr_err(void)
227 {
228         if (!get_OS_flag())
229         {
230                 display_goto_xy(0, 0);
231                 display_string("BD_ADDR: FAILED");
232                 display_goto_xy(0, 1);
233                 display_string("RESTART NXT.");
234                 display_update();
235         }
236 }
237
238 /*
239  * add information to the status bar
240  */
241 void add_status_info(SINT status)
242 {
243         CHAR *str;
244         
245         if (!get_OS_flag())
246         {
247                 switch(status)
248                 {
249                         case EXECUTED_FROM_FLASH:
250                                 str = "[F]";
251                                 break;
252                         case EXECUTED_FROM_SRAM:
253                                 str = "[R]";
254                                 break;
255                         case BT_CONNECTED:
256                                 str = "[BT]";
257                                 break;
258                         default:
259                                 return;
260                 }
261
262                 if ((status_bar_position + strlen(str)) <= MAX_STATUS_BAR_LENGTH)
263                 {
264                         strcpy(&status_bar[status_bar_position], (const CHAR *)str);
265                         status_bar_position += strlen(str);
266                 }
267         }
268 }
269
270 /*
271  * read buttons status on the NXT. This function is assumed to be executed
272  * every 1msec
273  */
274 void check_NXT_buttons(void)
275 {
276         U32 st;
277         static U32 last_act_time = 0;
278
279         /* check the buttons every 10msec */
280         st = systick_get_ms();
281         if (st >= last_act_time + 10) 
282         {
283                 last_act_time = st;
284                 ecrobot_poll_nxtstate();
285                 if (ecrobot_get_button_state() == STOP_PRESSED)
286                 {
287                         cpp_destructor();
288                         ecrobot_device_terminate();
289                         display_clear(0);
290                         display_force_update();
291                         systick_wait_ms(10);
292                         nxt_lcd_power_down(); /* reset LCD hardware */
293                         systick_wait_ms(10);
294
295                         /* jump to the entry of application */
296 #ifdef NXT_JSP
297                         lejos_osek_stop();
298 #else
299                         start();
300 #endif
301                 }
302                 else if (ecrobot_get_button_state() == EXIT_PRESSED)
303                 {
304                         cpp_destructor();
305                         ecrobot_device_terminate();
306                         display_clear(0);
307                         display_force_update();
308                         systick_wait_ms(10);
309                         nxt_lcd_power_down(); /* reset LCD hardware */
310                         systick_wait_ms(10);
311                         nxt_avr_power_down(); /* send command to AVR to sleep ARM */
312                 }
313         }
314 }
315