Samples of how to use the micro:bit DAL with mbed. This is a hg clone of the real git repo that can be found here: https://github.com/lancaster-university/microbit-samples

Dependencies:   microbit

Fork of microbit-samples by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AccelerometerDemo.cpp Source File

AccelerometerDemo.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_ACCELEROMETER_DEMO
00030 
00031 MicroBit uBit;
00032 
00033 //
00034 // Scales the given value that is in the -1024 to 1024 range
00035 // int a value between 0 and 4.
00036 //
00037 int pixel_from_g(int value)
00038 {
00039     int x = 0;
00040 
00041     if (value > -750)
00042         x++;
00043     if (value > -250)
00044         x++;
00045     if (value > 250)
00046         x++;
00047     if (value > 750)
00048         x++;
00049 
00050     return x;
00051 }
00052 
00053 int main()
00054 {
00055     // Initialise the micro:bit runtime.
00056     uBit.init();
00057 
00058     //
00059     // Periodically read the accelerometer x and y values, and plot a 
00060     // scaled version of this ont the display. 
00061     //
00062     while(1)
00063     {
00064         int x = pixel_from_g(uBit.accelerometer.getX());
00065         int y = pixel_from_g(uBit.accelerometer.getY());
00066 
00067         uBit.display.image.clear();
00068         uBit.display.image.setPixelValue(x, y, 255);
00069         
00070         uBit.sleep(100);
00071     }
00072 }
00073 
00074 #endif