Gセンサ制御 実装版

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge_byYUTAKA by keisuke Yoshida

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
Child:
1:a86c84ead24a
????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:76dfa9657d9d 1 #include "mbed.h"
jksoft 0:76dfa9657d9d 2 #include "BLEDevice.h"
jksoft 0:76dfa9657d9d 3 #include "RCBController.h"
jksoft 0:76dfa9657d9d 4 #include "TB6612.h"
jksoft 0:76dfa9657d9d 5
jksoft 0:76dfa9657d9d 6 #define DBG 0
jksoft 0:76dfa9657d9d 7
jksoft 0:76dfa9657d9d 8 BLEDevice ble;
jksoft 0:76dfa9657d9d 9 Serial pc(USBTX, USBRX);
jksoft 0:76dfa9657d9d 10 /* LEDs for indication: */
jksoft 0:76dfa9657d9d 11 DigitalOut ModeLed(P0_19);
jksoft 0:76dfa9657d9d 12 DigitalOut ConnectStateLed(P0_18);
jksoft 0:76dfa9657d9d 13 DigitalOut outlow(P0_20);
jksoft 0:76dfa9657d9d 14 //PwmOut ControllerStateLed(LED2);
jksoft 0:76dfa9657d9d 15
jksoft 0:76dfa9657d9d 16 AnalogIn fsen1(P0_2);
jksoft 0:76dfa9657d9d 17 AnalogIn fsen2(P0_3);
jksoft 0:76dfa9657d9d 18 AnalogIn fsen3(P0_4);
jksoft 0:76dfa9657d9d 19 AnalogIn fsen4(P0_5);
jksoft 0:76dfa9657d9d 20 #if 1
jksoft 0:76dfa9657d9d 21 TB6612 left(P0_29,P0_23,P0_24);
jksoft 0:76dfa9657d9d 22 TB6612 right(P0_28,P0_0,P0_30);
jksoft 0:76dfa9657d9d 23 #else
jksoft 0:76dfa9657d9d 24 TB6612 left(P0_29,P0_24,P0_23);
jksoft 0:76dfa9657d9d 25 TB6612 right(P0_28,P0_0,P0_30);
jksoft 0:76dfa9657d9d 26 #endif
jksoft 0:76dfa9657d9d 27 Ticker ticker;
jksoft 0:76dfa9657d9d 28
jksoft 0:76dfa9657d9d 29 DigitalIn sw1(P0_16);
jksoft 0:76dfa9657d9d 30 DigitalIn sw2(P0_17);
jksoft 0:76dfa9657d9d 31
jksoft 0:76dfa9657d9d 32 DigitalIn pin1(P0_8);
jksoft 0:76dfa9657d9d 33 DigitalIn pin2(P0_10);
jksoft 0:76dfa9657d9d 34
jksoft 0:76dfa9657d9d 35
jksoft 0:76dfa9657d9d 36 int base_fsen[4];
jksoft 0:76dfa9657d9d 37 int line_mode = 0;
jksoft 0:76dfa9657d9d 38 int challenge_mode = 0;
jksoft 0:76dfa9657d9d 39 char bValue = 0;
jksoft 0:76dfa9657d9d 40
jksoft 0:76dfa9657d9d 41 int get_line(int num);
jksoft 0:76dfa9657d9d 42
jksoft 0:76dfa9657d9d 43 /* RCBController Service */
jksoft 0:76dfa9657d9d 44 static const uint16_t RCBController_service_uuid = 0xFFF0;
jksoft 0:76dfa9657d9d 45 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
jksoft 0:76dfa9657d9d 46 static const uint16_t RCBController_b_Characteristic_uuid = 0xFFF3;
jksoft 0:76dfa9657d9d 47 uint8_t RCBControllerPayload[10] = {0,};
jksoft 0:76dfa9657d9d 48
jksoft 0:76dfa9657d9d 49 GattCharacteristic ControllerChar (RCBController_Characteristic_uuid,RCBControllerPayload,10, 10,
jksoft 0:76dfa9657d9d 50 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
jksoft 0:76dfa9657d9d 51 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
jksoft 0:76dfa9657d9d 52 static uint8_t _bValue = 0x00;
jksoft 0:76dfa9657d9d 53 static uint8_t _mValue[10] = {0,};
jksoft 0:76dfa9657d9d 54 GattCharacteristic b_Char(RCBController_b_Characteristic_uuid, _mValue, sizeof(_mValue), sizeof(_mValue),
jksoft 0:76dfa9657d9d 55 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
jksoft 0:76dfa9657d9d 56
jksoft 0:76dfa9657d9d 57 GattCharacteristic *ControllerChars[] = {&ControllerChar,&b_Char};
jksoft 0:76dfa9657d9d 58 GattService RCBControllerService(RCBController_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
jksoft 0:76dfa9657d9d 59
jksoft 0:76dfa9657d9d 60 RCBController controller;
jksoft 0:76dfa9657d9d 61
jksoft 0:76dfa9657d9d 62 void onConnected(Gap::Handle_t handle, const Gap::ConnectionParams_t *params)
jksoft 0:76dfa9657d9d 63 {
jksoft 0:76dfa9657d9d 64 ConnectStateLed = 0;
jksoft 0:76dfa9657d9d 65 #if DBG
jksoft 0:76dfa9657d9d 66 pc.printf("Connected\n\r");
jksoft 0:76dfa9657d9d 67 #endif
jksoft 0:76dfa9657d9d 68 }
jksoft 0:76dfa9657d9d 69
jksoft 0:76dfa9657d9d 70 void onDisconnected(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
jksoft 0:76dfa9657d9d 71 {
jksoft 0:76dfa9657d9d 72 ble.startAdvertising();
jksoft 0:76dfa9657d9d 73 ConnectStateLed = 1;
jksoft 0:76dfa9657d9d 74 #if DBG
jksoft 0:76dfa9657d9d 75 pc.printf("Disconnected\n\r");
jksoft 0:76dfa9657d9d 76 #endif
jksoft 0:76dfa9657d9d 77 }
jksoft 0:76dfa9657d9d 78
jksoft 0:76dfa9657d9d 79 void periodicCallback(void)
jksoft 0:76dfa9657d9d 80 {
jksoft 0:76dfa9657d9d 81 if (!ble.getGapState().connected) {
jksoft 0:76dfa9657d9d 82 return;
jksoft 0:76dfa9657d9d 83 }
jksoft 0:76dfa9657d9d 84 int line = get_line(0) ? 1 : 0;
jksoft 0:76dfa9657d9d 85 line |= get_line(1) ? 2 : 0;
jksoft 0:76dfa9657d9d 86 line |= get_line(2) ? 4 : 0;
jksoft 0:76dfa9657d9d 87 line |= get_line(3) ? 8 : 0;
jksoft 0:76dfa9657d9d 88 if( (bValue == 0)&&(line != 0) )
jksoft 0:76dfa9657d9d 89 {
jksoft 0:76dfa9657d9d 90 // game over
jksoft 0:76dfa9657d9d 91 left = 0.0;
jksoft 0:76dfa9657d9d 92 right = 0.0;
jksoft 0:76dfa9657d9d 93 bValue = 10;
jksoft 0:76dfa9657d9d 94 }
jksoft 0:76dfa9657d9d 95 if( bValue > 0 )
jksoft 0:76dfa9657d9d 96 {
jksoft 0:76dfa9657d9d 97 memcpy( _mValue , "GAME OVER",10);
jksoft 0:76dfa9657d9d 98 ble.updateCharacteristicValue(b_Char.getValueAttribute().getHandle(), (uint8_t *)_mValue, sizeof(_mValue));
jksoft 0:76dfa9657d9d 99 ModeLed = !ModeLed;
jksoft 0:76dfa9657d9d 100 bValue--;
jksoft 0:76dfa9657d9d 101 if( bValue == 0 )
jksoft 0:76dfa9657d9d 102 {
jksoft 0:76dfa9657d9d 103 ModeLed = 1;
jksoft 0:76dfa9657d9d 104 challenge_mode = 0;
jksoft 0:76dfa9657d9d 105 ticker.detach();
jksoft 0:76dfa9657d9d 106 }
jksoft 0:76dfa9657d9d 107 }
jksoft 0:76dfa9657d9d 108 }
jksoft 0:76dfa9657d9d 109
jksoft 0:76dfa9657d9d 110
jksoft 0:76dfa9657d9d 111 // GattEvent
jksoft 0:76dfa9657d9d 112 void onDataWritten(const GattCharacteristicWriteCBParams *params)
jksoft 0:76dfa9657d9d 113 {
jksoft 0:76dfa9657d9d 114 if( (params->charHandle == ControllerChar.getValueAttribute().getHandle()) && (line_mode == 0))
jksoft 0:76dfa9657d9d 115 {
jksoft 0:76dfa9657d9d 116 memcpy( &controller.data[0], params->data , params->len );
jksoft 0:76dfa9657d9d 117 //memcpy( &controller.data[0], RCBControllerPayload, sizeof(controller));
jksoft 0:76dfa9657d9d 118 #if DBG
jksoft 0:76dfa9657d9d 119
jksoft 0:76dfa9657d9d 120 pc.printf("DATA:%02X %02X %d %d %d %d %d %d %d %02X\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
jksoft 0:76dfa9657d9d 121 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
jksoft 0:76dfa9657d9d 122 #endif
jksoft 0:76dfa9657d9d 123 float right_factor;
jksoft 0:76dfa9657d9d 124 float left_factor;
jksoft 0:76dfa9657d9d 125
jksoft 0:76dfa9657d9d 126 left_factor = ((float)((int)controller.status.LeftAnalogUD -128) / 128.0);
jksoft 0:76dfa9657d9d 127 right_factor = ((float)((int)controller.status.RightAnalogUD -128) / 128.0);
jksoft 0:76dfa9657d9d 128
jksoft 0:76dfa9657d9d 129 if(challenge_mode == 1)
jksoft 0:76dfa9657d9d 130 {
jksoft 0:76dfa9657d9d 131 if( bValue == 0 )
jksoft 0:76dfa9657d9d 132 {
jksoft 0:76dfa9657d9d 133 float factor = ((float)((int)controller.status.AcceleX -128) / 128.0);
jksoft 0:76dfa9657d9d 134
jksoft 0:76dfa9657d9d 135 float right_factor = ((factor <= 0.0) ? 1.0 : 1.0 - (factor*2));
jksoft 0:76dfa9657d9d 136 float left_factor = ((factor >= 0.0) ? 1.0 : 1.0 - (-factor*2));
jksoft 0:76dfa9657d9d 137
jksoft 0:76dfa9657d9d 138 if( controller.status.B == 1 )
jksoft 0:76dfa9657d9d 139 {
jksoft 0:76dfa9657d9d 140 left = left_factor;
jksoft 0:76dfa9657d9d 141 right = right_factor;
jksoft 0:76dfa9657d9d 142 }
jksoft 0:76dfa9657d9d 143 else if( controller.status.A == 1 )
jksoft 0:76dfa9657d9d 144 {
jksoft 0:76dfa9657d9d 145 left = -right_factor;
jksoft 0:76dfa9657d9d 146 right = -left_factor;
jksoft 0:76dfa9657d9d 147 }
jksoft 0:76dfa9657d9d 148 else
jksoft 0:76dfa9657d9d 149 {
jksoft 0:76dfa9657d9d 150 left = 0;
jksoft 0:76dfa9657d9d 151 right = 0;
jksoft 0:76dfa9657d9d 152 }
jksoft 0:76dfa9657d9d 153 }
jksoft 0:76dfa9657d9d 154 }
jksoft 0:76dfa9657d9d 155 else if( (left_factor != 0.0)||(right_factor != 0.0) )
jksoft 0:76dfa9657d9d 156 {
jksoft 0:76dfa9657d9d 157 left = left_factor;
jksoft 0:76dfa9657d9d 158 right = right_factor;
jksoft 0:76dfa9657d9d 159 }
jksoft 0:76dfa9657d9d 160 else
jksoft 0:76dfa9657d9d 161 {
jksoft 0:76dfa9657d9d 162 float factor = ((float)((int)controller.status.AcceleX -128) / 128.0);
jksoft 0:76dfa9657d9d 163
jksoft 0:76dfa9657d9d 164 float right_factor = ((factor <= 0.0) ? 1.0 : 1.0 - (factor*2));
jksoft 0:76dfa9657d9d 165 float left_factor = ((factor >= 0.0) ? 1.0 : 1.0 - (-factor*2));
jksoft 0:76dfa9657d9d 166
jksoft 0:76dfa9657d9d 167 if( controller.status.B == 1 )
jksoft 0:76dfa9657d9d 168 {
jksoft 0:76dfa9657d9d 169 left = left_factor;
jksoft 0:76dfa9657d9d 170 right = right_factor;
jksoft 0:76dfa9657d9d 171 }
jksoft 0:76dfa9657d9d 172 else if( controller.status.A == 1 )
jksoft 0:76dfa9657d9d 173 {
jksoft 0:76dfa9657d9d 174 left = -right_factor;
jksoft 0:76dfa9657d9d 175 right = -left_factor;
jksoft 0:76dfa9657d9d 176 }
jksoft 0:76dfa9657d9d 177 else if( controller.status.UP == 1 )
jksoft 0:76dfa9657d9d 178 {
jksoft 0:76dfa9657d9d 179 left = 1.0;
jksoft 0:76dfa9657d9d 180 right = 1.0;
jksoft 0:76dfa9657d9d 181 }
jksoft 0:76dfa9657d9d 182 else if( controller.status.DOWN == 1 )
jksoft 0:76dfa9657d9d 183 {
jksoft 0:76dfa9657d9d 184 left = -1.0;
jksoft 0:76dfa9657d9d 185 right = -1.0;
jksoft 0:76dfa9657d9d 186 }
jksoft 0:76dfa9657d9d 187 else if( controller.status.RIGHT == 1 )
jksoft 0:76dfa9657d9d 188 {
jksoft 0:76dfa9657d9d 189 left = 1.0;
jksoft 0:76dfa9657d9d 190 right = -1.0;
jksoft 0:76dfa9657d9d 191 }
jksoft 0:76dfa9657d9d 192 else if( controller.status.LEFT == 1 )
jksoft 0:76dfa9657d9d 193 {
jksoft 0:76dfa9657d9d 194 left = -1.0;
jksoft 0:76dfa9657d9d 195 right = 1.0;
jksoft 0:76dfa9657d9d 196 }
jksoft 0:76dfa9657d9d 197 else
jksoft 0:76dfa9657d9d 198 {
jksoft 0:76dfa9657d9d 199 left = 0.0;
jksoft 0:76dfa9657d9d 200 right = 0.0;
jksoft 0:76dfa9657d9d 201 }
jksoft 0:76dfa9657d9d 202 if((controller.status.UP == 1)&&(controller.status.DOWN == 1))
jksoft 0:76dfa9657d9d 203 {
jksoft 0:76dfa9657d9d 204 left = 0.0;
jksoft 0:76dfa9657d9d 205 right = 0.0;
jksoft 0:76dfa9657d9d 206 ModeLed = 0;
jksoft 0:76dfa9657d9d 207 challenge_mode = 1;
jksoft 0:76dfa9657d9d 208 ticker.attach(periodicCallback, 0.1);
jksoft 0:76dfa9657d9d 209
jksoft 0:76dfa9657d9d 210 }
jksoft 0:76dfa9657d9d 211 }
jksoft 0:76dfa9657d9d 212 //ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;
jksoft 0:76dfa9657d9d 213 }
jksoft 0:76dfa9657d9d 214 }
jksoft 0:76dfa9657d9d 215
jksoft 0:76dfa9657d9d 216 int get_fsen(int num)
jksoft 0:76dfa9657d9d 217 {
jksoft 0:76dfa9657d9d 218 switch(num)
jksoft 0:76dfa9657d9d 219 {
jksoft 0:76dfa9657d9d 220 case 0:
jksoft 0:76dfa9657d9d 221 return((int)fsen1.read_u16());
jksoft 0:76dfa9657d9d 222 case 1:
jksoft 0:76dfa9657d9d 223 return((int)fsen2.read_u16());
jksoft 0:76dfa9657d9d 224 case 2:
jksoft 0:76dfa9657d9d 225 return((int)fsen3.read_u16());
jksoft 0:76dfa9657d9d 226 case 3:
jksoft 0:76dfa9657d9d 227 return((int)fsen4.read_u16());
jksoft 0:76dfa9657d9d 228 }
jksoft 0:76dfa9657d9d 229 return(0);
jksoft 0:76dfa9657d9d 230 }
jksoft 0:76dfa9657d9d 231
jksoft 0:76dfa9657d9d 232 void base()
jksoft 0:76dfa9657d9d 233 {
jksoft 0:76dfa9657d9d 234 wait(0.5);
jksoft 0:76dfa9657d9d 235
jksoft 0:76dfa9657d9d 236 for(int i=0;i<4;i++)
jksoft 0:76dfa9657d9d 237 {
jksoft 0:76dfa9657d9d 238 base_fsen[i] = 0;
jksoft 0:76dfa9657d9d 239 }
jksoft 0:76dfa9657d9d 240
jksoft 0:76dfa9657d9d 241 for(int j=0;j<10;j++)
jksoft 0:76dfa9657d9d 242 {
jksoft 0:76dfa9657d9d 243 for(int i=0;i<4;i++)
jksoft 0:76dfa9657d9d 244 {
jksoft 0:76dfa9657d9d 245 base_fsen[i] += get_fsen(i);
jksoft 0:76dfa9657d9d 246 }
jksoft 0:76dfa9657d9d 247 wait_ms(50);
jksoft 0:76dfa9657d9d 248 }
jksoft 0:76dfa9657d9d 249 for(int i=0;i<4;i++)
jksoft 0:76dfa9657d9d 250 {
jksoft 0:76dfa9657d9d 251 base_fsen[i] = base_fsen[i] / 10;
jksoft 0:76dfa9657d9d 252 }
jksoft 0:76dfa9657d9d 253 pc.printf("[0]:%05d[1]:%05d[2]:%05d[3]:%05d\n\r",base_fsen[0],base_fsen[1],base_fsen[2],base_fsen[3]);
jksoft 0:76dfa9657d9d 254
jksoft 0:76dfa9657d9d 255 }
jksoft 0:76dfa9657d9d 256
jksoft 0:76dfa9657d9d 257 int get_line(int num)
jksoft 0:76dfa9657d9d 258 {
jksoft 0:76dfa9657d9d 259 int in = get_fsen(num);
jksoft 0:76dfa9657d9d 260 int ret = 0;
jksoft 0:76dfa9657d9d 261
jksoft 0:76dfa9657d9d 262 #if 1
jksoft 0:76dfa9657d9d 263 if(in > 700)
jksoft 0:76dfa9657d9d 264 #else
jksoft 0:76dfa9657d9d 265 if( (in > (base_fsen[num] + 200))||(in < (base_fsen[num] - 200)))
jksoft 0:76dfa9657d9d 266 #endif
jksoft 0:76dfa9657d9d 267 {
jksoft 0:76dfa9657d9d 268 ret = 1;
jksoft 0:76dfa9657d9d 269 }
jksoft 0:76dfa9657d9d 270 return(ret);
jksoft 0:76dfa9657d9d 271 }
jksoft 0:76dfa9657d9d 272
jksoft 0:76dfa9657d9d 273 void line(void)
jksoft 0:76dfa9657d9d 274 {
jksoft 0:76dfa9657d9d 275 ModeLed = 0;
jksoft 0:76dfa9657d9d 276 wait(1);
jksoft 0:76dfa9657d9d 277 while(sw1 != 0)
jksoft 0:76dfa9657d9d 278 {
jksoft 0:76dfa9657d9d 279 #if 0
jksoft 0:76dfa9657d9d 280 int line = get_line(0) ? 0 : 1;
jksoft 0:76dfa9657d9d 281 line |= get_line(1) ? 0 : 2;
jksoft 0:76dfa9657d9d 282 line |= get_line(2) ? 0 : 4;
jksoft 0:76dfa9657d9d 283 line |= get_line(3) ? 0 : 8;
jksoft 0:76dfa9657d9d 284 #else
jksoft 0:76dfa9657d9d 285 int line = get_line(0) ? 1 : 0;
jksoft 0:76dfa9657d9d 286 line |= get_line(1) ? 2 : 0;
jksoft 0:76dfa9657d9d 287 line |= get_line(2) ? 4 : 0;
jksoft 0:76dfa9657d9d 288 line |= get_line(3) ? 8 : 0;
jksoft 0:76dfa9657d9d 289 #endif
jksoft 0:76dfa9657d9d 290 switch(line)
jksoft 0:76dfa9657d9d 291 {
jksoft 0:76dfa9657d9d 292 case 1: // ○○○●
jksoft 0:76dfa9657d9d 293 left = 1.0;
jksoft 0:76dfa9657d9d 294 right = -1.0;
jksoft 0:76dfa9657d9d 295 break;
jksoft 0:76dfa9657d9d 296 case 3: // ○○●●
jksoft 0:76dfa9657d9d 297 left = 1.0;
jksoft 0:76dfa9657d9d 298 right = -0.5;
jksoft 0:76dfa9657d9d 299 break;
jksoft 0:76dfa9657d9d 300 case 2: // ○○●○
jksoft 0:76dfa9657d9d 301 left = 1.0;
jksoft 0:76dfa9657d9d 302 right = 0.5;
jksoft 0:76dfa9657d9d 303 break;
jksoft 0:76dfa9657d9d 304 case 6: // ○●●○
jksoft 0:76dfa9657d9d 305 left = 1.0;
jksoft 0:76dfa9657d9d 306 right = 1.0;
jksoft 0:76dfa9657d9d 307 break;
jksoft 0:76dfa9657d9d 308 case 4: // ○●○○
jksoft 0:76dfa9657d9d 309 left = 0.5;
jksoft 0:76dfa9657d9d 310 right = 1.0;
jksoft 0:76dfa9657d9d 311 break;
jksoft 0:76dfa9657d9d 312 case 12: // ●●○○
jksoft 0:76dfa9657d9d 313 left = -0.5;
jksoft 0:76dfa9657d9d 314 right = 1.0;
jksoft 0:76dfa9657d9d 315 break;
jksoft 0:76dfa9657d9d 316 case 8: // ●○○○
jksoft 0:76dfa9657d9d 317 left = -1.0;
jksoft 0:76dfa9657d9d 318 right = 1.0;
jksoft 0:76dfa9657d9d 319 break;
jksoft 0:76dfa9657d9d 320 default:
jksoft 0:76dfa9657d9d 321 left = 1.0;
jksoft 0:76dfa9657d9d 322 right = 1.0;
jksoft 0:76dfa9657d9d 323 break;
jksoft 0:76dfa9657d9d 324 }
jksoft 0:76dfa9657d9d 325 }
jksoft 0:76dfa9657d9d 326 ModeLed = 1;
jksoft 0:76dfa9657d9d 327 left = 0.0;
jksoft 0:76dfa9657d9d 328 right = 0.0;
jksoft 0:76dfa9657d9d 329 wait(1);
jksoft 0:76dfa9657d9d 330 }
jksoft 0:76dfa9657d9d 331 int counter1 = 0;
jksoft 0:76dfa9657d9d 332 void p1_rise()
jksoft 0:76dfa9657d9d 333 {
jksoft 0:76dfa9657d9d 334 if( pin2 == 1 )
jksoft 0:76dfa9657d9d 335 {
jksoft 0:76dfa9657d9d 336 counter1++;
jksoft 0:76dfa9657d9d 337 }
jksoft 0:76dfa9657d9d 338 else
jksoft 0:76dfa9657d9d 339 {
jksoft 0:76dfa9657d9d 340 counter1--;
jksoft 0:76dfa9657d9d 341 }
jksoft 0:76dfa9657d9d 342 }
jksoft 0:76dfa9657d9d 343 /**************************************************************************/
jksoft 0:76dfa9657d9d 344 /*!
jksoft 0:76dfa9657d9d 345 @brief Program entry point
jksoft 0:76dfa9657d9d 346 */
jksoft 0:76dfa9657d9d 347 /**************************************************************************/
jksoft 0:76dfa9657d9d 348 int main(void)
jksoft 0:76dfa9657d9d 349 {
jksoft 0:76dfa9657d9d 350 sw1.mode(PullUp);
jksoft 0:76dfa9657d9d 351 sw2.mode(PullUp);
jksoft 0:76dfa9657d9d 352
jksoft 0:76dfa9657d9d 353 ModeLed = 1;
jksoft 0:76dfa9657d9d 354 ConnectStateLed = 1;
jksoft 0:76dfa9657d9d 355 #if DBG
jksoft 0:76dfa9657d9d 356 //pc.baud(921600);
jksoft 0:76dfa9657d9d 357 pc.baud(9600);
jksoft 0:76dfa9657d9d 358 pc.printf("Start\n\r");
jksoft 0:76dfa9657d9d 359 #endif
jksoft 0:76dfa9657d9d 360 outlow = 0;
jksoft 0:76dfa9657d9d 361
jksoft 0:76dfa9657d9d 362 if(sw2 == 0)
jksoft 0:76dfa9657d9d 363 {
jksoft 0:76dfa9657d9d 364 pin1.mode(PullDown);
jksoft 0:76dfa9657d9d 365 // pin1.rise(&p1_rise);
jksoft 0:76dfa9657d9d 366 while(1)
jksoft 0:76dfa9657d9d 367 {
jksoft 0:76dfa9657d9d 368 //int in1 = pin1;
jksoft 0:76dfa9657d9d 369 //int in2 = pin2;
jksoft 0:76dfa9657d9d 370 //ModeLed = pin1;
jksoft 0:76dfa9657d9d 371 //pc.printf("dat = %d %d\r\n",in1,in2);
jksoft 0:76dfa9657d9d 372 base();
jksoft 0:76dfa9657d9d 373 #if 0
jksoft 0:76dfa9657d9d 374 left = 1.0;
jksoft 0:76dfa9657d9d 375 right = 1.0;
jksoft 0:76dfa9657d9d 376 wait(5);
jksoft 0:76dfa9657d9d 377 left = -1.0;
jksoft 0:76dfa9657d9d 378 right = -1.0;
jksoft 0:76dfa9657d9d 379 wait(5);
jksoft 0:76dfa9657d9d 380 #endif
jksoft 0:76dfa9657d9d 381 }
jksoft 0:76dfa9657d9d 382 }
jksoft 0:76dfa9657d9d 383
jksoft 0:76dfa9657d9d 384 ble.init();
jksoft 0:76dfa9657d9d 385 ble.onConnection(onConnected);
jksoft 0:76dfa9657d9d 386 ble.onDisconnection(onDisconnected);
jksoft 0:76dfa9657d9d 387 ble.onDataWritten(onDataWritten);
jksoft 0:76dfa9657d9d 388
jksoft 0:76dfa9657d9d 389 /* setup advertising */
jksoft 0:76dfa9657d9d 390 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 0:76dfa9657d9d 391 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jksoft 0:76dfa9657d9d 392 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
jksoft 0:76dfa9657d9d 393 (const uint8_t *)"mbed WallbotBLE", sizeof("mbed WallbotBLE") - 1);
jksoft 0:76dfa9657d9d 394 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
jksoft 0:76dfa9657d9d 395 (const uint8_t *)RCBController_service_uuid, sizeof(RCBController_service_uuid));
jksoft 0:76dfa9657d9d 396
jksoft 0:76dfa9657d9d 397 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
jksoft 0:76dfa9657d9d 398 ble.startAdvertising();
jksoft 0:76dfa9657d9d 399
jksoft 0:76dfa9657d9d 400 ble.addService(RCBControllerService);
jksoft 0:76dfa9657d9d 401
jksoft 0:76dfa9657d9d 402
jksoft 0:76dfa9657d9d 403 while (true) {
jksoft 0:76dfa9657d9d 404 if(sw1 == 0)
jksoft 0:76dfa9657d9d 405 {
jksoft 0:76dfa9657d9d 406 bValue = 1;
jksoft 0:76dfa9657d9d 407 line_mode = 1;
jksoft 0:76dfa9657d9d 408 line();
jksoft 0:76dfa9657d9d 409 line_mode = 0;
jksoft 0:76dfa9657d9d 410 bValue = 0;
jksoft 0:76dfa9657d9d 411 }
jksoft 0:76dfa9657d9d 412 ble.waitForEvent();
jksoft 0:76dfa9657d9d 413 }
jksoft 0:76dfa9657d9d 414 }
jksoft 0:76dfa9657d9d 415