kmain.c
Go to the documentation of this file.
1 
6 /*
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.0 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS"
13  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
14  * License for the specific language governing rights and limitations
15  * under the License.
16  *
17  * The Original Code is legOS code, released October 17, 1999.
18  *
19  * The Initial Developer of the Original Code is Markus L. Noga.
20  * Portions created by Markus L. Noga are Copyright (C) 1999
21  * Markus L. Noga. All Rights Reserved.
22  *
23  * Contributor(s): Markus L. Noga <markus@noga.de>
24  * Kekoa Proudfoot <kekoa@graphics.stanford.edu>
25  * Lou Sortman <lou (at) sunsite (dot) unc (dot) edu>
26  */
27 
28 #include <sys/mm.h>
29 #include <sys/time.h>
30 #include <sys/tm.h>
31 #include <sys/irq.h>
32 #include <sys/lcd.h>
33 #include <sys/dsensor.h>
34 #include <sys/dmotor.h>
35 #include <sys/dsound.h>
36 #include <sys/lnp.h>
37 #include <sys/lnp-logical.h>
38 #include <sys/program.h>
39 #ifdef CONF_AUTOSHUTOFF
40 #include <sys/timeout.h>
41 #endif
42 #include <rom/system.h>
43 
44 #include <dbutton.h>
45 
46 #include <string.h>
47 #include <conio.h>
48 #include <unistd.h>
50 //
51 // Variables
52 //
54 
56 
58 unsigned char *firmware_string = "Do you byte, when I knock?";
59 
60 extern char __bss;
61 extern char __bss_end;
62 
64 extern char __text_hi, __etext_hi;
65 
66 #if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
67 static const note_t on_sound[]={{PITCH_G4, 1}, {PITCH_G5, 1}, {PITCH_END, 0}};
68 static const note_t off_sound[]={{PITCH_C4, 1}, {PITCH_C3, 1}, {PITCH_END, 0}};
69 #endif
70 
72 //
73 // Functions
74 //
76 
77 extern int main(int argc, char **argv);
78 
79 #if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
80 static void onOffSound(const note_t *snd) {
82  dsound_play(snd);
83 }
84 #endif
85 
87 extern inline void show_on(void) {
88  cls();
89 
90 #ifdef CONF_ASCII
91  cputs("ON");
92 #else
93 #ifdef CONF_CONIO
94  cputc_native_4(0x38);
95  cputc_native_3(0x3d);
96  cputc_native_2(0x7c);
97  cputc_native_1(0x7e);
98  cputc_native_0(0x6d);
99 #else
100  lcd_digit(1);
101 #endif
102 #endif
103 #ifndef CONF_LCD_REFRESH
104  lcd_refresh();
105 #endif
106 #if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
107  onOffSound(on_sound);
108 #endif
109  delay(250);
110 #if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
112 #endif
113 }
114 
116 extern inline void show_off(void) {
117  cls();
118 
119 #ifdef CONF_ASCII
120  cputs("OFF");
121 #else
122 #ifdef CONF_CONIO
123  cputc_native_4(0x7e);
124  cputc_native_3(0x1d);
125  cputc_native_2(0x1d);
126 #else
127  lcd_digit(0);
128 #endif
129 #endif
130 #ifndef CONF_LCD_REFRESH
131  lcd_refresh();
132 #endif
133 #if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
134  onOffSound(off_sound);
135 #endif
136 }
137 
138 
140 
142 void kmain(void) __attribute__((noreturn));
143 void kmain(void)
144 {
145  int reset_after_shutdown=0;
146 #ifdef CONF_DKEY
147  int c;
148 #endif
149 
150  /* Install the text.hi segment in the correct place. The
151  * firmware loader puts it in the bss segment, we copy it
152  * to it's final location.
153  */
155 
157 
158  /* Turn off motor, since writing to hitext manipulates motors */
159  motor_controller = 0;
160 
161  memset(&__bss, 0, &__bss_end - &__bss);
162 
163 #ifdef CONF_MM
164  mm_init();
165 #endif
166 
167  while (1) {
168  power_init();
169 
170 #ifdef CONF_AUTOSHUTOFF
171  shutoff_init();
172 #endif
173 
174  lcd_init();
175 
176 #ifdef CONF_DSOUND
177  dsound_init();
178 #endif
179 #ifdef CONF_TIME
180  systime_init();
181 #endif
182 #ifdef CONF_DSENSOR
183  ds_init();
184 #endif
185 #ifdef CONF_DMOTOR
186  dm_init();
187 #endif
188 #ifdef CONF_LNP
189  lnp_init();
191 #endif
192 #ifdef CONF_TM
193  tm_init();
194 #endif
195 #ifdef CONF_PROGRAM
196  program_init();
197 #endif
198 
199  show_on();
200 
201  // wait till power key released
202  //
203 #ifdef CONF_DKEY
205  while((c=dkey_multi) & KEY_ONOFF);
206 #else
207  while (PRESSED(dbutton(), BUTTON_ONOFF));
208  delay(100);
209 #endif
210 
211  cls();
212 #ifndef CONF_PROGRAM
213  lcd_show(man_run);
214 #ifndef CONF_LCD_REFRESH
215  lcd_refresh();
216 #endif
217 #endif
218 
219  // run app
220  //
221 #ifdef CONF_TM
222 # ifndef CONF_PROGRAM
224 # endif
225  tm_start();
226 #else
227  main(0, (void*)0);
228 #endif
229 
230  show_off();
231 
232  // ON/OFF + PROGRAM -> erase firmware
233 #ifdef CONF_DKEY
234  while((c=dkey_multi) & KEY_ONOFF)
235  if(c&KEY_PRGM)
236  reset_after_shutdown=1;
237 #else
238  while (PRESSED(dbutton(), BUTTON_ONOFF))
240  reset_after_shutdown=1;
241 #endif
242 
243 #ifdef CONF_PROGRAM
245 #endif
246 #ifdef CONF_LNP
248 #endif
249 #ifdef CONF_DMOTOR
250  dm_shutdown();
251 #endif
252 #ifdef CONF_DSENSOR
253  ds_shutdown();
254 #endif
255 #ifdef CONF_TIME
257 #endif
258 
259  if (reset_after_shutdown)
260  rom_reset();
261 
262  lcd_clear();
263  lcd_power_off();
264 
265  power_off();
266  }
267 }
void cls()
clear user portion of LCD
#define KEY_PRGM
the program key is pressed
Definition: dkey.h:47
void systime_init(void)
initialize system timer
Definition: systime.c:270
Interface: string functions.
#define PITCH_G5
Definition: dsound.h:144
void cputc_native_3(char mask)
write bit-pattern for segments at position 3 of LCD
int main(int argc, char **argv)
the user main()
#define lcd_digit(d)
display a single digit right of the man symbol
Definition: lcd.h:134
Interface: console input / output.
void * reset_vector
reset vector
Internal Interface: direct motor control.
Internal Interface: direct sensor access.
void program_init()
initialize program support
Internal Interface: Powerdown Timer Routines.
void cputc_native_0(char mask)
write bit-pattern for segments at position 0 of LCD
void systime_shutdown(void)
shutdown system timer
Definition: systime.c:317
static void dsound_play(const note_t *notes)
play a sequence of notes
Definition: dsound.h:234
#define DSOUND_DEFAULT_16th_ms
default duration of 1/16th note in ms
Definition: dsound.h:204
#define PITCH_C3
Definition: dsound.h:113
Internal LNP Interface: link networking protocol logical layer.
unsigned char motor_controller
RCX Motor Controller port.
#define BUTTON_ONOFF
the on/off button
Definition: dbutton.h:42
void program_shutdown()
shutdown program support
Internal Interface: program data structures and functions.
void power_off(void)
enters software standby mode.
Definition: system.h:42
void show_off(void)
show OFF string
Definition: kmain.c:116
Internal Interface: task management.
void rom_reset(void) __attribute__((noreturn))
turns off interrupts, then issues reset.
Definition: system.h:68
the note structure describing a single note.
Definition: dsound.h:56
volatile unsigned char dkey_multi
the currently active keys
void lnp_logical_init(void)
Initialize the logical layer (IR port)
void cputs(char *s)
Write string s to LCD (Only first 5 chars)
Interface: reduced UNIX standard library.
#define PRESSED(state, button)
true if all of the specified buttons are pressed
Definition: dbutton.h:56
void kmain(void)
the beginning of everything
Definition: kmain.c:142
char __etext_hi
Internal Interface: memory management.
void lcd_show(lcd_segment segment)
show LCD segment
Definition: lcd.h:156
#define PITCH_END
mark the end of a list of note_t entries
Definition: dsound.h:193
void delay(unsigned ms)
uncalibrated delay loop
Definition: conio.c:204
void dsound_init()
initialize sound driver
tid_t execi(int(*code_start)(int, char **), int argc, char **argv, priority_t priority, size_t stack_size)
void lnp_init(void)
Initialise protocol handlers.
void * memcpy(void *dest, const void *src, size_t size)
copy memory block from src to dest.
void * rom_reset_vector
ROM reset vector.
#define PITCH_C4
Definition: dsound.h:125
void cputc_native_4(char mask)
write bit-pattern for segments at position 4 of LCD
void ds_init(void)
initialize sensor a/d conversion
void lcd_refresh(void)
refresh the entire LCD display
Definition: lcd.c:254
void tm_init(void)
init task management
void show_on(void)
show ON string
Definition: kmain.c:87
#define PRIO_NORMAL
The priority of most tasks.
Definition: tm.h:53
void shutoff_init(void)
ROM Interface: RCX system control functions.
Internal Interface: direct sound control.
void dm_shutdown(void)
shutdown motors
#define KEY_ONOFF
the on/off key is pressed
Definition: dkey.h:44
void dm_init(void)
initialize motors
#define KEY_ANY
any of the keys
Definition: dkey.h:49
void lnp_logical_shutdown(void)
Shutdown the logical layer (IR port)
Interface: query raw button states.
#define BUTTON_PROGRAM
the program button
Definition: dbutton.h:45
static unsigned dsound_set_duration(unsigned duration)
set duration of a 16th note in ms; return the previous duration.
Definition: dsound.h:246
Definition: lcd.h:55
char __bss
the start of the uninitialized data segment
unsigned char * firmware_string
firmware recognition string
Definition: kmain.c:58
void lcd_init(void)
initialize the LCD display driver
Definition: lcd.c:295
void tm_start(void)
start task management
void lcd_clear(void)
clear LCD display
Definition: lcd.h:191
Internal Interface: LCD control and constants.
void power_init(void)
disables software standby mode so tm_idle_task() can use the sleep
Definition: system.h:54
Internal LNP Interface: RCX redirected IRQ vectors.
void cputc_native_1(char mask)
write bit-pattern for segments at position 1 of LCD
void mm_init()
initialize memory management
void * memset(void *s, int c, size_t n)
fill memory block with a byte value.
char __text_hi
the high memory segment
void cputc_native_2(char mask)
write bit-pattern for segments at position 2 of LCD
Internal LNP Interface: link networking protocol.
void lcd_power_off(void)
power off the LCD controller
Definition: lcd.c:279
void ds_shutdown(void)
shutdown sensor a/d conversion
#define PITCH_G4
Definition: dsound.h:132
Internal Interface: system time functions.
char __bss_end
the end of the uninitialized data segment
int dbutton(void)
get button states
Definition: dbutton.h:67
#define DEFAULT_STACK_SIZE
that's enough.
Definition: tm.h:81

brickOS is released under the Mozilla Public License.
Original code copyright 1998-2005 by the authors.

Generated on Sun Jun 29 2014 23:26:59 for brickOS Kernel Developer by doxygen 1.8.7