Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

Example Programs in C

Ex1: Our hello world example
a simple demostration of how to write to the LCD
demo/helloworld.c
#include <conio.h> #include <unistd.h> int main(int argc, char **argv) { cputs("hello"); sleep(1); cputs("world"); sleep(1); cls(); return 0; }
00001 #include <conio.h> 00002 #include <unistd.h> 00003 00004 int main(int argc, char **argv) { 00005 cputs("hello"); 00006 sleep(1); 00007 cputs("world"); 00008 sleep(1); 00009 cls(); 00010 00011 return 0; 00012 }

Ex2: generating sounds
a simple demostration of the RCX playing music
demo/sound.c
/*sound.c*/ #include <config.h> #if defined(CONF_DSOUND) #include <dsound.h> #include <tm.h> /*array of notes that make up the refrain*/ /*of Devil with a Blue Dress*/ static const note_t devil[] = { { PITCH_G4, QUARTER }, { PITCH_G4, QUARTER }, { PITCH_G4, QUARTER }, { PITCH_G4, QUARTER }, { PITCH_G4, HALF }, { PITCH_G4, HALF }, { PITCH_G4, HALF }, { PITCH_G4, HALF }, { PITCH_G4, HALF }, { PITCH_G4, HALF }, { PITCH_F4, QUARTER }, { PITCH_F4, QUARTER }, { PITCH_F4, QUARTER }, { PITCH_F4, QUARTER }, { PITCH_F4, HALF }, { PITCH_F4, HALF }, { PITCH_F4, HALF }, { PITCH_PAUSE, HALF }, { PITCH_PAUSE, HALF }, { PITCH_PAUSE, HALF }, { PITCH_E4, QUARTER }, { PITCH_E4, QUARTER }, { PITCH_E4, QUARTER }, { PITCH_E4, QUARTER }, { PITCH_F4, HALF }, { PITCH_F4, HALF }, { PITCH_E4, HALF }, { PITCH_E4, HALF }, { PITCH_F4, HALF }, { PITCH_F4, HALF }, { PITCH_E4, QUARTER }, { PITCH_E4, QUARTER }, { PITCH_E4, QUARTER }, { PITCH_E4, QUARTER }, { PITCH_F4, HALF }, { PITCH_F4, HALF }, { PITCH_E4, HALF }, { PITCH_PAUSE, HALF }, { PITCH_PAUSE, HALF }, { PITCH_PAUSE, HALF }, { PITCH_END, 0 } }; int main(int argc,char *argv[]) { /*The default makes this a really, really slow song*/ /*So, we speed it up a little bit.*/ dsound_set_duration(40); /*now, we play it*/ while (!shutdown_requested()) { if (wait_event(dsound_finished,0) != 0) dsound_play(devil); sleep(1); } return 0; } #else #warning sound.c requires CONF_DSOUND which is not set #warning sound demo will do nothing int main(int argc, char *argv[]) { return 0; } #endif // CONF_DSOUND
00001 /*sound.c*/ 00002 00003 #include <config.h> 00004 #if defined(CONF_DSOUND) 00005 #include <dsound.h> 00006 #include <tm.h> 00007 00008 /*array of notes that make up the refrain*/ 00009 /*of Devil with a Blue Dress*/ 00010 00011 static const note_t devil[] = { 00012 { PITCH_G4, QUARTER }, 00013 { PITCH_G4, QUARTER }, 00014 { PITCH_G4, QUARTER }, 00015 { PITCH_G4, QUARTER }, 00016 { PITCH_G4, HALF }, 00017 { PITCH_G4, HALF }, 00018 00019 { PITCH_G4, HALF }, 00020 { PITCH_G4, HALF }, 00021 { PITCH_G4, HALF }, 00022 { PITCH_G4, HALF }, 00023 00024 { PITCH_F4, QUARTER }, 00025 { PITCH_F4, QUARTER }, 00026 { PITCH_F4, QUARTER }, 00027 { PITCH_F4, QUARTER }, 00028 { PITCH_F4, HALF }, 00029 { PITCH_F4, HALF }, 00030 00031 { PITCH_F4, HALF }, 00032 { PITCH_PAUSE, HALF }, 00033 { PITCH_PAUSE, HALF }, 00034 { PITCH_PAUSE, HALF }, 00035 00036 { PITCH_E4, QUARTER }, 00037 { PITCH_E4, QUARTER }, 00038 { PITCH_E4, QUARTER }, 00039 { PITCH_E4, QUARTER }, 00040 { PITCH_F4, HALF }, 00041 { PITCH_F4, HALF }, 00042 00043 { PITCH_E4, HALF }, 00044 { PITCH_E4, HALF }, 00045 { PITCH_F4, HALF }, 00046 { PITCH_F4, HALF }, 00047 00048 { PITCH_E4, QUARTER }, 00049 { PITCH_E4, QUARTER }, 00050 { PITCH_E4, QUARTER }, 00051 { PITCH_E4, QUARTER }, 00052 { PITCH_F4, HALF }, 00053 { PITCH_F4, HALF }, 00054 00055 { PITCH_E4, HALF }, 00056 { PITCH_PAUSE, HALF }, 00057 { PITCH_PAUSE, HALF }, 00058 { PITCH_PAUSE, HALF }, 00059 { PITCH_END, 0 } 00060 }; 00061 00062 int main(int argc,char *argv[]) { 00063 00064 /*The default makes this a really, really slow song*/ 00065 /*So, we speed it up a little bit.*/ 00066 dsound_set_duration(40); 00067 00068 /*now, we play it*/ 00069 while (!shutdown_requested()) { 00070 if (wait_event(dsound_finished,0) != 0) 00071 dsound_play(devil); 00072 sleep(1); 00073 } 00074 00075 return 0; 00076 } 00077 #else 00078 #warning sound.c requires CONF_DSOUND which is not set 00079 #warning sound demo will do nothing 00080 int main(int argc, char *argv[]) { 00081 return 0; 00082 } 00083 #endif // CONF_DSOUND

