pbtest.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Ulrich Prinz, <uprinz2@netscape.net>
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00035 /*
00036  * $Id$
00037  */
00038 
00050 #include <stdio.h>
00051 #include <io.h>
00052 
00053 #include <cfg/arch.h>
00054 #include <cfg/dev.h>
00055 #include <dev/board.h>
00056 #include <sys/timer.h>
00057 #include <sys/event.h>
00058 #include <sys/thread.h>
00059 
00060 #include <dev/gpio.h>
00061 
00062 #include <dev/keys.h>
00063 #include <dev/led.h>
00064 
00065 #if   defined(AT91SAM7X_EK)
00066 #define LED1_PORT    NUTGPIO_PORTB
00067 #define LED1_PIN     19
00068 #define LED2_PORT    NUTGPIO_PORTB
00069 #define LED2_PIN     20
00070 #define LED3_PORT    NUTGPIO_PORTB
00071 #define LED3_PIN     21
00072 #define LED4_PORT    NUTGPIO_PORTB
00073 #define LED4_PIN     22
00074 
00075 #define KEY_MI_PORT  NUTGPIO_PORTA
00076 #define KEY_MI_PIN   25
00077 #define KEY_DN_PORT  NUTGPIO_PORTA
00078 #define KEY_DN_PIN   22
00079 #define KEY_LT_PORT  NUTGPIO_PORTA
00080 #define KEY_LT_PIN   23
00081 #define KEY_RT_PORT  NUTGPIO_PORTA
00082 #define KEY_RT_PIN   24
00083 #define KEY_UP_PORT  NUTGPIO_PORTA
00084 #define KEY_UP_PIN   21
00085 #elif defined(EVK1100)
00086 #define LED1_PORT    NUTGPIO_PORTB
00087 #define LED1_PIN     27
00088 #define LED2_PORT    NUTGPIO_PORTB
00089 #define LED2_PIN     28
00090 #define LED3_PORT    NUTGPIO_PORTB
00091 #define LED3_PIN     29
00092 #define LED4_PORT    NUTGPIO_PORTB
00093 #define LED4_PIN     30
00094 
00095 #define KEY_MI_PORT  NUTGPIO_PORTA
00096 #define KEY_MI_PIN   20
00097 #define KEY_DN_PORT  NUTGPIO_PORTA
00098 #define KEY_DN_PIN   27
00099 #define KEY_LT_PORT  NUTGPIO_PORTA
00100 #define KEY_LT_PIN   25
00101 #define KEY_RT_PORT  NUTGPIO_PORTA
00102 #define KEY_RT_PIN   28
00103 #define KEY_UP_PORT  NUTGPIO_PORTA
00104 #define KEY_UP_PIN   26
00105 
00106 #elif defined(EVK1101)
00107 #define LED1_PORT    NUTGPIO_PORTA
00108 #define LED1_PIN     7
00109 #define LED2_PORT    NUTGPIO_PORTA
00110 #define LED2_PIN     8
00111 #define LED3_PORT    NUTGPIO_PORTA
00112 #define LED3_PIN     21
00113 #define LED4_PORT    NUTGPIO_PORTA
00114 #define LED4_PIN     22
00115 
00116 #define KEY_MI_PORT  NUTGPIO_PORTA
00117 #define KEY_MI_PIN   13
00118 #define KEY_DN_PORT  NUTGPIO_PORTB
00119 #define KEY_DN_PIN   8
00120 #define KEY_LT_PORT  NUTGPIO_PORTB
00121 #define KEY_LT_PIN   6
00122 #define KEY_RT_PORT  NUTGPIO_PORTB
00123 #define KEY_RT_PIN   9
00124 #define KEY_UP_PORT  NUTGPIO_PORTB
00125 #define KEY_UP_PIN   7
00126 
00127 #elif defined(EVK1105)
00128 #define LED1_PORT    NUTGPIO_PORTB
00129 #define LED1_PIN     27
00130 #define LED2_PORT    NUTGPIO_PORTB
00131 #define LED2_PIN     28
00132 #define LED3_PORT    NUTGPIO_PORTA
00133 #define LED3_PIN     5
00134 #define LED4_PORT    NUTGPIO_PORTA
00135 #define LED4_PIN     6
00136 
00137 #define KEY_MI_PORT  NUTGPIO_PORTB
00138 #define KEY_MI_PIN   26
00139 #define KEY_DN_PORT  NUTGPIO_PORTB
00140 #define KEY_DN_PIN   23
00141 #define KEY_LT_PORT  NUTGPIO_PORTB
00142 #define KEY_LT_PIN   25
00143 #define KEY_RT_PORT  NUTGPIO_PORTB
00144 #define KEY_RT_PIN   24
00145 #define KEY_UP_PORT  NUTGPIO_PORTB
00146 #define KEY_UP_PIN   22
00147 #endif
00148 
00149 
00150 
00151 
00152 
00153 /* Handles for the LEDs */
00154 HANDLE led1, led2, led3, led4;
00155 /* Handles for the keys / joystick of the SAM7X-EK */
00156 HANDLE keyUp, keyDn, keyLt, keyRt, keyMi;
00157 /* Handles for two threads controlled by the keys */
00158 HANDLE keyT1w, keyT2w;
00159 
00168 THREAD(Key1Thread, arg)
00169 {
00170     NutThreadSetPriority(60);
00171     for (;;) {
00172         NutEventWait(&keyT1w, NUT_WAIT_INFINITE);
00173         if (NutGetKeyState(keyMi) & KEY_PENDING) {
00174             printf("KEY ENTER pressed\n");
00175             NutSetLed(led2, LED_OFF, 0, 0);
00176             NutSetLed(led3, LED_OFF, 0, 0);
00177             NutSetLed(led4, LED_OFF, 0, 0);
00178         }
00179         if (NutGetKeyState(keyDn) & KEY_PENDING) {
00180             printf("\nKEY DOWN pressed\n");
00181             NutSetLed(led2, LED_BLINK, 100, 900);
00182             NutSetLed(led3, LED_BLINK, 500, 500);
00183             NutSetLed(led4, LED_BLINK, 900, 100);
00184         }
00185     }
00186 }
00187 
00196 THREAD(Key2Thread, arg)
00197 {
00198     NutThreadSetPriority(60);
00199     for (;;) {
00200         NutEventWait(&keyT2w, NUT_WAIT_INFINITE);
00201         if (NutGetKeyState(keyLt) & KEY_PENDING) {
00202             printf("\nKEY LEFT pressed\n");
00203             NutSetLed(led2, LED_ON, 200, 0);
00204             NutSetLed(led4, LED_OFF, 0, 0);
00205         }
00206         if (NutGetKeyState(keyRt) & KEY_PENDING) {
00207             printf("\nKEY RIGHT pressed\n");
00208             NutSetLed(led2, LED_OFF, 0, 0);
00209             NutSetLed(led4, LED_ON, 200, 0);
00210         }
00211         if (NutGetKeyState(keyUp) & KEY_PENDING) {
00212             printf("\nKEY UP pressed\n");
00213             NutSetLed(led3, LED_FLIP, 0, 0);
00214         }
00215     }
00216 }
00217 
00218 
00219 /*
00220  * Main application thread.
00221  */
00222 int main(void)
00223 {
00224     uint32_t baud = 115200;
00225     /*
00226      * Register the UART device, open it, assign stdout to it and set
00227      * the baudrate.
00228      */
00229     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00230     freopen(DEV_DEBUG_NAME, "w", stdout);
00231     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00232 
00233 #if defined(EVK1104)
00234     puts("\n*** Sorry, LED and key Test not supported by the EVK1104 ***\n");
00235     fflush(stdout);
00236 #else
00237     puts("\n*** LED and key Test ***\n");
00238     fflush(stdout);
00239 
00240     /* Register LED1 as blinking led */
00241     NutRegisterLed(&led1, LED1_PORT, LED1_PIN);
00242     NutSetLed(led1, LED_BLINK, 100, 900);
00243 
00244     /* Register LED2 and let it flash once for 200ms */
00245     NutRegisterLed(&led2, LED2_PORT, LED2_PIN);
00246     NutSetLed(led2, LED_ON, 200, 0);
00247 
00248     /* Register LED3 and let it flash once for 200ms */
00249     NutRegisterLed(&led3, LED3_PORT, LED3_PIN);
00250     NutSetLed(led3, LED_ON, 200, 0);
00251 
00252     /* Register LED4 and let it flash once for 200ms */
00253     NutRegisterLed(&led4, LED4_PORT, LED4_PIN);
00254     NutSetLed(led4, LED_ON, 200, 0);
00255 
00256     /* Register keys for thread */
00257 
00258     /* First we register middle function of the 5-way control of the SAM7X-EK
00259      * to do something if it is pressed for less than 1s.
00260      */
00261     NutRegisterKey(&keyMi, KEY_MI_PORT, KEY_MI_PIN, KEY_ACTION_SHORT, 1000);
00262 
00263     /* Then we assign, what to do if the above event has happened
00264      * Here we assign to release a handle.
00265      */
00266     NutAssignKeyEvt(keyMi, &keyT1w);
00267 
00268     /* Second example is to register the down function of the 5-way controller
00269      * to a different functionality, it executes something after beeing pressed
00270      * for at least 2s.
00271      */
00272 #if !defined(EVK1105)     
00273     NutRegisterKey(&keyDn, KEY_DN_PORT, KEY_DN_PIN, KEY_ACTION_HOLD, 2000);
00274 #else
00275     /* The EVK1105 have touch keys, therefore we will not wait 2s here */
00276     NutRegisterKey(&keyDn, KEY_DN_PORT, KEY_DN_PIN, KEY_ACTION_UP, 0);
00277 #endif    
00278     
00279 
00280     /* Assign event to keyDn too.
00281      */
00282     NutAssignKeyEvt(keyDn, &keyT1w);
00283 
00284     /* Register keys for thread 2 mutex
00285      */
00286     NutRegisterKey(&keyLt, KEY_LT_PORT, KEY_LT_PIN, KEY_ACTION_UP, 0);
00287     NutRegisterKey(&keyRt, KEY_RT_PORT, KEY_RT_PIN, KEY_ACTION_DOWN, 0);
00288     NutRegisterKey(&keyUp, KEY_UP_PORT, KEY_UP_PIN, KEY_ACTION_DOWN, 0);
00289 
00290     /* We assign two of the keys to release a mutex.
00291      */
00292     NutAssignKeyEvt(keyLt, &keyT2w);
00293     NutAssignKeyEvt(keyRt, &keyT2w);
00294     NutAssignKeyEvt(keyUp, &keyT2w);
00295 
00296     /* Register threads that wait on keys */
00297     NutThreadCreate("k1", Key1Thread, NULL, 256);
00298     NutThreadCreate("k2", Key2Thread, NULL, 256);
00299 #endif    
00300 
00301     /*
00302      * Endless loop in main thread.
00303      */
00304     for (;;) {
00305         putchar('.');
00306         NutSleep(2000);
00307     }
00308     return 0;
00309 }

© 2000-2010 by contributors - visit http://www.ethernut.de/