pbtest.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Prinz, <uprinz2@netscape.net>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
00022  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  *
00037  * Revision 0.3  2009/09/12 ulrichprinz
00038  * First checkin, new push button driver example
00039  * (currently SAM7X256 is tested only)
00040  *
00041  */
00042 
00052 #include <stdio.h>
00053 #include <io.h>
00054 
00055 #include <cfg/arch.h>
00056 #include <cfg/dev.h>
00057 #include <dev/board.h>
00058 #include <sys/timer.h>
00059 #include <sys/event.h>
00060 #include <sys/thread.h>
00061 
00062 #include <dev/gpio.h>
00063 
00064 #include <dev/keys.h>
00065 #include <dev/led.h>
00066 
00067 /* Handles for the LEDs */
00068 HANDLE led1, led2, led3, led4;
00069 /* Handles for the keys / joystick of the SAM7X-EK */
00070 HANDLE keyUp, keyDn, keyLt, keyRt, keyMi;
00071 /* Handles for two threads controlled by the keys */
00072 HANDLE keyT1w, keyT2w;
00073 
00082 THREAD( Key1Thread, arg)
00083 {
00084     NutThreadSetPriority(60);
00085     for(;;) {
00086         NutEventWait( &keyT1w, NUT_WAIT_INFINITE);
00087         if( NutGetKeyState( keyMi) & KEY_PENDING) {
00088             printf("KEY ENTER pressed\n");
00089             NutSetLed( led2, LED_OFF, 0, 0);
00090             NutSetLed( led3, LED_OFF, 0, 0);
00091             NutSetLed( led4, LED_OFF, 0, 0);
00092         }
00093         if( NutGetKeyState( keyDn) & KEY_PENDING) {
00094             printf("\nKEY DOWN pressed\n");
00095             NutSetLed( led2, LED_BLINK, 100, 900);
00096             NutSetLed( led3, LED_BLINK, 500, 500);
00097             NutSetLed( led4, LED_BLINK, 900, 100);
00098         }
00099     }
00100 }
00101 
00110 THREAD( Key2Thread, arg)
00111 {
00112     NutThreadSetPriority(60);
00113     for(;;) {
00114         NutEventWait( &keyT2w, NUT_WAIT_INFINITE);
00115         if( NutGetKeyState( keyLt) & KEY_PENDING) {
00116             printf("\nKEY LEFT pressed\n");
00117             NutSetLed( led2, LED_ON, 200, 0);
00118             NutSetLed( led4, LED_OFF, 0, 0);
00119         }
00120         if( NutGetKeyState( keyRt) & KEY_PENDING) {
00121             printf("\nKEY RIGHT pressed\n");
00122             NutSetLed( led2, LED_OFF, 0, 0);
00123             NutSetLed( led4, LED_ON, 200, 0);
00124         }
00125         if( NutGetKeyState( keyUp) & KEY_PENDING) {
00126             printf("\nKEY UP pressed\n");
00127             NutSetLed( led3, LED_FLIP, 0, 0);
00128         }
00129     }
00130 }
00131 
00132 
00133 /*
00134  * Main application thread.
00135  */
00136 int main(void)
00137 {
00138     u_long baud = 115200;
00139     /*
00140      * Register the UART device, open it, assign stdout to it and set
00141      * the baudrate.
00142      */
00143     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00144     freopen(DEV_DEBUG_NAME, "w", stdout);
00145     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00146 
00147     puts("\n*** LED and key Test ***\n");
00148 
00149     /* Register LED1 as blinking led */
00150     NutRegisterLed( &led1, NUTGPIO_PORTB, 19);
00151     NutSetLed( led1, LED_BLINK, 100, 900);
00152 
00153     /* Register LED2 and let it flash once for 200ms */
00154     NutRegisterLed( &led2, NUTGPIO_PORTB, 20);
00155     NutSetLed( led2, LED_ON, 200, 0);
00156 
00157     /* Register LED3 and let it flash once for 200ms */
00158     NutRegisterLed( &led3, NUTGPIO_PORTB, 21);
00159     NutSetLed( led3, LED_ON, 200, 0);
00160 
00161     /* Register LED4 and let it flash once for 200ms */
00162     NutRegisterLed( &led4, NUTGPIO_PORTB, 22);
00163     NutSetLed( led4, LED_ON, 200, 0);
00164 
00165     /* Register keys for thread 1 mutex
00166      *   Register keyMi to release mutex simply on pressing key1
00167      *   what will clear three right LEDs.
00168      *   Register keyDn to release mutex if key2 is hold down for 2 seconds
00169      *   what will bring right three LEDs to different blinking.
00170      */
00171     NutRegisterKey( &keyMi, &keyT1w, NUTGPIO_PORTA, 25, KEY_ACTION_SHORT, 1000);
00172     NutRegisterKey( &keyDn, &keyT1w, NUTGPIO_PORTA, 22, KEY_ACTION_HOLD, 2000);
00173 
00174     /* Register keys for thread 2 mutex
00175      *   Register key3 to release mutex if it is relased
00176      *   Register key4 to release mutex if it is pressed very short
00177      */
00178     NutRegisterKey( &keyLt, &keyT2w, NUTGPIO_PORTA, 23, KEY_ACTION_UP, 0);
00179     NutRegisterKey( &keyRt, &keyT2w, NUTGPIO_PORTA, 24, KEY_ACTION_DOWN, 0);
00180     NutRegisterKey( &keyUp, &keyT2w, NUTGPIO_PORTA, 21, KEY_ACTION_DOWN, 0);
00181 
00182     /* Register threads that wait on keys */
00183     NutThreadCreate("k1", Key1Thread, NULL, 256);
00184     NutThreadCreate("k2", Key2Thread, NULL, 256);
00185 
00186     /*
00187      * Endless loop in main thread.
00188      */
00189     for (;;) {
00190         putchar('.');
00191         NutSleep(2000);
00192     }
00193     return 0;
00194 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/