For Contest
Dependencies: BLE_API HCSR04 X_NUCLEO_IDB0XA1 mbed
Fork of contest_IOT_10 by
jeu.h@6:55319ed07a08, 2015-11-04 (annotated)
- Committer:
- Mickado
- Date:
- Wed Nov 04 15:02:59 2015 +0000
- Revision:
- 6:55319ed07a08
- Child:
- 8:8d6474336444
Last version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mickado | 6:55319ed07a08 | 1 | #ifndef JEU_H |
Mickado | 6:55319ed07a08 | 2 | #define JEU_H |
Mickado | 6:55319ed07a08 | 3 | |
Mickado | 6:55319ed07a08 | 4 | #include "mbed.h" |
Mickado | 6:55319ed07a08 | 5 | #include "ble/BLE.h" |
Mickado | 6:55319ed07a08 | 6 | #include "ble/services/HeartRateService.h" |
Mickado | 6:55319ed07a08 | 7 | #include "ble/services/BatteryService.h" |
Mickado | 6:55319ed07a08 | 8 | #include "ble/services/DeviceInformationService.h" |
Mickado | 6:55319ed07a08 | 9 | |
Mickado | 6:55319ed07a08 | 10 | DigitalOut led1(LED1); |
Mickado | 6:55319ed07a08 | 11 | |
Mickado | 6:55319ed07a08 | 12 | /****************************** |
Mickado | 6:55319ed07a08 | 13 | | CONFIGURATION BLE | |
Mickado | 6:55319ed07a08 | 14 | ******************************/ |
Mickado | 6:55319ed07a08 | 15 | |
Mickado | 6:55319ed07a08 | 16 | uint16_t customServiceUUID = 0xA000; |
Mickado | 6:55319ed07a08 | 17 | uint16_t readP1UUID = 0xA001; |
Mickado | 6:55319ed07a08 | 18 | uint16_t readP2UUID = 0xA002; |
Mickado | 6:55319ed07a08 | 19 | uint16_t chronoUUID = 0xA003; |
Mickado | 6:55319ed07a08 | 20 | |
Mickado | 6:55319ed07a08 | 21 | const static char DEVICE_NAME[] = "Pong!"; |
Mickado | 6:55319ed07a08 | 22 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development |
Mickado | 6:55319ed07a08 | 23 | |
Mickado | 6:55319ed07a08 | 24 | |
Mickado | 6:55319ed07a08 | 25 | // Set Up custom Characteristics |
Mickado | 6:55319ed07a08 | 26 | static uint8_t readP1[10] = {0}; |
Mickado | 6:55319ed07a08 | 27 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readP1)> player1(readP1UUID, readP1); |
Mickado | 6:55319ed07a08 | 28 | static uint8_t readP2[10] = {0}; |
Mickado | 6:55319ed07a08 | 29 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readP2)> player2(readP2UUID, readP2); |
Mickado | 6:55319ed07a08 | 30 | static uint8_t readChrono[10] = {0}; |
Mickado | 6:55319ed07a08 | 31 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readChrono)> chronoJeu(chronoUUID, readChrono); |
Mickado | 6:55319ed07a08 | 32 | |
Mickado | 6:55319ed07a08 | 33 | // Set up custom service |
Mickado | 6:55319ed07a08 | 34 | GattCharacteristic *characteristics[] = {&player1, &player2, &chronoJeu}; |
Mickado | 6:55319ed07a08 | 35 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
Mickado | 6:55319ed07a08 | 36 | |
Mickado | 6:55319ed07a08 | 37 | static volatile bool triggerSensorPolling = false; |
Mickado | 6:55319ed07a08 | 38 | |
Mickado | 6:55319ed07a08 | 39 | void periodicCallback(void){ |
Mickado | 6:55319ed07a08 | 40 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Mickado | 6:55319ed07a08 | 41 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
Mickado | 6:55319ed07a08 | 42 | * heavy-weight sensor polling from the main thread. */ |
Mickado | 6:55319ed07a08 | 43 | triggerSensorPolling = true; |
Mickado | 6:55319ed07a08 | 44 | } |
Mickado | 6:55319ed07a08 | 45 | |
Mickado | 6:55319ed07a08 | 46 | void initJeuBLE(BLE &ble){ |
Mickado | 6:55319ed07a08 | 47 | ble.init(); |
Mickado | 6:55319ed07a08 | 48 | |
Mickado | 6:55319ed07a08 | 49 | /* Setup advertising. */ |
Mickado | 6:55319ed07a08 | 50 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Mickado | 6:55319ed07a08 | 51 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Mickado | 6:55319ed07a08 | 52 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Mickado | 6:55319ed07a08 | 53 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Mickado | 6:55319ed07a08 | 54 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
Mickado | 6:55319ed07a08 | 55 | ble.addService(customService); |
Mickado | 6:55319ed07a08 | 56 | ble.gap().startAdvertising(); |
Mickado | 6:55319ed07a08 | 57 | } |
Mickado | 6:55319ed07a08 | 58 | void miseAJourBLE(BLE &ble,uint8_t &p1_score,uint8_t &p2_score,uint8_t &chrono){ |
Mickado | 6:55319ed07a08 | 59 | const uint8_t scoreP1const = p1_score; |
Mickado | 6:55319ed07a08 | 60 | const uint8_t scoreP2const = p2_score; |
Mickado | 6:55319ed07a08 | 61 | const uint8_t chronoconst = chrono; |
Mickado | 6:55319ed07a08 | 62 | |
Mickado | 6:55319ed07a08 | 63 | ble.gattServer().write(player1.getValueHandle(), &scoreP1const, sizeof(scoreP1const)); |
Mickado | 6:55319ed07a08 | 64 | ble.gattServer().write(player2.getValueHandle(), &scoreP2const, sizeof(scoreP2const)); |
Mickado | 6:55319ed07a08 | 65 | ble.gattServer().write(chronoJeu.getValueHandle(), &chronoconst, sizeof(chronoconst)); |
Mickado | 6:55319ed07a08 | 66 | } |
Mickado | 6:55319ed07a08 | 67 | |
Mickado | 6:55319ed07a08 | 68 | |
Mickado | 6:55319ed07a08 | 69 | /****************************** |
Mickado | 6:55319ed07a08 | 70 | | CONFIGURATION JEU | |
Mickado | 6:55319ed07a08 | 71 | ******************************/ |
Mickado | 6:55319ed07a08 | 72 | |
Mickado | 6:55319ed07a08 | 73 | class InfosJeu { |
Mickado | 6:55319ed07a08 | 74 | public: |
Mickado | 6:55319ed07a08 | 75 | InfosJeu(){ |
Mickado | 6:55319ed07a08 | 76 | chrono=0; |
Mickado | 6:55319ed07a08 | 77 | p1_score=0; |
Mickado | 6:55319ed07a08 | 78 | p2_score=0; |
Mickado | 6:55319ed07a08 | 79 | chronoActive=0; |
Mickado | 6:55319ed07a08 | 80 | |
Mickado | 6:55319ed07a08 | 81 | posBallX = 5.0; |
Mickado | 6:55319ed07a08 | 82 | posBallY = 19.0; |
Mickado | 6:55319ed07a08 | 83 | angBall = 25.0; |
Mickado | 6:55319ed07a08 | 84 | state = 0; |
Mickado | 6:55319ed07a08 | 85 | p1_score = 0; |
Mickado | 6:55319ed07a08 | 86 | p2_score = 0; |
Mickado | 6:55319ed07a08 | 87 | wait(1); |
Mickado | 6:55319ed07a08 | 88 | //led=1; |
Mickado | 6:55319ed07a08 | 89 | distance1=0; |
Mickado | 6:55319ed07a08 | 90 | distance2=0; |
Mickado | 6:55319ed07a08 | 91 | for(int i=0;i<30;i++){ |
Mickado | 6:55319ed07a08 | 92 | pos1prec[i]=0; |
Mickado | 6:55319ed07a08 | 93 | pos2prec[i]=0; |
Mickado | 6:55319ed07a08 | 94 | pos1[i]=0; |
Mickado | 6:55319ed07a08 | 95 | pos2[i]=0; |
Mickado | 6:55319ed07a08 | 96 | } |
Mickado | 6:55319ed07a08 | 97 | } |
Mickado | 6:55319ed07a08 | 98 | uint8_t chrono; |
Mickado | 6:55319ed07a08 | 99 | bool chronoActive; |
Mickado | 6:55319ed07a08 | 100 | uint8_t p1_score; |
Mickado | 6:55319ed07a08 | 101 | uint8_t p2_score; |
Mickado | 6:55319ed07a08 | 102 | |
Mickado | 6:55319ed07a08 | 103 | int pos1prec[30]; |
Mickado | 6:55319ed07a08 | 104 | int pos2prec[30]; |
Mickado | 6:55319ed07a08 | 105 | int pos1[30]; |
Mickado | 6:55319ed07a08 | 106 | int pos2[30]; |
Mickado | 6:55319ed07a08 | 107 | double posBallX; |
Mickado | 6:55319ed07a08 | 108 | double posBallY; |
Mickado | 6:55319ed07a08 | 109 | double angBall; |
Mickado | 6:55319ed07a08 | 110 | int distance1; |
Mickado | 6:55319ed07a08 | 111 | int distance2; |
Mickado | 6:55319ed07a08 | 112 | |
Mickado | 6:55319ed07a08 | 113 | int state; |
Mickado | 6:55319ed07a08 | 114 | |
Mickado | 6:55319ed07a08 | 115 | void addPointP1(){p1_score++;} |
Mickado | 6:55319ed07a08 | 116 | void addPointP2(){p2_score++;} |
Mickado | 6:55319ed07a08 | 117 | void setChronoActive(){chronoActive=true;} |
Mickado | 6:55319ed07a08 | 118 | void incrementeChrono(){if(chronoActive){chrono++;}} |
Mickado | 6:55319ed07a08 | 119 | void stopChrono(){chronoActive=false;} |
Mickado | 6:55319ed07a08 | 120 | void resetAndStart(){chrono=0; p1_score=0; p2_score=0; setChronoActive();} |
Mickado | 6:55319ed07a08 | 121 | |
Mickado | 6:55319ed07a08 | 122 | void reinitPosition(int p1_sc, int p2_sc, int posX, int posY, int angle){ |
Mickado | 6:55319ed07a08 | 123 | p1_score = p1_sc; |
Mickado | 6:55319ed07a08 | 124 | p2_score = p2_sc; |
Mickado | 6:55319ed07a08 | 125 | posBallX = posX; |
Mickado | 6:55319ed07a08 | 126 | posBallY = posY; |
Mickado | 6:55319ed07a08 | 127 | angBall = angle; |
Mickado | 6:55319ed07a08 | 128 | } |
Mickado | 6:55319ed07a08 | 129 | }; |
Mickado | 6:55319ed07a08 | 130 | |
Mickado | 6:55319ed07a08 | 131 | |
Mickado | 6:55319ed07a08 | 132 | |
Mickado | 6:55319ed07a08 | 133 | |
Mickado | 6:55319ed07a08 | 134 | #endif |