Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/source/drivers/MicroBitLightSensor.cpp@0:b9164b348919, 2016-10-17 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 17 12:41:20 2016 +0000
- Revision:
- 0:b9164b348919
Official Sheffield ARMBand Micro:bit program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 0:b9164b348919 | 1 | /* |
MrBedfordVan | 0:b9164b348919 | 2 | The MIT License (MIT) |
MrBedfordVan | 0:b9164b348919 | 3 | |
MrBedfordVan | 0:b9164b348919 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
MrBedfordVan | 0:b9164b348919 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
MrBedfordVan | 0:b9164b348919 | 6 | |
MrBedfordVan | 0:b9164b348919 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
MrBedfordVan | 0:b9164b348919 | 8 | copy of this software and associated documentation files (the "Software"), |
MrBedfordVan | 0:b9164b348919 | 9 | to deal in the Software without restriction, including without limitation |
MrBedfordVan | 0:b9164b348919 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MrBedfordVan | 0:b9164b348919 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
MrBedfordVan | 0:b9164b348919 | 12 | Software is furnished to do so, subject to the following conditions: |
MrBedfordVan | 0:b9164b348919 | 13 | |
MrBedfordVan | 0:b9164b348919 | 14 | The above copyright notice and this permission notice shall be included in |
MrBedfordVan | 0:b9164b348919 | 15 | all copies or substantial portions of the Software. |
MrBedfordVan | 0:b9164b348919 | 16 | |
MrBedfordVan | 0:b9164b348919 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MrBedfordVan | 0:b9164b348919 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MrBedfordVan | 0:b9164b348919 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
MrBedfordVan | 0:b9164b348919 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MrBedfordVan | 0:b9164b348919 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
MrBedfordVan | 0:b9164b348919 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
MrBedfordVan | 0:b9164b348919 | 23 | DEALINGS IN THE SOFTWARE. |
MrBedfordVan | 0:b9164b348919 | 24 | */ |
MrBedfordVan | 0:b9164b348919 | 25 | |
MrBedfordVan | 0:b9164b348919 | 26 | /** |
MrBedfordVan | 0:b9164b348919 | 27 | * Class definition for MicroBitLightSensor. |
MrBedfordVan | 0:b9164b348919 | 28 | * |
MrBedfordVan | 0:b9164b348919 | 29 | * This is an object that interleaves light sensing with MicroBitDisplay. |
MrBedfordVan | 0:b9164b348919 | 30 | */ |
MrBedfordVan | 0:b9164b348919 | 31 | |
MrBedfordVan | 0:b9164b348919 | 32 | #include "MicroBitConfig.h" |
MrBedfordVan | 0:b9164b348919 | 33 | #include "MicroBitLightSensor.h" |
MrBedfordVan | 0:b9164b348919 | 34 | #include "MicroBitDisplay.h" |
MrBedfordVan | 0:b9164b348919 | 35 | |
MrBedfordVan | 0:b9164b348919 | 36 | /** |
MrBedfordVan | 0:b9164b348919 | 37 | * After the startSensing method has been called, this method will be called |
MrBedfordVan | 0:b9164b348919 | 38 | * MICROBIT_LIGHT_SENSOR_AN_SET_TIME after. |
MrBedfordVan | 0:b9164b348919 | 39 | * |
MrBedfordVan | 0:b9164b348919 | 40 | * It will then read from the currently selected channel using the AnalogIn |
MrBedfordVan | 0:b9164b348919 | 41 | * that was configured in the startSensing method. |
MrBedfordVan | 0:b9164b348919 | 42 | */ |
MrBedfordVan | 0:b9164b348919 | 43 | void MicroBitLightSensor::analogReady() |
MrBedfordVan | 0:b9164b348919 | 44 | { |
MrBedfordVan | 0:b9164b348919 | 45 | this->results[chan] = this->sensePin->read_u16(); |
MrBedfordVan | 0:b9164b348919 | 46 | |
MrBedfordVan | 0:b9164b348919 | 47 | analogDisable(); |
MrBedfordVan | 0:b9164b348919 | 48 | |
MrBedfordVan | 0:b9164b348919 | 49 | DigitalOut((PinName)(matrixMap.columnStart + chan)).write(1); |
MrBedfordVan | 0:b9164b348919 | 50 | |
MrBedfordVan | 0:b9164b348919 | 51 | chan++; |
MrBedfordVan | 0:b9164b348919 | 52 | |
MrBedfordVan | 0:b9164b348919 | 53 | chan = chan % MICROBIT_LIGHT_SENSOR_CHAN_NUM; |
MrBedfordVan | 0:b9164b348919 | 54 | } |
MrBedfordVan | 0:b9164b348919 | 55 | |
MrBedfordVan | 0:b9164b348919 | 56 | /** |
MrBedfordVan | 0:b9164b348919 | 57 | * Forcibly disables the AnalogIn, otherwise it will remain in possession |
MrBedfordVan | 0:b9164b348919 | 58 | * of the GPIO channel it is using, meaning that the display will not be |
MrBedfordVan | 0:b9164b348919 | 59 | * able to use a channel (COL). |
MrBedfordVan | 0:b9164b348919 | 60 | * |
MrBedfordVan | 0:b9164b348919 | 61 | * This is required as per PAN 3, details of which can be found here: |
MrBedfordVan | 0:b9164b348919 | 62 | * |
MrBedfordVan | 0:b9164b348919 | 63 | * https://www.nordicsemi.com/eng/nordic/download_resource/24634/5/88440387 |
MrBedfordVan | 0:b9164b348919 | 64 | */ |
MrBedfordVan | 0:b9164b348919 | 65 | void MicroBitLightSensor::analogDisable() |
MrBedfordVan | 0:b9164b348919 | 66 | { |
MrBedfordVan | 0:b9164b348919 | 67 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Disabled; |
MrBedfordVan | 0:b9164b348919 | 68 | |
MrBedfordVan | 0:b9164b348919 | 69 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos) | |
MrBedfordVan | 0:b9164b348919 | 70 | (ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling << ADC_CONFIG_INPSEL_Pos) | |
MrBedfordVan | 0:b9164b348919 | 71 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
MrBedfordVan | 0:b9164b348919 | 72 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
MrBedfordVan | 0:b9164b348919 | 73 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
MrBedfordVan | 0:b9164b348919 | 74 | } |
MrBedfordVan | 0:b9164b348919 | 75 | |
MrBedfordVan | 0:b9164b348919 | 76 | /** |
MrBedfordVan | 0:b9164b348919 | 77 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 78 | * |
MrBedfordVan | 0:b9164b348919 | 79 | * Create a representation of the light sensor. |
MrBedfordVan | 0:b9164b348919 | 80 | * |
MrBedfordVan | 0:b9164b348919 | 81 | * @param map The mapping information that relates pin inputs/outputs to physical screen coordinates. |
MrBedfordVan | 0:b9164b348919 | 82 | * Defaults to microbitMatrixMap, defined in MicroBitMatrixMaps.h. |
MrBedfordVan | 0:b9164b348919 | 83 | */ |
MrBedfordVan | 0:b9164b348919 | 84 | MicroBitLightSensor::MicroBitLightSensor(const MatrixMap &map) : |
MrBedfordVan | 0:b9164b348919 | 85 | analogTrigger(), |
MrBedfordVan | 0:b9164b348919 | 86 | matrixMap(map) |
MrBedfordVan | 0:b9164b348919 | 87 | { |
MrBedfordVan | 0:b9164b348919 | 88 | this->chan = 0; |
MrBedfordVan | 0:b9164b348919 | 89 | |
MrBedfordVan | 0:b9164b348919 | 90 | for(int i = 0; i < MICROBIT_LIGHT_SENSOR_CHAN_NUM; i++) |
MrBedfordVan | 0:b9164b348919 | 91 | results[i] = 0; |
MrBedfordVan | 0:b9164b348919 | 92 | |
MrBedfordVan | 0:b9164b348919 | 93 | if (EventModel::defaultEventBus) |
MrBedfordVan | 0:b9164b348919 | 94 | EventModel::defaultEventBus->listen(MICROBIT_ID_DISPLAY, MICROBIT_DISPLAY_EVT_LIGHT_SENSE, this, &MicroBitLightSensor::startSensing, MESSAGE_BUS_LISTENER_IMMEDIATE); |
MrBedfordVan | 0:b9164b348919 | 95 | |
MrBedfordVan | 0:b9164b348919 | 96 | this->sensePin = NULL; |
MrBedfordVan | 0:b9164b348919 | 97 | } |
MrBedfordVan | 0:b9164b348919 | 98 | |
MrBedfordVan | 0:b9164b348919 | 99 | /** |
MrBedfordVan | 0:b9164b348919 | 100 | * This method returns a summed average of the three sections of the display. |
MrBedfordVan | 0:b9164b348919 | 101 | * |
MrBedfordVan | 0:b9164b348919 | 102 | * A section is defined as: |
MrBedfordVan | 0:b9164b348919 | 103 | * ___________________ |
MrBedfordVan | 0:b9164b348919 | 104 | * | 1 | | 2 | | 3 | |
MrBedfordVan | 0:b9164b348919 | 105 | * |___|___|___|___|___| |
MrBedfordVan | 0:b9164b348919 | 106 | * | | | | | | |
MrBedfordVan | 0:b9164b348919 | 107 | * |___|___|___|___|___| |
MrBedfordVan | 0:b9164b348919 | 108 | * | 2 | | 3 | | 1 | |
MrBedfordVan | 0:b9164b348919 | 109 | * |___|___|___|___|___| |
MrBedfordVan | 0:b9164b348919 | 110 | * | | | | | | |
MrBedfordVan | 0:b9164b348919 | 111 | * |___|___|___|___|___| |
MrBedfordVan | 0:b9164b348919 | 112 | * | 3 | | 1 | | 2 | |
MrBedfordVan | 0:b9164b348919 | 113 | * |___|___|___|___|___| |
MrBedfordVan | 0:b9164b348919 | 114 | * |
MrBedfordVan | 0:b9164b348919 | 115 | * Where each number represents a different section on the 5 x 5 matrix display. |
MrBedfordVan | 0:b9164b348919 | 116 | * |
MrBedfordVan | 0:b9164b348919 | 117 | * @return returns a value in the range 0 - 255 where 0 is dark, and 255 |
MrBedfordVan | 0:b9164b348919 | 118 | * is very bright |
MrBedfordVan | 0:b9164b348919 | 119 | */ |
MrBedfordVan | 0:b9164b348919 | 120 | int MicroBitLightSensor::read() |
MrBedfordVan | 0:b9164b348919 | 121 | { |
MrBedfordVan | 0:b9164b348919 | 122 | int sum = 0; |
MrBedfordVan | 0:b9164b348919 | 123 | |
MrBedfordVan | 0:b9164b348919 | 124 | for(int i = 0; i < MICROBIT_LIGHT_SENSOR_CHAN_NUM; i++) |
MrBedfordVan | 0:b9164b348919 | 125 | sum += results[i]; |
MrBedfordVan | 0:b9164b348919 | 126 | |
MrBedfordVan | 0:b9164b348919 | 127 | int average = sum / MICROBIT_LIGHT_SENSOR_CHAN_NUM; |
MrBedfordVan | 0:b9164b348919 | 128 | |
MrBedfordVan | 0:b9164b348919 | 129 | average = min(average, MICROBIT_LIGHT_SENSOR_MAX_VALUE); |
MrBedfordVan | 0:b9164b348919 | 130 | |
MrBedfordVan | 0:b9164b348919 | 131 | average = max(average, MICROBIT_LIGHT_SENSOR_MIN_VALUE); |
MrBedfordVan | 0:b9164b348919 | 132 | |
MrBedfordVan | 0:b9164b348919 | 133 | int inverted = (MICROBIT_LIGHT_SENSOR_MAX_VALUE - average) + MICROBIT_LIGHT_SENSOR_MIN_VALUE; |
MrBedfordVan | 0:b9164b348919 | 134 | |
MrBedfordVan | 0:b9164b348919 | 135 | int a = 0; |
MrBedfordVan | 0:b9164b348919 | 136 | |
MrBedfordVan | 0:b9164b348919 | 137 | int b = 255; |
MrBedfordVan | 0:b9164b348919 | 138 | |
MrBedfordVan | 0:b9164b348919 | 139 | int normalised = a + ((((inverted - MICROBIT_LIGHT_SENSOR_MIN_VALUE)) * (b - a))/ (MICROBIT_LIGHT_SENSOR_MAX_VALUE - MICROBIT_LIGHT_SENSOR_MIN_VALUE)); |
MrBedfordVan | 0:b9164b348919 | 140 | |
MrBedfordVan | 0:b9164b348919 | 141 | return normalised; |
MrBedfordVan | 0:b9164b348919 | 142 | } |
MrBedfordVan | 0:b9164b348919 | 143 | |
MrBedfordVan | 0:b9164b348919 | 144 | /** |
MrBedfordVan | 0:b9164b348919 | 145 | * The method that is invoked by sending MICROBIT_DISPLAY_EVT_LIGHT_SENSE |
MrBedfordVan | 0:b9164b348919 | 146 | * using the id MICROBIT_ID_DISPLAY. |
MrBedfordVan | 0:b9164b348919 | 147 | * |
MrBedfordVan | 0:b9164b348919 | 148 | * @note this can be manually driven by calling this member function, with |
MrBedfordVan | 0:b9164b348919 | 149 | * a MicroBitEvent using the CREATE_ONLY option of the MicroBitEvent |
MrBedfordVan | 0:b9164b348919 | 150 | * constructor. |
MrBedfordVan | 0:b9164b348919 | 151 | */ |
MrBedfordVan | 0:b9164b348919 | 152 | void MicroBitLightSensor::startSensing(MicroBitEvent) |
MrBedfordVan | 0:b9164b348919 | 153 | { |
MrBedfordVan | 0:b9164b348919 | 154 | for(int rowCount = 0; rowCount < matrixMap.rows; rowCount++) |
MrBedfordVan | 0:b9164b348919 | 155 | DigitalOut((PinName)(matrixMap.rowStart + rowCount)).write(0); |
MrBedfordVan | 0:b9164b348919 | 156 | |
MrBedfordVan | 0:b9164b348919 | 157 | PinName currentPin = (PinName)(matrixMap.columnStart + chan); |
MrBedfordVan | 0:b9164b348919 | 158 | |
MrBedfordVan | 0:b9164b348919 | 159 | DigitalOut(currentPin).write(1); |
MrBedfordVan | 0:b9164b348919 | 160 | |
MrBedfordVan | 0:b9164b348919 | 161 | DigitalIn(currentPin, PullNone).~DigitalIn(); |
MrBedfordVan | 0:b9164b348919 | 162 | |
MrBedfordVan | 0:b9164b348919 | 163 | if(this->sensePin != NULL) |
MrBedfordVan | 0:b9164b348919 | 164 | delete this->sensePin; |
MrBedfordVan | 0:b9164b348919 | 165 | |
MrBedfordVan | 0:b9164b348919 | 166 | this->sensePin = new AnalogIn(currentPin); |
MrBedfordVan | 0:b9164b348919 | 167 | |
MrBedfordVan | 0:b9164b348919 | 168 | analogTrigger.attach_us(this, &MicroBitLightSensor::analogReady, MICROBIT_LIGHT_SENSOR_AN_SET_TIME); |
MrBedfordVan | 0:b9164b348919 | 169 | } |
MrBedfordVan | 0:b9164b348919 | 170 | |
MrBedfordVan | 0:b9164b348919 | 171 | /** |
MrBedfordVan | 0:b9164b348919 | 172 | * A destructor for MicroBitLightSensor. |
MrBedfordVan | 0:b9164b348919 | 173 | * |
MrBedfordVan | 0:b9164b348919 | 174 | * The destructor removes the listener, used by MicroBitLightSensor from the default EventModel. |
MrBedfordVan | 0:b9164b348919 | 175 | */ |
MrBedfordVan | 0:b9164b348919 | 176 | MicroBitLightSensor::~MicroBitLightSensor() |
MrBedfordVan | 0:b9164b348919 | 177 | { |
MrBedfordVan | 0:b9164b348919 | 178 | if (EventModel::defaultEventBus) |
MrBedfordVan | 0:b9164b348919 | 179 | EventModel::defaultEventBus->ignore(MICROBIT_ID_DISPLAY, MICROBIT_DISPLAY_EVT_LIGHT_SENSE, this, &MicroBitLightSensor::startSensing); |
MrBedfordVan | 0:b9164b348919 | 180 | } |