MakingThings
  • Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

led.c

Go to the documentation of this file.
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 /** \file led.c 
00019   Controller LED.
00020   Functions for controlling the status LED on the Make Controller Board.
00021 */
00022 
00023 #include "io.h"
00024 #include "config.h"
00025 #include "led.h"
00026 
00027 static int Led_users;
00028 
00029 #if ( CONTROLLER_VERSION == 50 )
00030   #define LED_IO IO_PB25
00031 #elif ( CONTROLLER_VERSION == 90 )
00032   #define LED_IO IO_PB12
00033 #elif ( CONTROLLER_VERSION == 95 || CONTROLLER_VERSION == 100 || CONTROLLER_VERSION == 200 )
00034   #define LED_IO IO_PA12
00035 #endif
00036 
00037 static int Led_Start( void );
00038 static int Led_Stop( void );
00039 
00040 /** \defgroup Led LED
00041   Controls the single green LED on the MAKE Controller Board.
00042   There are two LEDs on the MAKE Controller Board - one green and one red.  The red LED is simply
00043   a power indicator and cannot be controlled by the Controller.  The green LED can be used for
00044   program feedback.  In many MakingThings applications, for example, it is set to blink once a
00045   second, showing the board's "heartbeat" and letting the user know that the board is running.
00046 * \ingroup Core
00047 * @{
00048 */
00049 
00050 /**
00051   Sets whether the Led subsystem is active.
00052   @param state An integer specifying the active state of the LED system - 1 (on) or 0 (off).
00053   @return Zero on success.
00054   
00055   \b Example
00056   \code
00057   // enable the LED system
00058   Led_SetActive(1);
00059   \endcode
00060 */
00061 int Led_SetActive( int state )
00062 {
00063   if ( state )
00064     return Led_Start( );
00065   else
00066     return Led_Stop( );
00067 }
00068 
00069 /**
00070   Read the active state of the LED system.
00071   @return State - 1/non-zero (on) or 0 (off).
00072   
00073   \b Example
00074   \code
00075   int enabled = Led_GetActive();
00076   if(enabled)
00077   {
00078     // then we're enabled
00079   }
00080   else
00081   {
00082     // not enabled
00083   }
00084   \endcode
00085 */
00086 int Led_GetActive( )
00087 {
00088   return Led_users > 0;
00089 }
00090 
00091 /** 
00092   Control the LED on the MAKE Controller Board.
00093   @param state An integer specifying the state - on (1) or off (0).
00094   @return 0 on success.
00095   
00096   \b Example
00097   \code
00098   // turn the LED on
00099   Led_SetState(1);
00100   \endcode
00101 */
00102 int Led_SetState( int state )
00103 {
00104   if ( Led_users == 0 )
00105   {
00106     int status = Led_Start();
00107     if ( status != CONTROLLER_OK )
00108       return status;
00109   }
00110 
00111   return Io_SetValue( LED_IO, !state );
00112 }
00113 
00114 /** 
00115   Read the state of the LED on the MAKE Controller Board.
00116   @return State - 1/non-zero (on) or 0 (off).
00117   
00118   \b Example
00119   \code
00120   int led_on = Led_GetState();
00121   if(led_on)
00122   {
00123     // the LED is on
00124   }
00125   else
00126   {
00127     // the LED is off
00128   }
00129   \endcode
00130 */
00131 int Led_GetState( )
00132 {
00133   if ( Led_users == 0 )
00134   {
00135     int status = Led_Start();
00136     if ( status != CONTROLLER_OK )
00137       return status;
00138   }
00139 
00140   return Io_GetValue( LED_IO ) ? 0 : 1;
00141 }
00142 
00143 /** @}
00144 */
00145 
00146 int Led_Start()
00147 {
00148   if ( Led_users == 0 )
00149   {
00150     int status;
00151     status = Io_Start( LED_IO, true );
00152     if ( status != CONTROLLER_OK )
00153       return status;
00154     Io_SetValue( LED_IO, false );
00155     Io_SetPio( LED_IO, true );
00156     Io_SetDirection( LED_IO, IO_OUTPUT );
00157   }
00158   Led_users++;
00159  
00160   return CONTROLLER_OK;
00161 }
00162 
00163 int Led_Stop()
00164 {
00165   if ( Led_users == 1 )
00166   {
00167     Io_Stop( LED_IO );
00168     Io_SetValue( LED_IO, false );
00169     Io_SetPio( LED_IO, true );
00170     Io_SetDirection( LED_IO, IO_OUTPUT );
00171   }
00172   Led_users--;
00173 
00174   return CONTROLLER_OK;
00175 }
00176 
00177 #ifdef OSC
00178 
00179 #include "osc.h"
00180 #include "string.h"
00181 #include "stdio.h"
00182 
00183 // Need a list of property names
00184 // MUST end in zero
00185 static char* LedOsc_Name = "led";
00186 static char* LedOsc_PropertyNames[] = { "active", "state", 0 }; // must have a trailing 0
00187 
00188 int LedOsc_PropertySet( int property, int value );
00189 int LedOsc_PropertyGet( int property );
00190 
00191 // Returns the name of the subsystem
00192 const char* LedOsc_GetName( )
00193 {
00194   return LedOsc_Name;
00195 }
00196 
00197 // Now getting a message.  This is actually a part message, with the first
00198 // part (the subsystem) already parsed off.
00199 int LedOsc_ReceiveMessage( int channel, char* message, int length )
00200 {
00201   int status = Osc_IntReceiverHelper( channel, message, length, 
00202                                 LedOsc_Name,
00203                                 LedOsc_PropertySet, LedOsc_PropertyGet, 
00204                                 LedOsc_PropertyNames );
00205 
00206   if ( status != CONTROLLER_OK )
00207     Osc_SendError( channel, LedOsc_Name, status );
00208   return CONTROLLER_OK;
00209 }
00210 
00211 // Set the index LED, property with the value
00212 int LedOsc_PropertySet( int property, int value )
00213 {
00214   switch ( property )
00215   {
00216     case 0: 
00217       Led_SetActive( value );
00218       break;      
00219     case 1:
00220       Led_SetState( value );
00221       break;
00222   }
00223   return CONTROLLER_OK;
00224 }
00225 
00226 // Get the index LED, property
00227 int LedOsc_PropertyGet( int property )
00228 {
00229   int value = 0;
00230   switch ( property )
00231   {
00232     case 0:
00233       value = Led_GetActive( );
00234       break;
00235     case 1:
00236       value = Led_GetState( );
00237       break;
00238   }
00239   
00240   return value;
00241 }
00242 
00243 #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.