Eddystone test using modified DAL

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit-eddystone

Fork of microbit-dal by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitLightSensor.h Source File

MicroBitLightSensor.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_LIGHT_SENSOR_H
00027 #define MICROBIT_LIGHT_SENSOR_H
00028 
00029 #include "mbed.h"
00030 #include "MicroBitConfig.h"
00031 #include "MicroBitComponent.h"
00032 #include "EventModel.h"
00033 #include "MicroBitMatrixMaps.h"
00034 
00035 #define MICROBIT_LIGHT_SENSOR_CHAN_NUM      3
00036 #define MICROBIT_LIGHT_SENSOR_AN_SET_TIME   4000
00037 #define MICROBIT_LIGHT_SENSOR_TICK_PERIOD   5
00038 
00039 #define MICROBIT_LIGHT_SENSOR_MAX_VALUE     338
00040 #define MICROBIT_LIGHT_SENSOR_MIN_VALUE     75
00041 
00042 /**
00043   * Class definition for MicroBitLightSensor.
00044   *
00045   * This is an object that interleaves light sensing with MicroBitDisplay.
00046   */
00047 class MicroBitLightSensor
00048 {
00049 
00050     //contains the results from each section of the display
00051     int results[MICROBIT_LIGHT_SENSOR_CHAN_NUM];
00052 
00053     //holds the current channel (also used to index the results array)
00054     uint8_t chan;
00055 
00056     //a Timeout which triggers our analogReady() call
00057     Timeout analogTrigger;
00058 
00059     //a pointer the currently sensed pin, represented as an AnalogIn
00060     AnalogIn* sensePin;
00061 
00062     const MatrixMap &matrixMap;
00063 
00064     /**
00065       * After the startSensing method has been called, this method will be called
00066       * MICROBIT_LIGHT_SENSOR_AN_SET_TIME after.
00067       *
00068       * It will then read from the currently selected channel using the AnalogIn
00069       * that was configured in the startSensing method.
00070       */
00071     void analogReady();
00072 
00073     /**
00074       * Forcibly disables the AnalogIn, otherwise it will remain in possession
00075       * of the GPIO channel it is using, meaning that the display will not be
00076       * able to use a channel (COL).
00077       *
00078       * This is required as per PAN 3, details of which can be found here:
00079       *
00080       * https://www.nordicsemi.com/eng/nordic/download_resource/24634/5/88440387
00081       */
00082     void analogDisable();
00083 
00084     public:
00085 
00086     /**
00087       * Constructor.
00088       *
00089       * Create a representation of the light sensor.
00090       *
00091       * @param map The mapping information that relates pin inputs/outputs to physical screen coordinates.
00092       *            Defaults to microbitMatrixMap, defined in MicroBitMatrixMaps.h.
00093       */
00094     MicroBitLightSensor(const MatrixMap &map);
00095 
00096     /**
00097       * This method returns a summed average of the three sections of the display.
00098       *
00099       * A section is defined as:
00100       *  ___________________
00101       * | 1 |   | 2 |   | 3 |
00102       * |___|___|___|___|___|
00103       * |   |   |   |   |   |
00104       * |___|___|___|___|___|
00105       * | 2 |   | 3 |   | 1 |
00106       * |___|___|___|___|___|
00107       * |   |   |   |   |   |
00108       * |___|___|___|___|___|
00109       * | 3 |   | 1 |   | 2 |
00110       * |___|___|___|___|___|
00111       *
00112       * Where each number represents a different section on the 5 x 5 matrix display.
00113       *
00114       * @return returns a value in the range 0 - 255 where 0 is dark, and 255
00115       * is very bright
00116       */
00117     int read();
00118 
00119     /**
00120       * The method that is invoked by sending MICROBIT_DISPLAY_EVT_LIGHT_SENSE
00121       * using the id MICROBIT_ID_DISPLAY.
00122       *
00123       * @note this can be manually driven by calling this member function, with
00124       *       a MicroBitEvent using the CREATE_ONLY option of the MicroBitEvent
00125       *       constructor.
00126       */
00127     void startSensing(MicroBitEvent);
00128 
00129     /**
00130       * A destructor for MicroBitLightSensor.
00131       *
00132       * The destructor removes the listener, used by MicroBitLightSensor from the default EventModel.
00133       */
00134     ~MicroBitLightSensor();
00135 };
00136 
00137 #endif