00001 /********************************************************************************* 00002 00003 Copyright 2006-2008 MakingThings 00004 00005 Licensed under the Apache License, 00006 Version 2.0 (the "License"); you may not use this file except in compliance 00007 with the License. You may obtain a copy of the License at 00008 00009 http://www.apache.org/licenses/LICENSE-2.0 00010 00011 Unless required by applicable law or agreed to in writing, software distributed 00012 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 00013 CONDITIONS OF ANY KIND, either express or implied. See the License for 00014 the specific language governing permissions and limitations under the License. 00015 00016 *********************************************************************************/ 00017 00018 // MakingThings - Make Controller Kit - 2006 00019 00020 /** \file appled.c 00021 MAKE Application Board LED control. 00022 Library of functions for the Make Application Board's LED subsystem. 00023 */ 00024 00025 #include "io.h" 00026 #include "appled.h" 00027 #include "config.h" 00028 00029 #include "AT91SAM7X256.h" 00030 00031 #define APPLED_COUNT 4 00032 00033 #if ( APPBOARD_VERSION == 50 ) 00034 #define APPLED_0_IO IO_PB19 00035 #define APPLED_1_IO IO_PB20 00036 #define APPLED_2_IO IO_PB21 00037 #define APPLED_3_IO IO_PB22 00038 #endif 00039 #if ( APPBOARD_VERSION == 90 ) 00040 #define APPLED_0_IO IO_PA10 00041 #define APPLED_1_IO IO_PA11 00042 #define APPLED_2_IO IO_PA13 00043 #define APPLED_3_IO IO_PA15 00044 #endif 00045 #if ( APPBOARD_VERSION == 95 || APPBOARD_VERSION == 100 ) 00046 #define APPLED_0_IO IO_PA15 00047 #define APPLED_1_IO IO_PA13 00048 #define APPLED_2_IO IO_PA28 00049 #define APPLED_3_IO IO_PA27 00050 #endif 00051 00052 static int AppLed_GetIo( int index ); 00053 static int AppLed_Start( int index ); 00054 static int AppLed_Stop( int index ); 00055 00056 static int AppLed_users[ APPLED_COUNT ]; 00057 00058 /** \defgroup AppLed Application Board LEDs 00059 * The Application Board LED subsystem controls the 4 LEDs on the Application Board, for status and program feedback. 00060 * \ingroup Libraries 00061 * @{ 00062 */ 00063 00064 /** 00065 Enable an LED in the AppLed subsystem. 00066 @param index An integer specifying which LED (0-3). 00067 @param state An integer specifying locked/active (1) or unlocked/inactive (0). 00068 @return Zero on success. 00069 00070 \b Example 00071 \code 00072 // enable LED 0 00073 AppLed_SetActive(0, 1); 00074 \endcode 00075 */ 00076 int AppLed_SetActive( int index, int state ) 00077 { 00078 if ( index < 0 || index >= APPLED_COUNT ) 00079 return CONTROLLER_ERROR_ILLEGAL_INDEX; 00080 00081 if ( state ) 00082 return AppLed_Start( index ); 00083 else 00084 return AppLed_Stop( index ); 00085 } 00086 00087 /** 00088 Read whether an AppLed is enabled. 00089 @param index An integer specifying which LED (0-3). 00090 @return Zero if available, otherwise non-zero. 00091 00092 \b Example 00093 \code 00094 if(AppLed_GetActive(1)) 00095 { 00096 // LED 1 is enabled 00097 } 00098 else 00099 { 00100 // LED 1 is not enabled 00101 } 00102 \endcode 00103 */ 00104 int AppLed_GetActive( int index ) 00105 { 00106 if ( index < 0 || index >= APPLED_COUNT ) 00107 return false; 00108 return AppLed_users[ index ] > 0; 00109 } 00110 00111 /** 00112 Turn an LED on or off. 00113 Sets whether the specified LED on the Application Board is on or off. 00114 @param index an integer specifying which LED (0-3). 00115 @param state an integer specifying on (1) or off (0). 00116 @return Zero on success, otherwise error code. 00117 00118 \b Example 00119 \code 00120 // turn on LED 2 00121 AppLed_SetState( 2, 1 ); 00122 \endcode 00123 */ 00124 int AppLed_SetState( int index, int state ) 00125 { 00126 if ( index < 0 || index >= APPLED_COUNT ) 00127 return CONTROLLER_ERROR_ILLEGAL_INDEX; 00128 00129 if ( AppLed_users[ index ] < 1 ) 00130 { 00131 int status = AppLed_Start( index ); 00132 if ( status != CONTROLLER_OK ) 00133 return status; 00134 } 00135 00136 int io = AppLed_GetIo( index ); 00137 // remember the LED's are tied to +3.3, so they're 00138 // on when the output is off 00139 if ( state ) 00140 Io_SetValue( io, false ); 00141 else 00142 Io_SetValue( io, true ); 00143 00144 return CONTROLLER_OK; 00145 } 00146 00147 /** 00148 Read whether an LED is on or off. 00149 @param index An integer specifying which LED (0-3). 00150 @return the LED state, or zero on error. 00151 00152 \b Example 00153 \code 00154 if(AppLed_GetState(2)) 00155 { 00156 // LED 2 is currently on 00157 } 00158 else 00159 { 00160 // LED 2 is currently off 00161 } 00162 \endcode 00163 */ 00164 int AppLed_GetState( int index ) 00165 { 00166 if ( index < 0 || index >= APPLED_COUNT ) 00167 return 0; 00168 00169 if ( AppLed_users[ index ] < 1 ) 00170 { 00171 int status = AppLed_Start( index ); 00172 if ( status != CONTROLLER_OK ) 00173 return status; 00174 } 00175 00176 int io = AppLed_GetIo( index ); 00177 return Io_GetValue( io ) ? 0 : 1; 00178 } 00179 00180 /** @} 00181 */ 00182 00183 int AppLed_Start( int index ) 00184 { 00185 int status; 00186 00187 if ( AppLed_users[ index ]++ == 0 ) 00188 { 00189 int io = AppLed_GetIo( index ); 00190 00191 status = Io_Start( io, true ); 00192 if ( status != CONTROLLER_OK ) 00193 { 00194 AppLed_users[ index ]--; 00195 return CONTROLLER_ERROR_CANT_LOCK; 00196 } 00197 00198 // Got it, now set the io up right 00199 Io_SetPio( io, true ); 00200 Io_SetValue( io, true ); 00201 Io_SetDirection( io, IO_OUTPUT ); 00202 } 00203 00204 return CONTROLLER_OK; 00205 } 00206 00207 int AppLed_Stop( int index ) 00208 { 00209 if ( AppLed_users[ index ] < 1 ) 00210 return CONTROLLER_ERROR_TOO_MANY_STOPS; 00211 00212 if ( --AppLed_users[ index ] == 0 ) 00213 { 00214 int io = AppLed_GetIo( index ); 00215 Io_Stop( io ); 00216 } 00217 return CONTROLLER_OK; 00218 } 00219 00220 int AppLed_GetIo( int index ) 00221 { 00222 int io = -1; 00223 switch ( index ) 00224 { 00225 case 0: 00226 io = APPLED_0_IO; 00227 break; 00228 case 1: 00229 io = APPLED_1_IO; 00230 break; 00231 case 2: 00232 io = APPLED_2_IO; 00233 break; 00234 case 3: 00235 io = APPLED_3_IO; 00236 break; 00237 } 00238 return io; 00239 } 00240 00241 #ifdef OSC 00242 00243 /** \defgroup AppLEDOSC App LED - OSC 00244 Control the Application Board's Status LEDs via OSC. 00245 \ingroup OSC 00246 00247 \section devices Devices 00248 There are 4 LEDs on the Make Application Board, numbered 0 - 3. 00249 00250 \section properties Properties 00251 The LEDs have two properties: 00252 - state 00253 - active 00254 00255 \par State 00256 The \b state property corresponds to the on/off state of a given LED. 00257 For example, to turn on the first LED, send a message like 00258 \verbatim /appled/0/state 1\endverbatim 00259 To turn it off, send the message \verbatim /appled/0/state 0\endverbatim 00260 00261 \par Active 00262 The \b active property corresponds to the active state of an LED. 00263 If an LED is set to be active, no other tasks will be able to 00264 write to it. If you're not seeing appropriate 00265 responses to your messages to the LED, check the whether it's 00266 locked by sending a message like 00267 \verbatim /appled/0/active \endverbatim 00268 \par 00269 You can set the active flag by sending 00270 \verbatim /appled/0/active 1 \endverbatim 00271 */ 00272 00273 #include "osc.h" 00274 #include "string.h" 00275 #include "stdio.h" 00276 00277 // Need a list of property names 00278 // MUST end in zero 00279 static char* AppLedOsc_Name = "appled"; 00280 static char* AppLedOsc_PropertyNames[] = { "active", "state", 0 }; // must have a trailing 0 00281 00282 int AppLedOsc_PropertySet( int index, int property, int value ); 00283 int AppLedOsc_PropertyGet( int index, int property ); 00284 00285 // Returns the name of the subsystem 00286 const char* AppLedOsc_GetName( ) 00287 { 00288 return AppLedOsc_Name; 00289 } 00290 00291 // Now getting a message. This is actually a part message, with the first 00292 // part (the subsystem) already parsed off. 00293 int AppLedOsc_ReceiveMessage( int channel, char* message, int length ) 00294 { 00295 int status = Osc_IndexIntReceiverHelper( channel, message, length, 00296 APPLED_COUNT, AppLedOsc_Name, 00297 AppLedOsc_PropertySet, AppLedOsc_PropertyGet, 00298 AppLedOsc_PropertyNames ); 00299 00300 if ( status != CONTROLLER_OK ) 00301 return Osc_SendError( channel, AppLedOsc_Name, status ); 00302 return CONTROLLER_OK; 00303 } 00304 00305 // Set the index LED, property with the value 00306 int AppLedOsc_PropertySet( int index, int property, int value ) 00307 { 00308 switch ( property ) 00309 { 00310 case 0: 00311 AppLed_SetActive( index, value ); 00312 break; 00313 case 1: 00314 AppLed_SetState( index, value ); 00315 break; 00316 } 00317 return CONTROLLER_OK; 00318 } 00319 00320 // Get the index LED, property 00321 int AppLedOsc_PropertyGet( int index, int property ) 00322 { 00323 int value = 0; 00324 switch ( property ) 00325 { 00326 case 0: 00327 value = AppLed_GetActive( index ); 00328 break; 00329 case 1: 00330 value = AppLed_GetState( index ); 00331 break; 00332 } 00333 00334 return value; 00335 } 00336 00337 #endif
The Make Controller Kit is an open source project maintained by MakingThings.
MakingThings code is released under the Apache 2.0 license.
Bug tracker, development wiki and status can be found at http://dev.makingthings.com.
This document was last updated on 18 May 2009.