Ex3: A line following robot
a simple demostration of
demo/linetrack.c
/* * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * The Original Code is legOS code, released October 17, 1999. * * The Initial Developer of the Original Code is Markus L. Noga. * Portions created by Markus L. Noga are Copyright (C) 1999 * Markus L. Noga. All Rights Reserved. * * Contributor(s): Markus L. Noga <markus@noga.de> */ #include <config.h> #if defined(CONF_DSENSOR) && defined(CONF_DMOTOR) #include <conio.h> #include <unistd.h> #include <tm.h> #include <dsensor.h> #include <dmotor.h> #define LIGHTSENS SENSOR_2 #define DARK_THRESH 0x40 #define BRIGHT_THRESH 0x48 #define NORMAL_SPEED (2*MAX_SPEED/3) #define TURN_SPEED (MAX_SPEED) // #define STRAIGHT_LINE static wakeup_t dark_found(wakeup_t data) { return LIGHT(LIGHTSENS)<(unsigned short)data; } static wakeup_t bright_found(wakeup_t data) { return LIGHT(LIGHTSENS)>(unsigned short)data; } static void locate_line() { motor_a_speed(NORMAL_SPEED); motor_c_speed(NORMAL_SPEED); motor_a_dir(fwd); motor_c_dir(fwd); wait_event(dark_found,DARK_THRESH); } static void follow_line() { int dir=0; while (!shutdown_requested()) { motor_a_speed(NORMAL_SPEED); motor_c_speed(NORMAL_SPEED); motor_a_dir(fwd); motor_c_dir(fwd); if (wait_event(bright_found,BRIGHT_THRESH) != 0) { if(dir==0) motor_a_dir(rev); else motor_c_dir(rev); #ifdef STRAIGHT_LINE dir=!dir; #endif motor_a_speed(TURN_SPEED); motor_c_speed(TURN_SPEED); wait_event(dark_found,DARK_THRESH); } } } int main(int argc, char *argv[]) { ds_active(&LIGHTSENS); locate_line(); follow_line(); return 0; } #else #warning linetrack.c requires CONF_DSENSOR and CONF_DMOTOR #warning linetrack demo will do nothing int main(int argc, char *argv[]) { return 0; } #endif // defined(CONF_DSENSOR) && defined(CONF_DMOTOR)
00001 00014 /* 00015 * The contents of this file are subject to the Mozilla Public License 00016 * Version 1.0 (the "License"); you may not use this file except in 00017 * compliance with the License. You may obtain a copy of the License at 00018 * http://www.mozilla.org/MPL/ 00019 * 00020 * Software distributed under the License is distributed on an "AS IS" 00021 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 00022 * License for the specific language governing rights and limitations 00023 * under the License. 00024 * 00025 * The Original Code is legOS code, released October 17, 1999. 00026 * 00027 * The Initial Developer of the Original Code is Markus L. Noga. 00028 * Portions created by Markus L. Noga are Copyright (C) 1999 00029 * Markus L. Noga. All Rights Reserved. 00030 * 00031 * Contributor(s): Markus L. Noga <markus@noga.de> 00032 */ 00033 00034 #include <config.h> 00035 #if defined(CONF_DSENSOR) && defined(CONF_DMOTOR) 00036 00037 #include <conio.h> 00038 #include <unistd.h> 00039 #include <tm.h> 00040 00041 #include <dsensor.h> 00042 #include <dmotor.h> 00043 00044 #define LIGHTSENS SENSOR_2 00045 #define DARK_THRESH 0x40 00046 #define BRIGHT_THRESH 0x48 00047 00048 #define NORMAL_SPEED (2*MAX_SPEED/3) 00049 #define TURN_SPEED (MAX_SPEED) 00050 00051 // #define STRAIGHT_LINE 00052 00053 00054 static wakeup_t dark_found(wakeup_t data) { 00055 return LIGHT(LIGHTSENS)<(unsigned short)data; 00056 } 00057 00058 static wakeup_t bright_found(wakeup_t data) { 00059 return LIGHT(LIGHTSENS)>(unsigned short)data; 00060 } 00061 00062 static void locate_line() { 00063 motor_a_speed(NORMAL_SPEED); 00064 motor_c_speed(NORMAL_SPEED); 00065 motor_a_dir(fwd); 00066 motor_c_dir(fwd); 00067 00068 wait_event(dark_found,DARK_THRESH); 00069 } 00070 00071 static void follow_line() { 00072 int dir=0; 00073 00074 00075 while (!shutdown_requested()) { 00076 motor_a_speed(NORMAL_SPEED); 00077 motor_c_speed(NORMAL_SPEED); 00078 motor_a_dir(fwd); 00079 motor_c_dir(fwd); 00080 00081 if (wait_event(bright_found,BRIGHT_THRESH) != 0) 00082 { 00083 if(dir==0) 00084 motor_a_dir(rev); 00085 else 00086 motor_c_dir(rev); 00087 #ifdef STRAIGHT_LINE 00088 dir=!dir; 00089 #endif 00090 00091 motor_a_speed(TURN_SPEED); 00092 motor_c_speed(TURN_SPEED); 00093 00094 wait_event(dark_found,DARK_THRESH); 00095 } 00096 } 00097 } 00098 00099 int main(int argc, char *argv[]) { 00100 ds_active(&LIGHTSENS); 00101 00102 locate_line(); 00103 follow_line(); 00104 00105 return 0; 00106 } 00107 #else 00108 #warning linetrack.c requires CONF_DSENSOR and CONF_DMOTOR 00109 #warning linetrack demo will do nothing 00110 int main(int argc, char *argv[]) { 00111 return 0; 00112 } 00113 #endif // defined(CONF_DSENSOR) && defined(CONF_DMOTOR)

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

Generated on Thu Jul 29 08:55:49 2004 for brickOS C by doxygen 1.3.7