Pehr Hovey / Mbed 2 deprecated mbed_osc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_io_osc.cpp Source File

mbed_io_osc.cpp

00001 /*
00002  * Pehr Hovey
00003  *
00004  * mBed OSC - IoOsc subsystem
00005  * Get/set info about the system like ip address, hostname
00006  * Based on code from Make Controller
00007  */
00008 /*********************************************************************************
00009 
00010  Copyright 2006-2009 MakingThings
00011 
00012  Licensed under the Apache License,
00013  Version 2.0 (the "License"); you may not use this file except in compliance
00014  with the License. You may obtain a copy of the License at
00015 
00016  http://www.apache.org/licenses/LICENSE-2.0
00017 
00018  Unless required by applicable law or agreed to in writing, software distributed
00019  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
00020  CONDITIONS OF ANY KIND, either express or implied. See the License for
00021  the specific language governing permissions and limitations under the License.
00022 
00023 *********************************************************************************/
00024 
00025 #include "mbed_io_osc.h"
00026 #include "osc_sys.h"
00027 #include "mbed.h"
00028 
00029 
00030 static char* IoOsc_Name = "io"; //the OSC container for this address
00031 static char* IoOsc_PropertyNames[] = { "active", "value",
00032                                             0 }; // must have a trailing 0
00033 
00034 
00035 
00036 const char* IoOsc_GetName( void )
00037 {
00038   return IoOsc_Name;
00039 }
00040 // Now getting a message.  This is actually a part message, with the first
00041 // part (the subsystem) already parsed off.
00042 int IoOsc_ReceiveMessage( int channel, char* message, int length )
00043 {
00044   //printf("IoOsc Receive: message is %s\r\n",message);
00045     int status = Osc_IndexIntReceiverHelper( channel, message, length,
00046                                      IO_CHANNELS,IoOsc_validIndex, IoOsc_Name,
00047                                      IoOsc_PropertySet, IoOsc_PropertyGet,
00048                                      IoOsc_PropertyNames );
00049   if ( status != CONTROLLER_OK ){
00050    //printf("IoOsc Receive error status = %d\r\n",status);
00051       return Osc_SendError( channel, IoOsc_Name, status );
00052   }
00053   return CONTROLLER_OK;
00054 
00055 }
00056 
00057 
00058 int IoOsc_PropertySet( int index, int property, int value )
00059 {
00060   printf("IoOsc SET index=%d prop=%d val=%d\r\n",index,property,value);
00061     switch ( property )
00062   {
00063     case 0: //active
00064    //   Led_SetActive( index, value );
00065       //Set if the pin is on or not
00066       break;
00067     case 1: //value on-off
00068         //printf("Setting LED index=%d to value=%d\r\n",index,value);
00069         return Io_SetValue(index,value);
00070       break;
00071   }
00072   return CONTROLLER_OK;
00073 }
00074 
00075 //Retrieve a value which will be sent back by the receiverhelper
00076 int IoOsc_PropertyGet( int index, int property )
00077 {
00078   int value = 0;
00079 
00080   switch ( property )
00081   {
00082     case 0: //is it active?
00083       //value = Led_GetActive( index );
00084 
00085       break;
00086     case 1: // is it on or off?
00087      // printf("Getting value for LED index=%d \r\n",index);
00088       value = Io_GetValue( index );
00089 
00090       break;
00091 
00092   }
00093  // printf("IoOsc GET index=%d prop=%d val=%d\r\n",index,value);
00094   return value;
00095 }
00096 
00097 
00098 
00099 //DigitalInOut D5(p5); // also broadcast test button
00100 //DigitalInOut D6(p6);
00101 //DigitalInOut D7(p7);
00102 //DigitalInOut D8(p8);
00103 //DigitalInOut D9(p9);
00104 //DigitalInOut D10(p10);
00105 //DigitalInOut D11(p11);
00106 //DigitalInOut D12(p12);
00107 //DigitalInOut D13(p13);
00108 //DigitalInOut D14(p14);
00109 
00110 DigitalOut D6(p6); //just make then Out for now
00111 DigitalOut D7(p7);
00112 DigitalOut D8(p8);
00113 DigitalOut D9(p9);
00114 DigitalOut D10(p10);
00115 DigitalOut D11(p11);
00116 DigitalOut D12(p12);
00117 DigitalOut D13(p13);
00118 DigitalOut D14(p14);
00119 
00120 
00121 //Only certain pin indexes are allowed
00122 bool IoOsc_validIndex(int index){
00123     return ((index > 5 && index <=14) ||
00124             (index >= 27 && index <=30)); //21-26 are PWM so exclude
00125 }
00126 //Set the actual pin
00127 int Io_SetValue(int index, int value){
00128     if(value > 1)
00129             value = 1;
00130         if(value < 0)
00131             value = 0;
00132     if(IoOsc_validIndex(index)){
00133         switch(index){
00134             case 6:
00135                 D6 = value;
00136             break;
00137             case 7:
00138                 D7 = value;
00139             break;
00140             case 8:
00141                 D8 = value;
00142             break;
00143             case 9:
00144                 D9 = value;
00145             break;
00146             case 10:
00147                 D10 = value;
00148             break;
00149             case 11:
00150                 D11 = value;
00151             break;
00152             case 12:
00153                 D12 = value;
00154             break;
00155             case 13:
00156                 D13 = value;
00157             break;
00158             case 14:
00159                 D14 = value;
00160             break;
00161             }
00162         return CONTROLLER_OK;
00163     }else{
00164         return CONTROLLER_ERROR_ILLEGAL_INDEX;
00165     }
00166 
00167 }
00168 int Io_GetValue(int index){
00169     return 0;
00170 }
00171 
00172 /*** Control the four onboard LEDs ***/
00173 
00174 //do LEDs here  for now
00175 
00176 DigitalOut L1(LED1);
00177 DigitalOut L2(LED2);
00178 DigitalOut L3(LED3);
00179 DigitalOut L4(LED4);
00180 
00181 int Led_SetValue(int index, int value){
00182     if(value > 1)
00183         value = 1;
00184     if(value < 0)
00185         value = 0;
00186 
00187     if(index >= 0 && index < 4){
00188         switch(index){
00189         case 0:
00190             L1 = value;
00191         break;
00192         case 1:
00193             L2 = value;
00194         break;
00195         case 2:
00196             L3 = value;
00197         break;
00198         case 3:
00199             L4 = value;
00200         break;
00201         }
00202         return CONTROLLER_OK;
00203     }else{
00204 
00205             return CONTROLLER_ERROR_ILLEGAL_INDEX;
00206 
00207     }
00208 }
00209 int Led_GetValue(int index){
00210     int val = -1;
00211 
00212     if(index >= 0 && index < 4){
00213         switch(index){
00214         case 0:
00215             val = L1.read();
00216         break;
00217         case 1:
00218             val = L2.read();
00219         break;
00220         case 2:
00221             val = L3.read();
00222         break;
00223         case 3:
00224             val = L4.read();
00225         break;
00226         }
00227         return val;
00228     }else{
00229 
00230             return CONTROLLER_ERROR_ILLEGAL_INDEX;
00231         }
00232 }
00233 
00234 // Need a list of property names
00235 // MUST end in zero
00236 static char* LedOsc_Name = "led";
00237 static char* LedOsc_PropertyNames[] = { "active", "value", 0 }; // must have a trailing 0
00238 
00239 
00240 // Returns the name of the subsystem
00241 const char* LedOsc_GetName( )
00242 {
00243   return LedOsc_Name;
00244 }
00245 
00246 // Now getting a message.  This is actually a part message, with the first
00247 // part (the subsystem) already parsed off.
00248 int LedOsc_ReceiveMessage( int channel, char* message, int length )
00249 {
00250   //printf("LedOsc Receive: message is %s\r\n",message);
00251     int status = Osc_IndexIntReceiverHelper( channel, message, length,
00252                                      LED_CHANNELS, NULL,LedOsc_Name,
00253                                      LedOsc_PropertySet, LedOsc_PropertyGet,
00254                                      LedOsc_PropertyNames );
00255   if ( status != CONTROLLER_OK ){
00256    //printf("LedOsc Receive error status = %d\r\n",status);
00257       return Osc_SendError( channel, LedOsc_Name, status );
00258   }
00259   return CONTROLLER_OK;
00260 
00261 }
00262 
00263 
00264 int LedOsc_PropertySet( int index, int property, int value )
00265 {
00266   printf("LedOsc SET index=%d prop=%d val=%d\r\n",index,property,value);
00267     switch ( property )
00268   {
00269     case 0: //active
00270    //   Led_SetActive( index, value );
00271       //Set if the pin is on or not
00272       break;
00273     case 1: //value on-off
00274         //printf("Setting LED index=%d to value=%d\r\n",index,value);
00275         return Led_SetValue(index,value);
00276       break;
00277   }
00278   return CONTROLLER_OK;
00279 }
00280 
00281 //Retrieve a value which will be sent back by the receiverhelper
00282 int LedOsc_PropertyGet( int index, int property )
00283 {
00284   int value = 0;
00285 
00286   switch ( property )
00287   {
00288     case 0: //is it active?
00289       //value = Led_GetActive( index );
00290 
00291       break;
00292     case 1: // is it on or off?
00293      // printf("Getting value for LED index=%d \r\n",index);
00294       value = Led_GetValue( index );
00295 
00296       break;
00297 
00298   }
00299  // printf("LedOsc GET index=%d prop=%d val=%d\r\n",index,value);
00300   return value;
00301 }
00302