Current version

Fork of mbed-os-example-ble-LED by mbed-os-examples

Committer:
jurica238814
Date:
Thu Feb 23 13:07:54 2017 +0000
Revision:
19:41b1396c50d2
Parent:
18:d089fb6ff789
Child:
20:f9f18052df0c
LancController init commit (Does not work yet....)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 2:864ddfb70a9c 1 /* mbed Microcontroller Library
mbed_official 2:864ddfb70a9c 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 2:864ddfb70a9c 3 *
mbed_official 2:864ddfb70a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 2:864ddfb70a9c 5 * you may not use this file except in compliance with the License.
mbed_official 2:864ddfb70a9c 6 * You may obtain a copy of the License at
mbed_official 2:864ddfb70a9c 7 *
mbed_official 2:864ddfb70a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 2:864ddfb70a9c 9 *
mbed_official 2:864ddfb70a9c 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 2:864ddfb70a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 2:864ddfb70a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 2:864ddfb70a9c 13 * See the License for the specific language governing permissions and
mbed_official 2:864ddfb70a9c 14 * limitations under the License.
mbed_official 2:864ddfb70a9c 15 */
mbed_official 2:864ddfb70a9c 16
mbed_official 11:7404978b24e7 17 #include <events/mbed_events.h>
mbed_official 2:864ddfb70a9c 18 #include <mbed.h>
mbed_official 2:864ddfb70a9c 19 #include "ble/BLE.h"
mbed_official 2:864ddfb70a9c 20 #include "LEDService.h"
mbed_official 2:864ddfb70a9c 21
jurica238814 19:41b1396c50d2 22 #define NO_COMMAND (0)
jurica238814 19:41b1396c50d2 23 #define ZOOM_IN (1)
jurica238814 19:41b1396c50d2 24 #define ZOOM_OUT (2)
jurica238814 19:41b1396c50d2 25 #define FOCUS_NEAR (3)
jurica238814 19:41b1396c50d2 26 #define FOCUS_FAR (4)
jurica238814 19:41b1396c50d2 27 #define RECORD (5)
jurica238814 19:41b1396c50d2 28
jurica238814 19:41b1396c50d2 29 #define LANC_COMMAND_PIN (p2) // Pin connected to Tr to pull lanc bus down/up
jurica238814 19:41b1396c50d2 30 #define LANC_PIN (p3) // Lanc bus pin (to scan for START/STOP bits)
jurica238814 19:41b1396c50d2 31
jurica238814 18:d089fb6ff789 32
jurica238814 19:41b1396c50d2 33 #define LANC_BIT_DURATION (104) // One LANC bit duration in ms
jurica238814 19:41b1396c50d2 34 #define LANC_MIN_DELAY (5000)
jurica238814 19:41b1396c50d2 35 #define NUMBER_OF_REPETITIONS (1)
jurica238814 19:41b1396c50d2 36
jurica238814 19:41b1396c50d2 37 #define REC_CMD_1 (0x18)
jurica238814 19:41b1396c50d2 38 #define REC_CMD_2 (0x33)
jurica238814 19:41b1396c50d2 39 #define ZOOM_IN_CMD_4_1 (0x28)
jurica238814 19:41b1396c50d2 40 #define ZOOM_IN_CMD_4_2 (0x08)
jurica238814 19:41b1396c50d2 41 #define ZOOM_OUT_CMD_4_1 (0x28)
jurica238814 19:41b1396c50d2 42 #define ZOOM_OUT_CMD_4_2 (0x18)
jurica238814 19:41b1396c50d2 43 #define FOCUS_NEAR_1 (0x28)
jurica238814 19:41b1396c50d2 44 #define FOCUS_NEAR_2 (0x47)
jurica238814 19:41b1396c50d2 45 #define FOCUS_FAR_1 (0x28)
jurica238814 19:41b1396c50d2 46 #define FOCUS_FAR_2 (0x45)
jurica238814 18:d089fb6ff789 47
jurica238814 18:d089fb6ff789 48 DigitalOut alivenessLED(p26, 0);
jurica238814 18:d089fb6ff789 49 DigitalOut led_red(p22, 0);
jurica238814 18:d089fb6ff789 50 DigitalOut led_blue(p23, 0);
jurica238814 18:d089fb6ff789 51 DigitalOut led_green(p24, 0);
mbed_official 2:864ddfb70a9c 52
jurica238814 19:41b1396c50d2 53 void send_command(uint8_t command_1, uint8_t command_2);
jurica238814 19:41b1396c50d2 54
jurica238814 19:41b1396c50d2 55 DigitalOut command_pin LANC_COMMAND_PIN;
jurica238814 19:41b1396c50d2 56 DigitalIn lanc_pin LANC_PIN;
jurica238814 19:41b1396c50d2 57
jurica238814 19:41b1396c50d2 58 Timer timer;
jurica238814 19:41b1396c50d2 59
jurica238814 19:41b1396c50d2 60 const static char DEVICE_NAME[] = "Lanc";
mbed_official 2:864ddfb70a9c 61 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
mbed_official 2:864ddfb70a9c 62
mbed_official 2:864ddfb70a9c 63 static EventQueue eventQueue(
mbed_official 2:864ddfb70a9c 64 /* event count */ 10 * /* event size */ 32
mbed_official 2:864ddfb70a9c 65 );
mbed_official 2:864ddfb70a9c 66
mbed_official 2:864ddfb70a9c 67 LEDService *ledServicePtr;
mbed_official 2:864ddfb70a9c 68
jurica238814 18:d089fb6ff789 69 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
mbed_official 2:864ddfb70a9c 70 (void) params;
mbed_official 2:864ddfb70a9c 71 BLE::Instance().gap().startAdvertising();
mbed_official 2:864ddfb70a9c 72 }
mbed_official 2:864ddfb70a9c 73
jurica238814 18:d089fb6ff789 74 void blinkCallback(void){
mbed_official 2:864ddfb70a9c 75 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
mbed_official 2:864ddfb70a9c 76 }
mbed_official 2:864ddfb70a9c 77
mbed_official 2:864ddfb70a9c 78 /**
mbed_official 2:864ddfb70a9c 79 * This callback allows the LEDService to receive updates to the ledState Characteristic.
mbed_official 2:864ddfb70a9c 80 *
mbed_official 2:864ddfb70a9c 81 * @param[in] params
mbed_official 2:864ddfb70a9c 82 * Information about the characterisitc being updated.
mbed_official 2:864ddfb70a9c 83 */
mbed_official 2:864ddfb70a9c 84 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
mbed_official 2:864ddfb70a9c 85 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
jurica238814 18:d089fb6ff789 86 //actuatedLED = *(params->data);
jurica238814 18:d089fb6ff789 87 led_red = 1;
jurica238814 18:d089fb6ff789 88 led_blue = 1;
jurica238814 18:d089fb6ff789 89 led_green = 1;
jurica238814 18:d089fb6ff789 90 if (*(params->data)==ZOOM_IN){
jurica238814 18:d089fb6ff789 91 led_red = 0;
jurica238814 19:41b1396c50d2 92 send_command(ZOOM_IN_CMD_4_1,ZOOM_IN_CMD_4_2);
jurica238814 18:d089fb6ff789 93 }
jurica238814 18:d089fb6ff789 94 else if (*(params->data)==ZOOM_OUT){
jurica238814 18:d089fb6ff789 95 led_blue = 0;
jurica238814 19:41b1396c50d2 96 send_command(ZOOM_OUT_CMD_4_1,ZOOM_OUT_CMD_4_2);
jurica238814 18:d089fb6ff789 97 }
jurica238814 18:d089fb6ff789 98 else if (*(params->data)==FOCUS_NEAR){
jurica238814 18:d089fb6ff789 99 led_green = 0;
jurica238814 19:41b1396c50d2 100 send_command(FOCUS_NEAR_1, FOCUS_NEAR_2);
jurica238814 18:d089fb6ff789 101 }
jurica238814 18:d089fb6ff789 102 else if (*(params->data)==FOCUS_FAR){
jurica238814 18:d089fb6ff789 103 led_green = 0;
jurica238814 18:d089fb6ff789 104 led_red = 0;
jurica238814 19:41b1396c50d2 105 send_command(FOCUS_FAR_1,FOCUS_FAR_2);
jurica238814 18:d089fb6ff789 106 }
jurica238814 18:d089fb6ff789 107 else if (*(params->data)==RECORD){
jurica238814 18:d089fb6ff789 108 led_blue = 0;
jurica238814 18:d089fb6ff789 109 led_red = 0;
jurica238814 19:41b1396c50d2 110 send_command(REC_CMD_1, REC_CMD_2);
jurica238814 18:d089fb6ff789 111 }
jurica238814 18:d089fb6ff789 112 else if (*(params->data)==NO_COMMAND){
jurica238814 18:d089fb6ff789 113 // No command
jurica238814 18:d089fb6ff789 114 }
mbed_official 2:864ddfb70a9c 115 }
mbed_official 2:864ddfb70a9c 116 }
mbed_official 2:864ddfb70a9c 117
mbed_official 2:864ddfb70a9c 118 /**
mbed_official 2:864ddfb70a9c 119 * This function is called when the ble initialization process has failled
mbed_official 2:864ddfb70a9c 120 */
jurica238814 19:41b1396c50d2 121 void onBleInitError(BLE &ble, ble_error_t error){
mbed_official 2:864ddfb70a9c 122 /* Initialization error handling should go here */
mbed_official 2:864ddfb70a9c 123 }
mbed_official 2:864ddfb70a9c 124
mbed_official 2:864ddfb70a9c 125 /**
mbed_official 2:864ddfb70a9c 126 * Callback triggered when the ble initialization process has finished
mbed_official 2:864ddfb70a9c 127 */
jurica238814 19:41b1396c50d2 128 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
mbed_official 2:864ddfb70a9c 129 BLE& ble = params->ble;
mbed_official 2:864ddfb70a9c 130 ble_error_t error = params->error;
mbed_official 2:864ddfb70a9c 131
mbed_official 2:864ddfb70a9c 132 if (error != BLE_ERROR_NONE) {
mbed_official 2:864ddfb70a9c 133 /* In case of error, forward the error handling to onBleInitError */
mbed_official 2:864ddfb70a9c 134 onBleInitError(ble, error);
mbed_official 2:864ddfb70a9c 135 return;
mbed_official 2:864ddfb70a9c 136 }
mbed_official 2:864ddfb70a9c 137
mbed_official 2:864ddfb70a9c 138 /* Ensure that it is the default instance of BLE */
mbed_official 2:864ddfb70a9c 139 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
mbed_official 2:864ddfb70a9c 140 return;
mbed_official 2:864ddfb70a9c 141 }
mbed_official 2:864ddfb70a9c 142
mbed_official 2:864ddfb70a9c 143 ble.gap().onDisconnection(disconnectionCallback);
mbed_official 2:864ddfb70a9c 144 ble.gattServer().onDataWritten(onDataWrittenCallback);
mbed_official 2:864ddfb70a9c 145
mbed_official 2:864ddfb70a9c 146 bool initialValueForLEDCharacteristic = false;
mbed_official 2:864ddfb70a9c 147 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
mbed_official 2:864ddfb70a9c 148
mbed_official 2:864ddfb70a9c 149 /* setup advertising */
mbed_official 2:864ddfb70a9c 150 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
mbed_official 2:864ddfb70a9c 151 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
mbed_official 2:864ddfb70a9c 152 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
mbed_official 2:864ddfb70a9c 153 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
mbed_official 2:864ddfb70a9c 154 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
mbed_official 2:864ddfb70a9c 155 ble.gap().startAdvertising();
mbed_official 2:864ddfb70a9c 156 }
mbed_official 2:864ddfb70a9c 157
mbed_official 2:864ddfb70a9c 158 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 2:864ddfb70a9c 159 BLE &ble = BLE::Instance();
mbed_official 11:7404978b24e7 160 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 2:864ddfb70a9c 161 }
mbed_official 2:864ddfb70a9c 162
jurica238814 19:41b1396c50d2 163 void init_lanc_camera(void){
jurica238814 19:41b1396c50d2 164 // Function will set bus to HIGH and hold it for 5ms
jurica238814 19:41b1396c50d2 165
jurica238814 19:41b1396c50d2 166 }
jurica238814 19:41b1396c50d2 167
jurica238814 19:41b1396c50d2 168 void init_lanc_bus(){
jurica238814 19:41b1396c50d2 169 // Function initialize lanc bus by pulling it up
jurica238814 19:41b1396c50d2 170 // Bus goes HIGH by setting command pin to LOW
jurica238814 19:41b1396c50d2 171 command_pin = 0;
jurica238814 19:41b1396c50d2 172 wait_ms(LANC_MIN_DELAY);
jurica238814 19:41b1396c50d2 173 }
jurica238814 19:41b1396c50d2 174
jurica238814 19:41b1396c50d2 175 void send_command(uint8_t command_1, uint8_t command_2){
jurica238814 19:41b1396c50d2 176 uint8_t command_counter = 0;
jurica238814 19:41b1396c50d2 177
jurica238814 19:41b1396c50d2 178
jurica238814 19:41b1396c50d2 179 while(command_counter < NUMBER_OF_REPETITIONS){
jurica238814 19:41b1396c50d2 180 //Arduino's PulseIn function (inline implemetation)
jurica238814 19:41b1396c50d2 181 while (lanc_pin == 0); // Waiting for HIGH
jurica238814 19:41b1396c50d2 182 timer.start();
jurica238814 19:41b1396c50d2 183 while(lanc_pin == 1); // Waiting for LOW
jurica238814 19:41b1396c50d2 184 timer.stop();
jurica238814 19:41b1396c50d2 185 if(timer.read_ms()>LANC_MIN_DELAY){
jurica238814 19:41b1396c50d2 186 break;
jurica238814 19:41b1396c50d2 187 }
jurica238814 19:41b1396c50d2 188 }
jurica238814 19:41b1396c50d2 189
jurica238814 19:41b1396c50d2 190 // Program is out of the while loop -> bus is LOW -> START bit of BYTE_0 is here
jurica238814 19:41b1396c50d2 191
jurica238814 19:41b1396c50d2 192 // Write Byte 0 to the bus
jurica238814 19:41b1396c50d2 193 for(int i=0; i<8; i++){
jurica238814 19:41b1396c50d2 194 command_pin = (command_1 & (1<<i));
jurica238814 19:41b1396c50d2 195 wait_ms(LANC_BIT_DURATION);
jurica238814 19:41b1396c50d2 196 }
jurica238814 19:41b1396c50d2 197 // Put LANC bus back to HIGH
jurica238814 19:41b1396c50d2 198 command_pin = 0;
jurica238814 19:41b1396c50d2 199 // Make sure to be in the stop bit before the Byte 1
jurica238814 19:41b1396c50d2 200 wait_ms(10);
jurica238814 19:41b1396c50d2 201
jurica238814 19:41b1396c50d2 202 // Look while lanc bus is HIGH for STOP bit
jurica238814 19:41b1396c50d2 203 while(lanc_pin == 1);
jurica238814 19:41b1396c50d2 204
jurica238814 19:41b1396c50d2 205 wait_ms(LANC_BIT_DURATION);
jurica238814 19:41b1396c50d2 206 //Write Byte 1 to the bus
jurica238814 19:41b1396c50d2 207 for(int i=0; i<8; i++){
jurica238814 19:41b1396c50d2 208 command_pin = (command_2 & (1<<i));
jurica238814 19:41b1396c50d2 209 wait_ms(LANC_BIT_DURATION);
jurica238814 19:41b1396c50d2 210 }
jurica238814 19:41b1396c50d2 211 // Put LANC bus back to HIGH
jurica238814 19:41b1396c50d2 212 command_pin = 0;
jurica238814 19:41b1396c50d2 213 command_counter++;
jurica238814 19:41b1396c50d2 214
jurica238814 19:41b1396c50d2 215 /*
jurica238814 19:41b1396c50d2 216 * Control Bytes 0 and 1 are written. Program does not care what happend in
jurica238814 19:41b1396c50d2 217 * Bytes 2-7 and just waits for next start bit after a long pause to send
jurica238814 19:41b1396c50d2 218 * command again
jurica238814 19:41b1396c50d2 219 */
jurica238814 19:41b1396c50d2 220 }
jurica238814 19:41b1396c50d2 221
jurica238814 19:41b1396c50d2 222
jurica238814 18:d089fb6ff789 223 int main(){
jurica238814 19:41b1396c50d2 224 // Clear RGB led
jurica238814 18:d089fb6ff789 225 led_red = 1;
jurica238814 18:d089fb6ff789 226 led_green = 1;
jurica238814 18:d089fb6ff789 227 led_blue = 1;
jurica238814 19:41b1396c50d2 228
jurica238814 19:41b1396c50d2 229 init_lanc_bus();
jurica238814 19:41b1396c50d2 230
jurica238814 18:d089fb6ff789 231 eventQueue.call_every(50, blinkCallback);
mbed_official 2:864ddfb70a9c 232 BLE &ble = BLE::Instance();
mbed_official 2:864ddfb70a9c 233 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 2:864ddfb70a9c 234 ble.init(bleInitComplete);
mbed_official 2:864ddfb70a9c 235
mbed_official 11:7404978b24e7 236 eventQueue.dispatch_forever();
mbed_official 2:864ddfb70a9c 237
mbed_official 2:864ddfb70a9c 238 return 0;
mbed_official 2:864ddfb70a9c 239 }