A program for the BBC MicroBit that displays temperature and compass headings.

Dependencies:   microbit

Committer:
daw9000
Date:
Fri Jul 22 16:30:22 2016 +0000
Revision:
0:1072edc2281c
General app for temperature with offset adjustment also can display heading and acceleration (microbit examples). Adjust screen brightness

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daw9000 0:1072edc2281c 1 /*
daw9000 0:1072edc2281c 2 The MIT License (MIT)
daw9000 0:1072edc2281c 3
daw9000 0:1072edc2281c 4 Copyright (c) 2016 British Broadcasting Corporation.
daw9000 0:1072edc2281c 5 This software is provided by Lancaster University by arrangement with the BBC.
daw9000 0:1072edc2281c 6
daw9000 0:1072edc2281c 7 Permission is hereby granted, free of charge, to any person obtaining a
daw9000 0:1072edc2281c 8 copy of this software and associated documentation files (the "Software"),
daw9000 0:1072edc2281c 9 to deal in the Software without restriction, including without limitation
daw9000 0:1072edc2281c 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
daw9000 0:1072edc2281c 11 and/or sell copies of the Software, and to permit persons to whom the
daw9000 0:1072edc2281c 12 Software is furnished to do so, subject to the following conditions:
daw9000 0:1072edc2281c 13
daw9000 0:1072edc2281c 14 The above copyright notice and this permission notice shall be included in
daw9000 0:1072edc2281c 15 all copies or substantial portions of the Software.
daw9000 0:1072edc2281c 16
daw9000 0:1072edc2281c 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
daw9000 0:1072edc2281c 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
daw9000 0:1072edc2281c 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
daw9000 0:1072edc2281c 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
daw9000 0:1072edc2281c 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
daw9000 0:1072edc2281c 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
daw9000 0:1072edc2281c 23 DEALINGS IN THE SOFTWARE.
daw9000 0:1072edc2281c 24 */
daw9000 0:1072edc2281c 25
daw9000 0:1072edc2281c 26 #include "MicroBit.h"
daw9000 0:1072edc2281c 27
daw9000 0:1072edc2281c 28
daw9000 0:1072edc2281c 29 MicroBit uBit;
daw9000 0:1072edc2281c 30 int xx=0, mode=0;
daw9000 0:1072edc2281c 31 //
daw9000 0:1072edc2281c 32 // Scales the given value that is in the -1024 to 1024 range
daw9000 0:1072edc2281c 33 // int a value between 0 and 4.
daw9000 0:1072edc2281c 34 //
daw9000 0:1072edc2281c 35 int pixel_from_g(int value)
daw9000 0:1072edc2281c 36 {
daw9000 0:1072edc2281c 37 int x = 0;
daw9000 0:1072edc2281c 38
daw9000 0:1072edc2281c 39 if (value > -750)
daw9000 0:1072edc2281c 40 x++;
daw9000 0:1072edc2281c 41 if (value > -250)
daw9000 0:1072edc2281c 42 x++;
daw9000 0:1072edc2281c 43 if (value > 250)
daw9000 0:1072edc2281c 44 x++;
daw9000 0:1072edc2281c 45 if (value > 750)
daw9000 0:1072edc2281c 46 x++;
daw9000 0:1072edc2281c 47
daw9000 0:1072edc2281c 48 return x;
daw9000 0:1072edc2281c 49 }
daw9000 0:1072edc2281c 50
daw9000 0:1072edc2281c 51 void onButton(MicroBitEvent e)
daw9000 0:1072edc2281c 52 {
daw9000 0:1072edc2281c 53 if (e.source == MICROBIT_ID_BUTTON_A and e.value == MICROBIT_BUTTON_EVT_CLICK)
daw9000 0:1072edc2281c 54 {
daw9000 0:1072edc2281c 55 if (mode==0){
daw9000 0:1072edc2281c 56 xx++;}
daw9000 0:1072edc2281c 57 }
daw9000 0:1072edc2281c 58
daw9000 0:1072edc2281c 59 if (e.source == MICROBIT_ID_BUTTON_B and e.value == MICROBIT_BUTTON_EVT_CLICK)
daw9000 0:1072edc2281c 60 {
daw9000 0:1072edc2281c 61 if (mode==0){
daw9000 0:1072edc2281c 62 xx--;}
daw9000 0:1072edc2281c 63 }
daw9000 0:1072edc2281c 64
daw9000 0:1072edc2281c 65 if (e.source == MICROBIT_ID_BUTTON_A and e.value == MICROBIT_BUTTON_EVT_LONG_CLICK)
daw9000 0:1072edc2281c 66 {
daw9000 0:1072edc2281c 67
daw9000 0:1072edc2281c 68 }
daw9000 0:1072edc2281c 69
daw9000 0:1072edc2281c 70 if (e.source == MICROBIT_ID_BUTTON_B and e.value == MICROBIT_BUTTON_EVT_LONG_CLICK)
daw9000 0:1072edc2281c 71 {
daw9000 0:1072edc2281c 72 }
daw9000 0:1072edc2281c 73
daw9000 0:1072edc2281c 74 if (e.source == MICROBIT_ID_BUTTON_AB and e.value == MICROBIT_BUTTON_EVT_CLICK)
daw9000 0:1072edc2281c 75 {
daw9000 0:1072edc2281c 76 mode++;
daw9000 0:1072edc2281c 77 if (mode>2)
daw9000 0:1072edc2281c 78 {
daw9000 0:1072edc2281c 79 mode=0;
daw9000 0:1072edc2281c 80 uBit.sleep(100);
daw9000 0:1072edc2281c 81 }
daw9000 0:1072edc2281c 82 }
daw9000 0:1072edc2281c 83
daw9000 0:1072edc2281c 84 if (e.source == MICROBIT_ID_IO_P0)
daw9000 0:1072edc2281c 85 {
daw9000 0:1072edc2281c 86 if (uBit.io.P0.isTouched())
daw9000 0:1072edc2281c 87 {
daw9000 0:1072edc2281c 88 if (uBit.display.getBrightness() > 25)
daw9000 0:1072edc2281c 89 {
daw9000 0:1072edc2281c 90 uBit.display.setBrightness(uBit.display.getBrightness()-100);
daw9000 0:1072edc2281c 91 }
daw9000 0:1072edc2281c 92 else{
daw9000 0:1072edc2281c 93 uBit.display.setBrightness(5);
daw9000 0:1072edc2281c 94 }
daw9000 0:1072edc2281c 95 uBit.sleep(100);
daw9000 0:1072edc2281c 96
daw9000 0:1072edc2281c 97 }
daw9000 0:1072edc2281c 98
daw9000 0:1072edc2281c 99 }
daw9000 0:1072edc2281c 100
daw9000 0:1072edc2281c 101 if (e.source == MICROBIT_ID_IO_P1)
daw9000 0:1072edc2281c 102 {
daw9000 0:1072edc2281c 103
daw9000 0:1072edc2281c 104 if (uBit.io.P1.isTouched())
daw9000 0:1072edc2281c 105 {
daw9000 0:1072edc2281c 106 if (uBit.display.getBrightness()<225)
daw9000 0:1072edc2281c 107 {
daw9000 0:1072edc2281c 108 uBit.display.setBrightness(uBit.display.getBrightness()+100);
daw9000 0:1072edc2281c 109 }
daw9000 0:1072edc2281c 110 else
daw9000 0:1072edc2281c 111 {
daw9000 0:1072edc2281c 112 uBit.display.setBrightness(255);
daw9000 0:1072edc2281c 113 }
daw9000 0:1072edc2281c 114 uBit.sleep(100);
daw9000 0:1072edc2281c 115
daw9000 0:1072edc2281c 116 }
daw9000 0:1072edc2281c 117 }
daw9000 0:1072edc2281c 118
daw9000 0:1072edc2281c 119
daw9000 0:1072edc2281c 120 if (e.source == MICROBIT_ID_IO_P2)
daw9000 0:1072edc2281c 121 {
daw9000 0:1072edc2281c 122
daw9000 0:1072edc2281c 123 if (uBit.io.P2.isTouched())
daw9000 0:1072edc2281c 124 {
daw9000 0:1072edc2281c 125 uBit.display.scroll("DevTemp=");
daw9000 0:1072edc2281c 126
daw9000 0:1072edc2281c 127 uBit.display.scroll( uBit.thermometer.getTemperature());
daw9000 0:1072edc2281c 128 uBit.sleep(100);
daw9000 0:1072edc2281c 129 }
daw9000 0:1072edc2281c 130
daw9000 0:1072edc2281c 131 }
daw9000 0:1072edc2281c 132 }
daw9000 0:1072edc2281c 133 int main(void)
daw9000 0:1072edc2281c 134 {
daw9000 0:1072edc2281c 135 // Initialise the micro:bit runtime.
daw9000 0:1072edc2281c 136 uBit.init();
daw9000 0:1072edc2281c 137 // Register to receive events when any buttons are clicked, including the A+B virtual button (both buttons at once).
daw9000 0:1072edc2281c 138 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButton);
daw9000 0:1072edc2281c 139 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButton);
daw9000 0:1072edc2281c 140 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_EVT_ANY, onButton);
daw9000 0:1072edc2281c 141 // Also register for touch events on P0, P1 and P2.
daw9000 0:1072edc2281c 142 uBit.messageBus.listen(MICROBIT_ID_IO_P0, MICROBIT_EVT_ANY, onButton);
daw9000 0:1072edc2281c 143 uBit.messageBus.listen(MICROBIT_ID_IO_P1, MICROBIT_EVT_ANY, onButton);
daw9000 0:1072edc2281c 144 uBit.messageBus.listen(MICROBIT_ID_IO_P2, MICROBIT_EVT_ANY, onButton);
daw9000 0:1072edc2281c 145
daw9000 0:1072edc2281c 146 // Put the P0, P1 and P2 pins into touch sense mode.
daw9000 0:1072edc2281c 147
daw9000 0:1072edc2281c 148 uBit.io.P0.isTouched();
daw9000 0:1072edc2281c 149 uBit.io.P1.isTouched();
daw9000 0:1072edc2281c 150 uBit.io.P2.isTouched();
daw9000 0:1072edc2281c 151
daw9000 0:1072edc2281c 152 while (true){
daw9000 0:1072edc2281c 153 if ( (not(uBit.io.P0.isTouched())) and (not(uBit.io.P1.isTouched())) and (not (uBit.io.P2.isTouched())) )
daw9000 0:1072edc2281c 154 {
daw9000 0:1072edc2281c 155 if (mode==0)
daw9000 0:1072edc2281c 156 {
daw9000 0:1072edc2281c 157 uBit.display.scroll("Temp=");
daw9000 0:1072edc2281c 158
daw9000 0:1072edc2281c 159 uBit.display.scroll( uBit.thermometer.getTemperature() + xx);
daw9000 0:1072edc2281c 160 uBit.sleep(100);
daw9000 0:1072edc2281c 161 }
daw9000 0:1072edc2281c 162 if (mode==1)
daw9000 0:1072edc2281c 163 {
daw9000 0:1072edc2281c 164 int x = pixel_from_g(uBit.accelerometer.getX());
daw9000 0:1072edc2281c 165 int y = pixel_from_g(uBit.accelerometer.getY());
daw9000 0:1072edc2281c 166
daw9000 0:1072edc2281c 167 uBit.display.image.clear();
daw9000 0:1072edc2281c 168 uBit.display.image.setPixelValue(x, y, 255);
daw9000 0:1072edc2281c 169
daw9000 0:1072edc2281c 170 uBit.sleep(100);
daw9000 0:1072edc2281c 171 }
daw9000 0:1072edc2281c 172 if (mode==2)
daw9000 0:1072edc2281c 173 {
daw9000 0:1072edc2281c 174 uBit.display.scroll("Hdg:");
daw9000 0:1072edc2281c 175 uBit.display.scroll(uBit.compass.heading());
daw9000 0:1072edc2281c 176 uBit.sleep(100);
daw9000 0:1072edc2281c 177 }
daw9000 0:1072edc2281c 178
daw9000 0:1072edc2281c 179
daw9000 0:1072edc2281c 180 }
daw9000 0:1072edc2281c 181
daw9000 0:1072edc2281c 182 uBit.sleep(200);
daw9000 0:1072edc2281c 183
daw9000 0:1072edc2281c 184 }
daw9000 0:1072edc2281c 185
daw9000 0:1072edc2281c 186
daw9000 0:1072edc2281c 187
daw9000 0:1072edc2281c 188
daw9000 0:1072edc2281c 189 // If main exits, there may still be other fibers running or registered event handlers etc.
daw9000 0:1072edc2281c 190 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
daw9000 0:1072edc2281c 191 // sit in the idle task forever, in a power efficient sleep.
daw9000 0:1072edc2281c 192 release_fiber();
daw9000 0:1072edc2281c 193 }
daw9000 0:1072edc2281c 194
daw9000 0:1072edc2281c 195