mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitComponent.h Source File

MicroBitComponent.h

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 #ifndef MICROBIT_COMPONENT_H
00027 #define MICROBIT_COMPONENT_H
00028 
00029 #include "MicroBitConfig.h"
00030 
00031 // Enumeration of core components.
00032 #define MICROBIT_ID_BUTTON_A            1
00033 #define MICROBIT_ID_BUTTON_B            2
00034 #define MICROBIT_ID_BUTTON_RESET        3
00035 #define MICROBIT_ID_ACCELEROMETER       4
00036 #define MICROBIT_ID_COMPASS             5
00037 #define MICROBIT_ID_DISPLAY             6
00038 
00039 //EDGE connector events
00040 #define MICROBIT_IO_PINS                20
00041 
00042 #define MICROBIT_ID_IO_P0               7           //P0 is the left most pad (ANALOG/DIGITAL)
00043 #define MICROBIT_ID_IO_P1               8           //P1 is the middle pad (ANALOG/DIGITAL)
00044 #define MICROBIT_ID_IO_P2               9           //P2 is the right most pad (ANALOG/DIGITAL)
00045 #define MICROBIT_ID_IO_P3               10          //COL1 (ANALOG/DIGITAL)
00046 #define MICROBIT_ID_IO_P4               11          //BTN_A
00047 #define MICROBIT_ID_IO_P5               12          //COL2 (ANALOG/DIGITAL)
00048 #define MICROBIT_ID_IO_P6               13          //ROW2
00049 #define MICROBIT_ID_IO_P7               14          //ROW1
00050 #define MICROBIT_ID_IO_P8               15          //PIN 18
00051 #define MICROBIT_ID_IO_P9               16          //ROW3
00052 #define MICROBIT_ID_IO_P10              17          //COL3 (ANALOG/DIGITAL)
00053 #define MICROBIT_ID_IO_P11              18          //BTN_B
00054 #define MICROBIT_ID_IO_P12              19          //PIN 20
00055 #define MICROBIT_ID_IO_P13              20          //SCK
00056 #define MICROBIT_ID_IO_P14              21          //MISO
00057 #define MICROBIT_ID_IO_P15              22          //MOSI
00058 #define MICROBIT_ID_IO_P16              23          //PIN 16
00059 #define MICROBIT_ID_IO_P19              24          //SCL
00060 #define MICROBIT_ID_IO_P20              25          //SDA
00061 
00062 #define MICROBIT_ID_BUTTON_AB           26          // Button A+B multibutton
00063 #define MICROBIT_ID_GESTURE             27          // Gesture events
00064 
00065 #define MICROBIT_ID_THERMOMETER         28
00066 #define MICROBIT_ID_RADIO               29
00067 #define MICROBIT_ID_RADIO_DATA_READY    30
00068 #define MICROBIT_ID_MULTIBUTTON_ATTACH  31
00069 #define MICROBIT_ID_SERIAL              32
00070 
00071 #define MICROBIT_ID_MESSAGE_BUS_LISTENER            1021          // Message bus indication that a handler for a given ID has been registered.
00072 #define MICROBIT_ID_NOTIFY_ONE                      1022          // Notfication channel, for general purpose synchronisation
00073 #define MICROBIT_ID_NOTIFY                          1023          // Notfication channel, for general purpose synchronisation
00074 
00075 // Universal flags used as part of the status field
00076 #define MICROBIT_COMPONENT_RUNNING      0x01
00077 
00078 
00079 /**
00080   * Class definition for MicroBitComponent.
00081   *
00082   * All components should inherit from this class.
00083   *
00084   * If a component requires regular updates, then that component can be added to the 
00085   * to the systemTick and/or idleTick queues. This provides a simple, extensible mechanism
00086   * for code that requires periodic/occasional background processing but does not warrant
00087   * the complexity of maintaining its own thread. 
00088   *
00089   * Two levels of support are available. 
00090   *
00091   * systemTick() provides a periodic callback during the
00092   * micro:bit's system timer interrupt. This provides a guaranteed periodic callback, but in interrupt context
00093   * and is suitable for code with lightweight processing requirements, but strict time constraints.
00094   * 
00095   * idleTick() provides a periodic callback whenever the scheduler is idle. This provides occasional, callbacks
00096   * in the main thread context, but with no guarantees of frequency. This is suitable for non-urgent background tasks.
00097   *
00098   * Components wishing to use these facilities should override the systemTick and/or idleTick functions defined here, and
00099   * register their components using system_timer_add_component() fiber_add_idle_component() respectively.
00100   *
00101   */
00102 class MicroBitComponent
00103 {
00104     protected:
00105 
00106     uint16_t id;                    // Event Bus ID of this component
00107     uint8_t status;                 // Component defined state.
00108 
00109     public:
00110 
00111     /**
00112       * The default constructor of a MicroBitComponent
00113       */
00114     MicroBitComponent()
00115     {
00116         this->id = 0;
00117         this->status = 0;
00118     }
00119 
00120     /**
00121       * The system timer will call this member function once the component has been added to
00122       * the array of system components using system_timer_add_component. This callback
00123       * will be in interrupt context.
00124       */
00125     virtual void systemTick()
00126     {
00127     }
00128 
00129     /**
00130       * The idle thread will call this member function once the component has been added to the array
00131       * of idle components using fiber_add_idle_component. 
00132       */
00133     virtual void idleTick()
00134     {
00135     }
00136 
00137     /**
00138       * If you have added your component to the idle or system tick component arrays,
00139       * you must remember to remove your component from them if your component is destructed.
00140       */
00141     virtual ~MicroBitComponent()
00142     {
00143     }
00144 };
00145 
00146 #endif