Damien Abos / microbit-samples

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BTSync.cpp Source File

BTSync.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #include "MicroBit.h"
00027 #include "MicroBitSamples.h"
00028 
00029 #ifdef MICROBIT_SAMPLE_BT_SYNC
00030 
00031 MicroBit uBit;
00032 
00033 uint16_t value;
00034 
00035 void increment()
00036 {
00037     if (value < 57) {
00038         value = value + 1;
00039         // Indicate that a new sample is available
00040         MicroBitEvent e(MICROBIT_ID_VALUE, MICROBIT_VALUE_EVT_UPDATE);
00041     }
00042 
00043 #if CONFIG_ENABLED(MICROBIT_DBG)
00044     if(SERIAL_DEBUG) SERIAL_DEBUG->printf("++\r\n");
00045 #endif
00046 }
00047 
00048 void decrement()
00049 {
00050     if (value > 48) {
00051         value = value - 1;
00052         // Indicate that a new sample is available
00053         MicroBitEvent e(MICROBIT_ID_VALUE, MICROBIT_VALUE_EVT_UPDATE);
00054     }
00055 
00056 #if CONFIG_ENABLED(MICROBIT_DBG)
00057     if(SERIAL_DEBUG) SERIAL_DEBUG->printf("--\r\n");
00058 #endif
00059 }
00060 
00061 void onValueChanged(MicroBitEvent e)
00062 {
00063 #if CONFIG_ENABLED(MICROBIT_DBG)
00064     if(SERIAL_DEBUG) SERIAL_DEBUG->printf("valueChanged\r\n");
00065 #endif
00066     uBit.display.printChar(value);
00067 }
00068 
00069 int main()
00070 {
00071     // Initialise the micro:bit runtime.
00072     uBit.init();
00073     
00074     MicroBitSerial serial(USBTX, USBRX);
00075 
00076 #if CONFIG_ENABLED(MICROBIT_DBG)
00077     if(SERIAL_DEBUG) SERIAL_DEBUG->printf("Init...\r\n");
00078 #endif
00079     
00080     value = 48;
00081 
00082     new MicroBitValueService(*uBit.ble, &value);
00083 
00084 
00085 #if CONFIG_ENABLED(MICROBIT_DBG)
00086     if(SERIAL_DEBUG) SERIAL_DEBUG->printf("Started!\r\n");
00087 #endif
00088 
00089     uBit.display.scroll("Hi Bernard Controls! from GFI :)");
00090 
00091     while(1) {
00092         if (uBit.buttonA.isPressed()) {
00093             decrement();
00094         } else if (uBit.buttonB.isPressed()) {
00095             increment();
00096         }
00097         uBit.display.printChar(value);
00098         uBit.sleep(100);
00099     }
00100 }
00101 
00102 #endif