Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.

Dependencies:   mbed

Committer:
pehrhovey
Date:
Wed Mar 17 03:17:38 2010 +0000
Revision:
0:439354122597

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:439354122597 1 /*
pehrhovey 0:439354122597 2 * Pehr Hovey
pehrhovey 0:439354122597 3 *
pehrhovey 0:439354122597 4 * mBed OSC - IoOsc subsystem
pehrhovey 0:439354122597 5 * Get/set info about the system like ip address, hostname
pehrhovey 0:439354122597 6 * Based on code from Make Controller
pehrhovey 0:439354122597 7 */
pehrhovey 0:439354122597 8 /*********************************************************************************
pehrhovey 0:439354122597 9
pehrhovey 0:439354122597 10 Copyright 2006-2009 MakingThings
pehrhovey 0:439354122597 11
pehrhovey 0:439354122597 12 Licensed under the Apache License,
pehrhovey 0:439354122597 13 Version 2.0 (the "License"); you may not use this file except in compliance
pehrhovey 0:439354122597 14 with the License. You may obtain a copy of the License at
pehrhovey 0:439354122597 15
pehrhovey 0:439354122597 16 http://www.apache.org/licenses/LICENSE-2.0
pehrhovey 0:439354122597 17
pehrhovey 0:439354122597 18 Unless required by applicable law or agreed to in writing, software distributed
pehrhovey 0:439354122597 19 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
pehrhovey 0:439354122597 20 CONDITIONS OF ANY KIND, either express or implied. See the License for
pehrhovey 0:439354122597 21 the specific language governing permissions and limitations under the License.
pehrhovey 0:439354122597 22
pehrhovey 0:439354122597 23 *********************************************************************************/
pehrhovey 0:439354122597 24
pehrhovey 0:439354122597 25 #include "mbed_io_osc.h"
pehrhovey 0:439354122597 26 #include "osc_sys.h"
pehrhovey 0:439354122597 27 #include "mbed.h"
pehrhovey 0:439354122597 28
pehrhovey 0:439354122597 29
pehrhovey 0:439354122597 30 static char* IoOsc_Name = "io"; //the OSC container for this address
pehrhovey 0:439354122597 31 static char* IoOsc_PropertyNames[] = { "active", "value",
pehrhovey 0:439354122597 32 0 }; // must have a trailing 0
pehrhovey 0:439354122597 33
pehrhovey 0:439354122597 34
pehrhovey 0:439354122597 35
pehrhovey 0:439354122597 36 const char* IoOsc_GetName( void )
pehrhovey 0:439354122597 37 {
pehrhovey 0:439354122597 38 return IoOsc_Name;
pehrhovey 0:439354122597 39 }
pehrhovey 0:439354122597 40 // Now getting a message. This is actually a part message, with the first
pehrhovey 0:439354122597 41 // part (the subsystem) already parsed off.
pehrhovey 0:439354122597 42 int IoOsc_ReceiveMessage( int channel, char* message, int length )
pehrhovey 0:439354122597 43 {
pehrhovey 0:439354122597 44 //printf("IoOsc Receive: message is %s\r\n",message);
pehrhovey 0:439354122597 45 int status = Osc_IndexIntReceiverHelper( channel, message, length,
pehrhovey 0:439354122597 46 IO_CHANNELS,IoOsc_validIndex, IoOsc_Name,
pehrhovey 0:439354122597 47 IoOsc_PropertySet, IoOsc_PropertyGet,
pehrhovey 0:439354122597 48 IoOsc_PropertyNames );
pehrhovey 0:439354122597 49 if ( status != CONTROLLER_OK ){
pehrhovey 0:439354122597 50 //printf("IoOsc Receive error status = %d\r\n",status);
pehrhovey 0:439354122597 51 return Osc_SendError( channel, IoOsc_Name, status );
pehrhovey 0:439354122597 52 }
pehrhovey 0:439354122597 53 return CONTROLLER_OK;
pehrhovey 0:439354122597 54
pehrhovey 0:439354122597 55 }
pehrhovey 0:439354122597 56
pehrhovey 0:439354122597 57
pehrhovey 0:439354122597 58 int IoOsc_PropertySet( int index, int property, int value )
pehrhovey 0:439354122597 59 {
pehrhovey 0:439354122597 60 printf("IoOsc SET index=%d prop=%d val=%d\r\n",index,property,value);
pehrhovey 0:439354122597 61 switch ( property )
pehrhovey 0:439354122597 62 {
pehrhovey 0:439354122597 63 case 0: //active
pehrhovey 0:439354122597 64 // Led_SetActive( index, value );
pehrhovey 0:439354122597 65 //Set if the pin is on or not
pehrhovey 0:439354122597 66 break;
pehrhovey 0:439354122597 67 case 1: //value on-off
pehrhovey 0:439354122597 68 //printf("Setting LED index=%d to value=%d\r\n",index,value);
pehrhovey 0:439354122597 69 return Io_SetValue(index,value);
pehrhovey 0:439354122597 70 break;
pehrhovey 0:439354122597 71 }
pehrhovey 0:439354122597 72 return CONTROLLER_OK;
pehrhovey 0:439354122597 73 }
pehrhovey 0:439354122597 74
pehrhovey 0:439354122597 75 //Retrieve a value which will be sent back by the receiverhelper
pehrhovey 0:439354122597 76 int IoOsc_PropertyGet( int index, int property )
pehrhovey 0:439354122597 77 {
pehrhovey 0:439354122597 78 int value = 0;
pehrhovey 0:439354122597 79
pehrhovey 0:439354122597 80 switch ( property )
pehrhovey 0:439354122597 81 {
pehrhovey 0:439354122597 82 case 0: //is it active?
pehrhovey 0:439354122597 83 //value = Led_GetActive( index );
pehrhovey 0:439354122597 84
pehrhovey 0:439354122597 85 break;
pehrhovey 0:439354122597 86 case 1: // is it on or off?
pehrhovey 0:439354122597 87 // printf("Getting value for LED index=%d \r\n",index);
pehrhovey 0:439354122597 88 value = Io_GetValue( index );
pehrhovey 0:439354122597 89
pehrhovey 0:439354122597 90 break;
pehrhovey 0:439354122597 91
pehrhovey 0:439354122597 92 }
pehrhovey 0:439354122597 93 // printf("IoOsc GET index=%d prop=%d val=%d\r\n",index,value);
pehrhovey 0:439354122597 94 return value;
pehrhovey 0:439354122597 95 }
pehrhovey 0:439354122597 96
pehrhovey 0:439354122597 97
pehrhovey 0:439354122597 98
pehrhovey 0:439354122597 99 //DigitalInOut D5(p5); // also broadcast test button
pehrhovey 0:439354122597 100 //DigitalInOut D6(p6);
pehrhovey 0:439354122597 101 //DigitalInOut D7(p7);
pehrhovey 0:439354122597 102 //DigitalInOut D8(p8);
pehrhovey 0:439354122597 103 //DigitalInOut D9(p9);
pehrhovey 0:439354122597 104 //DigitalInOut D10(p10);
pehrhovey 0:439354122597 105 //DigitalInOut D11(p11);
pehrhovey 0:439354122597 106 //DigitalInOut D12(p12);
pehrhovey 0:439354122597 107 //DigitalInOut D13(p13);
pehrhovey 0:439354122597 108 //DigitalInOut D14(p14);
pehrhovey 0:439354122597 109
pehrhovey 0:439354122597 110 DigitalOut D6(p6); //just make then Out for now
pehrhovey 0:439354122597 111 DigitalOut D7(p7);
pehrhovey 0:439354122597 112 DigitalOut D8(p8);
pehrhovey 0:439354122597 113 DigitalOut D9(p9);
pehrhovey 0:439354122597 114 DigitalOut D10(p10);
pehrhovey 0:439354122597 115 DigitalOut D11(p11);
pehrhovey 0:439354122597 116 DigitalOut D12(p12);
pehrhovey 0:439354122597 117 DigitalOut D13(p13);
pehrhovey 0:439354122597 118 DigitalOut D14(p14);
pehrhovey 0:439354122597 119
pehrhovey 0:439354122597 120
pehrhovey 0:439354122597 121 //Only certain pin indexes are allowed
pehrhovey 0:439354122597 122 bool IoOsc_validIndex(int index){
pehrhovey 0:439354122597 123 return ((index > 5 && index <=14) ||
pehrhovey 0:439354122597 124 (index >= 27 && index <=30)); //21-26 are PWM so exclude
pehrhovey 0:439354122597 125 }
pehrhovey 0:439354122597 126 //Set the actual pin
pehrhovey 0:439354122597 127 int Io_SetValue(int index, int value){
pehrhovey 0:439354122597 128 if(value > 1)
pehrhovey 0:439354122597 129 value = 1;
pehrhovey 0:439354122597 130 if(value < 0)
pehrhovey 0:439354122597 131 value = 0;
pehrhovey 0:439354122597 132 if(IoOsc_validIndex(index)){
pehrhovey 0:439354122597 133 switch(index){
pehrhovey 0:439354122597 134 case 6:
pehrhovey 0:439354122597 135 D6 = value;
pehrhovey 0:439354122597 136 break;
pehrhovey 0:439354122597 137 case 7:
pehrhovey 0:439354122597 138 D7 = value;
pehrhovey 0:439354122597 139 break;
pehrhovey 0:439354122597 140 case 8:
pehrhovey 0:439354122597 141 D8 = value;
pehrhovey 0:439354122597 142 break;
pehrhovey 0:439354122597 143 case 9:
pehrhovey 0:439354122597 144 D9 = value;
pehrhovey 0:439354122597 145 break;
pehrhovey 0:439354122597 146 case 10:
pehrhovey 0:439354122597 147 D10 = value;
pehrhovey 0:439354122597 148 break;
pehrhovey 0:439354122597 149 case 11:
pehrhovey 0:439354122597 150 D11 = value;
pehrhovey 0:439354122597 151 break;
pehrhovey 0:439354122597 152 case 12:
pehrhovey 0:439354122597 153 D12 = value;
pehrhovey 0:439354122597 154 break;
pehrhovey 0:439354122597 155 case 13:
pehrhovey 0:439354122597 156 D13 = value;
pehrhovey 0:439354122597 157 break;
pehrhovey 0:439354122597 158 case 14:
pehrhovey 0:439354122597 159 D14 = value;
pehrhovey 0:439354122597 160 break;
pehrhovey 0:439354122597 161 }
pehrhovey 0:439354122597 162 return CONTROLLER_OK;
pehrhovey 0:439354122597 163 }else{
pehrhovey 0:439354122597 164 return CONTROLLER_ERROR_ILLEGAL_INDEX;
pehrhovey 0:439354122597 165 }
pehrhovey 0:439354122597 166
pehrhovey 0:439354122597 167 }
pehrhovey 0:439354122597 168 int Io_GetValue(int index){
pehrhovey 0:439354122597 169 return 0;
pehrhovey 0:439354122597 170 }
pehrhovey 0:439354122597 171
pehrhovey 0:439354122597 172 /*** Control the four onboard LEDs ***/
pehrhovey 0:439354122597 173
pehrhovey 0:439354122597 174 //do LEDs here for now
pehrhovey 0:439354122597 175
pehrhovey 0:439354122597 176 DigitalOut L1(LED1);
pehrhovey 0:439354122597 177 DigitalOut L2(LED2);
pehrhovey 0:439354122597 178 DigitalOut L3(LED3);
pehrhovey 0:439354122597 179 DigitalOut L4(LED4);
pehrhovey 0:439354122597 180
pehrhovey 0:439354122597 181 int Led_SetValue(int index, int value){
pehrhovey 0:439354122597 182 if(value > 1)
pehrhovey 0:439354122597 183 value = 1;
pehrhovey 0:439354122597 184 if(value < 0)
pehrhovey 0:439354122597 185 value = 0;
pehrhovey 0:439354122597 186
pehrhovey 0:439354122597 187 if(index >= 0 && index < 4){
pehrhovey 0:439354122597 188 switch(index){
pehrhovey 0:439354122597 189 case 0:
pehrhovey 0:439354122597 190 L1 = value;
pehrhovey 0:439354122597 191 break;
pehrhovey 0:439354122597 192 case 1:
pehrhovey 0:439354122597 193 L2 = value;
pehrhovey 0:439354122597 194 break;
pehrhovey 0:439354122597 195 case 2:
pehrhovey 0:439354122597 196 L3 = value;
pehrhovey 0:439354122597 197 break;
pehrhovey 0:439354122597 198 case 3:
pehrhovey 0:439354122597 199 L4 = value;
pehrhovey 0:439354122597 200 break;
pehrhovey 0:439354122597 201 }
pehrhovey 0:439354122597 202 return CONTROLLER_OK;
pehrhovey 0:439354122597 203 }else{
pehrhovey 0:439354122597 204
pehrhovey 0:439354122597 205 return CONTROLLER_ERROR_ILLEGAL_INDEX;
pehrhovey 0:439354122597 206
pehrhovey 0:439354122597 207 }
pehrhovey 0:439354122597 208 }
pehrhovey 0:439354122597 209 int Led_GetValue(int index){
pehrhovey 0:439354122597 210 int val = -1;
pehrhovey 0:439354122597 211
pehrhovey 0:439354122597 212 if(index >= 0 && index < 4){
pehrhovey 0:439354122597 213 switch(index){
pehrhovey 0:439354122597 214 case 0:
pehrhovey 0:439354122597 215 val = L1.read();
pehrhovey 0:439354122597 216 break;
pehrhovey 0:439354122597 217 case 1:
pehrhovey 0:439354122597 218 val = L2.read();
pehrhovey 0:439354122597 219 break;
pehrhovey 0:439354122597 220 case 2:
pehrhovey 0:439354122597 221 val = L3.read();
pehrhovey 0:439354122597 222 break;
pehrhovey 0:439354122597 223 case 3:
pehrhovey 0:439354122597 224 val = L4.read();
pehrhovey 0:439354122597 225 break;
pehrhovey 0:439354122597 226 }
pehrhovey 0:439354122597 227 return val;
pehrhovey 0:439354122597 228 }else{
pehrhovey 0:439354122597 229
pehrhovey 0:439354122597 230 return CONTROLLER_ERROR_ILLEGAL_INDEX;
pehrhovey 0:439354122597 231 }
pehrhovey 0:439354122597 232 }
pehrhovey 0:439354122597 233
pehrhovey 0:439354122597 234 // Need a list of property names
pehrhovey 0:439354122597 235 // MUST end in zero
pehrhovey 0:439354122597 236 static char* LedOsc_Name = "led";
pehrhovey 0:439354122597 237 static char* LedOsc_PropertyNames[] = { "active", "value", 0 }; // must have a trailing 0
pehrhovey 0:439354122597 238
pehrhovey 0:439354122597 239
pehrhovey 0:439354122597 240 // Returns the name of the subsystem
pehrhovey 0:439354122597 241 const char* LedOsc_GetName( )
pehrhovey 0:439354122597 242 {
pehrhovey 0:439354122597 243 return LedOsc_Name;
pehrhovey 0:439354122597 244 }
pehrhovey 0:439354122597 245
pehrhovey 0:439354122597 246 // Now getting a message. This is actually a part message, with the first
pehrhovey 0:439354122597 247 // part (the subsystem) already parsed off.
pehrhovey 0:439354122597 248 int LedOsc_ReceiveMessage( int channel, char* message, int length )
pehrhovey 0:439354122597 249 {
pehrhovey 0:439354122597 250 //printf("LedOsc Receive: message is %s\r\n",message);
pehrhovey 0:439354122597 251 int status = Osc_IndexIntReceiverHelper( channel, message, length,
pehrhovey 0:439354122597 252 LED_CHANNELS, NULL,LedOsc_Name,
pehrhovey 0:439354122597 253 LedOsc_PropertySet, LedOsc_PropertyGet,
pehrhovey 0:439354122597 254 LedOsc_PropertyNames );
pehrhovey 0:439354122597 255 if ( status != CONTROLLER_OK ){
pehrhovey 0:439354122597 256 //printf("LedOsc Receive error status = %d\r\n",status);
pehrhovey 0:439354122597 257 return Osc_SendError( channel, LedOsc_Name, status );
pehrhovey 0:439354122597 258 }
pehrhovey 0:439354122597 259 return CONTROLLER_OK;
pehrhovey 0:439354122597 260
pehrhovey 0:439354122597 261 }
pehrhovey 0:439354122597 262
pehrhovey 0:439354122597 263
pehrhovey 0:439354122597 264 int LedOsc_PropertySet( int index, int property, int value )
pehrhovey 0:439354122597 265 {
pehrhovey 0:439354122597 266 printf("LedOsc SET index=%d prop=%d val=%d\r\n",index,property,value);
pehrhovey 0:439354122597 267 switch ( property )
pehrhovey 0:439354122597 268 {
pehrhovey 0:439354122597 269 case 0: //active
pehrhovey 0:439354122597 270 // Led_SetActive( index, value );
pehrhovey 0:439354122597 271 //Set if the pin is on or not
pehrhovey 0:439354122597 272 break;
pehrhovey 0:439354122597 273 case 1: //value on-off
pehrhovey 0:439354122597 274 //printf("Setting LED index=%d to value=%d\r\n",index,value);
pehrhovey 0:439354122597 275 return Led_SetValue(index,value);
pehrhovey 0:439354122597 276 break;
pehrhovey 0:439354122597 277 }
pehrhovey 0:439354122597 278 return CONTROLLER_OK;
pehrhovey 0:439354122597 279 }
pehrhovey 0:439354122597 280
pehrhovey 0:439354122597 281 //Retrieve a value which will be sent back by the receiverhelper
pehrhovey 0:439354122597 282 int LedOsc_PropertyGet( int index, int property )
pehrhovey 0:439354122597 283 {
pehrhovey 0:439354122597 284 int value = 0;
pehrhovey 0:439354122597 285
pehrhovey 0:439354122597 286 switch ( property )
pehrhovey 0:439354122597 287 {
pehrhovey 0:439354122597 288 case 0: //is it active?
pehrhovey 0:439354122597 289 //value = Led_GetActive( index );
pehrhovey 0:439354122597 290
pehrhovey 0:439354122597 291 break;
pehrhovey 0:439354122597 292 case 1: // is it on or off?
pehrhovey 0:439354122597 293 // printf("Getting value for LED index=%d \r\n",index);
pehrhovey 0:439354122597 294 value = Led_GetValue( index );
pehrhovey 0:439354122597 295
pehrhovey 0:439354122597 296 break;
pehrhovey 0:439354122597 297
pehrhovey 0:439354122597 298 }
pehrhovey 0:439354122597 299 // printf("LedOsc GET index=%d prop=%d val=%d\r\n",index,value);
pehrhovey 0:439354122597 300 return value;
pehrhovey 0:439354122597 301 }
pehrhovey 0:439354122597 302