The IR Puck can mimic arbitrary infrared remote controls. Built on the Puck IOT platform.
Dependencies: Puck IRSender mbed
The IR Puck is a puck that can mimic arbitrary infrared remote controls. This is useful for controlling things like TVs, radios, airconditioners, window blinds, and just about anything and everything that can be otherwise be controlled by a regular remote control.
A tutorial for the IR Puck is available on GitHub.
Tutorials and in-depth documentation for the Puck platform is available at the project's GitHub page
main.cpp@4:24d9873936e6, 2014-07-15 (annotated)
- Committer:
- stiaje
- Date:
- Tue Jul 15 08:49:45 2014 +0000
- Revision:
- 4:24d9873936e6
- Parent:
- 1:e92c3b50191d
- Child:
- 5:3642c0af497e
Wait for bluetooth disconnect before sending IR signal.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sigveseb | 0:c94311378ec1 | 1 | #include "mbed.h" |
sigveseb | 0:c94311378ec1 | 2 | #include "BLEDevice.h" |
sigveseb | 0:c94311378ec1 | 3 | #include "IR.h" |
sigveseb | 0:c94311378ec1 | 4 | #include "nRF51822n.h" |
sigveseb | 0:c94311378ec1 | 5 | |
sigveseb | 0:c94311378ec1 | 6 | BLEDevice ble; |
sigveseb | 0:c94311378ec1 | 7 | |
sigveseb | 0:c94311378ec1 | 8 | DigitalOut myled(LED1); |
sigveseb | 0:c94311378ec1 | 9 | DigitalOut yourled(LED2); |
sigveseb | 0:c94311378ec1 | 10 | nRF51822n nrf; |
sigveseb | 0:c94311378ec1 | 11 | |
sigveseb | 0:c94311378ec1 | 12 | Serial py(USBTX, USBRX); |
sigveseb | 0:c94311378ec1 | 13 | |
sigveseb | 0:c94311378ec1 | 14 | const static uint8_t beaconPayload[] = { |
stiaje | 4:24d9873936e6 | 15 | 0x00, 0x00, // Company identifier code (0x004C == Apple) - Should be all zero until we figure out this weird iOS bug |
sigveseb | 0:c94311378ec1 | 16 | 0x02, // ID |
sigveseb | 0:c94311378ec1 | 17 | 0x15, // length of the remaining payload |
sigveseb | 0:c94311378ec1 | 18 | 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID |
sigveseb | 0:c94311378ec1 | 19 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, |
sigveseb | 0:c94311378ec1 | 20 | 0x13, 0x37, // the major value to differenciate a location |
sigveseb | 0:c94311378ec1 | 21 | 0xFA, 0xCE, // the minor value to differenciate a location |
sigveseb | 0:c94311378ec1 | 22 | 0xC8 // 2's complement of the Tx power (-56dB) |
sigveseb | 0:c94311378ec1 | 23 | }; |
sigveseb | 0:c94311378ec1 | 24 | |
sigveseb | 0:c94311378ec1 | 25 | extern GattService ir_service; |
sigveseb | 0:c94311378ec1 | 26 | extern GattCharacteristic header, one, zero, ptrail, predata, code; |
sigveseb | 0:c94311378ec1 | 27 | |
stiaje | 4:24d9873936e6 | 28 | bool received_ir_transmission = false; |
stiaje | 4:24d9873936e6 | 29 | bool device_disconnected = true; |
stiaje | 4:24d9873936e6 | 30 | |
sigveseb | 0:c94311378ec1 | 31 | void onDataWritten(uint16_t handle) |
sigveseb | 0:c94311378ec1 | 32 | { |
sigveseb | 0:c94311378ec1 | 33 | py.printf("Data written! %i\n", handle); |
sigveseb | 0:c94311378ec1 | 34 | for (int i = 0; i < ir_service.getCharacteristicCount(); i++) { |
sigveseb | 0:c94311378ec1 | 35 | GattCharacteristic* characteristic = ir_service.getCharacteristic(i); |
sigveseb | 0:c94311378ec1 | 36 | characteristic->getMaxLength(); |
sigveseb | 0:c94311378ec1 | 37 | if (characteristic->getHandle() == handle) { |
sigveseb | 0:c94311378ec1 | 38 | uint16_t max_length = characteristic->getMaxLength(); |
sigveseb | 0:c94311378ec1 | 39 | ble.readCharacteristicValue(handle, characteristic->getValuePtr(), &max_length); |
aleksanb | 1:e92c3b50191d | 40 | for (int i=0; i<max_length; i++) { |
aleksanb | 1:e92c3b50191d | 41 | py.printf("Got value: %d\n", characteristic->getValuePtr()[i]); |
aleksanb | 1:e92c3b50191d | 42 | } |
sigveseb | 0:c94311378ec1 | 43 | break; |
sigveseb | 0:c94311378ec1 | 44 | } |
sigveseb | 0:c94311378ec1 | 45 | } |
aleksanb | 1:e92c3b50191d | 46 | |
sigveseb | 0:c94311378ec1 | 47 | if (code.getHandle() == handle) { |
stiaje | 4:24d9873936e6 | 48 | py.printf("Received complete instruction\n"); |
stiaje | 4:24d9873936e6 | 49 | received_ir_transmission = true; |
sigveseb | 0:c94311378ec1 | 50 | } |
sigveseb | 0:c94311378ec1 | 51 | } |
sigveseb | 0:c94311378ec1 | 52 | |
sigveseb | 0:c94311378ec1 | 53 | void disconnectionCallback(void) |
sigveseb | 0:c94311378ec1 | 54 | { |
sigveseb | 0:c94311378ec1 | 55 | py.printf("Disconnected!\n"); |
stiaje | 4:24d9873936e6 | 56 | device_disconnected = true; |
sigveseb | 0:c94311378ec1 | 57 | } |
sigveseb | 0:c94311378ec1 | 58 | |
sigveseb | 0:c94311378ec1 | 59 | void connectionCallback(void) |
sigveseb | 0:c94311378ec1 | 60 | { |
aleksanb | 1:e92c3b50191d | 61 | py.printf("Connected!\n"); |
stiaje | 4:24d9873936e6 | 62 | device_disconnected = false; |
sigveseb | 0:c94311378ec1 | 63 | } |
sigveseb | 0:c94311378ec1 | 64 | |
sigveseb | 0:c94311378ec1 | 65 | void onDataSent(uint16_t data) |
sigveseb | 0:c94311378ec1 | 66 | { |
sigveseb | 0:c94311378ec1 | 67 | py.printf("onDataSent!\n"); |
sigveseb | 0:c94311378ec1 | 68 | } |
sigveseb | 0:c94311378ec1 | 69 | |
aleksanb | 1:e92c3b50191d | 70 | int main() |
aleksanb | 1:e92c3b50191d | 71 | { |
aleksanb | 1:e92c3b50191d | 72 | py.printf("Start of main\n"); |
aleksanb | 1:e92c3b50191d | 73 | |
sigveseb | 0:c94311378ec1 | 74 | ble.init(); |
sigveseb | 0:c94311378ec1 | 75 | ble.onConnection(connectionCallback); |
sigveseb | 0:c94311378ec1 | 76 | ble.onDisconnection(disconnectionCallback); |
sigveseb | 0:c94311378ec1 | 77 | ble.onDataWritten(onDataWritten); |
sigveseb | 0:c94311378ec1 | 78 | ble.onDataSent(onDataSent); |
sigveseb | 0:c94311378ec1 | 79 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
sigveseb | 0:c94311378ec1 | 80 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
sigveseb | 0:c94311378ec1 | 81 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
aleksanb | 1:e92c3b50191d | 82 | |
sigveseb | 0:c94311378ec1 | 83 | ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, |
aleksanb | 1:e92c3b50191d | 84 | beaconPayload, sizeof(beaconPayload)); |
aleksanb | 1:e92c3b50191d | 85 | |
sigveseb | 0:c94311378ec1 | 86 | ble.startAdvertising(); |
sigveseb | 0:c94311378ec1 | 87 | |
sigveseb | 0:c94311378ec1 | 88 | ble.addService(ir_service); |
aleksanb | 1:e92c3b50191d | 89 | |
sigveseb | 0:c94311378ec1 | 90 | myled = 1; |
aleksanb | 1:e92c3b50191d | 91 | |
aleksanb | 1:e92c3b50191d | 92 | py.printf("Listening..\n"); |
sigveseb | 0:c94311378ec1 | 93 | |
sigveseb | 0:c94311378ec1 | 94 | while (true) { |
sigveseb | 0:c94311378ec1 | 95 | ble.waitForEvent(); |
stiaje | 4:24d9873936e6 | 96 | if (received_ir_transmission && device_disconnected) { |
aleksanb | 1:e92c3b50191d | 97 | fireIRCode(header.getValuePtr(), one.getValuePtr(), zero.getValuePtr(), ptrail.getValuePtr(), predata.getValuePtr(), code.getValuePtr()); |
stiaje | 4:24d9873936e6 | 98 | received_ir_transmission = false; |
stiaje | 4:24d9873936e6 | 99 | ble.startAdvertising(); |
aleksanb | 1:e92c3b50191d | 100 | } |
sigveseb | 0:c94311378ec1 | 101 | myled = !myled; |
sigveseb | 0:c94311378ec1 | 102 | } |
sigveseb | 0:c94311378ec1 | 103 | } |