Starting point for the student buggy project
Fork of microbit-hello-world by
main.cpp@3:c3a42966b47c, 2017-07-31 (annotated)
- Committer:
- OyaideA
- Date:
- Mon Jul 31 22:24:03 2017 +0000
- Revision:
- 3:c3a42966b47c
- Parent:
- 2:47b7a55b0805
- Child:
- 4:2d939ef2b09c
Starting point for student buggy project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LancasterUniversity | 0:0041f35b0c4c | 1 | /* |
LancasterUniversity | 0:0041f35b0c4c | 2 | The MIT License (MIT) |
LancasterUniversity | 0:0041f35b0c4c | 3 | |
LancasterUniversity | 0:0041f35b0c4c | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
LancasterUniversity | 0:0041f35b0c4c | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
LancasterUniversity | 0:0041f35b0c4c | 6 | |
LancasterUniversity | 0:0041f35b0c4c | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
LancasterUniversity | 0:0041f35b0c4c | 8 | copy of this software and associated documentation files (the "Software"), |
LancasterUniversity | 0:0041f35b0c4c | 9 | to deal in the Software without restriction, including without limitation |
LancasterUniversity | 0:0041f35b0c4c | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
LancasterUniversity | 0:0041f35b0c4c | 11 | and/or sell copies of the Software, and to permit persons to whom the |
LancasterUniversity | 0:0041f35b0c4c | 12 | Software is furnished to do so, subject to the following conditions: |
LancasterUniversity | 0:0041f35b0c4c | 13 | |
LancasterUniversity | 0:0041f35b0c4c | 14 | The above copyright notice and this permission notice shall be included in |
LancasterUniversity | 0:0041f35b0c4c | 15 | all copies or substantial portions of the Software. |
LancasterUniversity | 0:0041f35b0c4c | 16 | |
LancasterUniversity | 0:0041f35b0c4c | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
LancasterUniversity | 0:0041f35b0c4c | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
LancasterUniversity | 0:0041f35b0c4c | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
LancasterUniversity | 0:0041f35b0c4c | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
LancasterUniversity | 0:0041f35b0c4c | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
LancasterUniversity | 0:0041f35b0c4c | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
LancasterUniversity | 0:0041f35b0c4c | 23 | DEALINGS IN THE SOFTWARE. |
LancasterUniversity | 0:0041f35b0c4c | 24 | */ |
LancasterUniversity | 0:0041f35b0c4c | 25 | |
OyaideA | 2:47b7a55b0805 | 26 | #include "Buggy.h" |
LancasterUniversity | 0:0041f35b0c4c | 27 | |
OyaideA | 1:8c05eb53f714 | 28 | |
OyaideA | 1:8c05eb53f714 | 29 | void onButtonA(MicroBitEvent); |
OyaideA | 1:8c05eb53f714 | 30 | int main(); |
OyaideA | 2:47b7a55b0805 | 31 | void SelfTest(); |
OyaideA | 2:47b7a55b0805 | 32 | void DisplaySonarTiming(); |
LancasterUniversity | 0:0041f35b0c4c | 33 | |
OyaideA | 1:8c05eb53f714 | 34 | MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A); |
OyaideA | 1:8c05eb53f714 | 35 | MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B); |
OyaideA | 1:8c05eb53f714 | 36 | |
OyaideA | 1:8c05eb53f714 | 37 | |
OyaideA | 1:8c05eb53f714 | 38 | void onButtonA(MicroBitEvent evt) |
OyaideA | 1:8c05eb53f714 | 39 | { |
OyaideA | 1:8c05eb53f714 | 40 | if(evt.value == MICROBIT_BUTTON_EVT_CLICK) |
OyaideA | 1:8c05eb53f714 | 41 | { |
OyaideA | 2:47b7a55b0805 | 42 | |
OyaideA | 1:8c05eb53f714 | 43 | } |
OyaideA | 1:8c05eb53f714 | 44 | } |
OyaideA | 1:8c05eb53f714 | 45 | |
OyaideA | 1:8c05eb53f714 | 46 | |
OyaideA | 1:8c05eb53f714 | 47 | |
OyaideA | 1:8c05eb53f714 | 48 | |
LancasterUniversity | 0:0041f35b0c4c | 49 | int main() |
OyaideA | 2:47b7a55b0805 | 50 | { |
OyaideA | 1:8c05eb53f714 | 51 | //Initialise the micro:bit runtime. |
OyaideA | 2:47b7a55b0805 | 52 | InitialiseBuggy(); |
OyaideA | 1:8c05eb53f714 | 53 | |
OyaideA | 2:47b7a55b0805 | 54 | //Run self test forever |
OyaideA | 2:47b7a55b0805 | 55 | //SelfTest(); |
OyaideA | 2:47b7a55b0805 | 56 | //Print the sonar time |
OyaideA | 3:c3a42966b47c | 57 | //DisplaySonarTiming(); |
OyaideA | 3:c3a42966b47c | 58 | |
OyaideA | 2:47b7a55b0805 | 59 | |
LancasterUniversity | 0:0041f35b0c4c | 60 | |
LancasterUniversity | 0:0041f35b0c4c | 61 | // If main exits, there may still be other fibers running or registered event handlers etc. |
LancasterUniversity | 0:0041f35b0c4c | 62 | // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then |
LancasterUniversity | 0:0041f35b0c4c | 63 | // sit in the idle task forever, in a power efficient sleep. |
LancasterUniversity | 0:0041f35b0c4c | 64 | release_fiber(); |
LancasterUniversity | 0:0041f35b0c4c | 65 | } |
LancasterUniversity | 0:0041f35b0c4c | 66 | |
OyaideA | 2:47b7a55b0805 | 67 | void SelfTest() |
OyaideA | 2:47b7a55b0805 | 68 | { |
OyaideA | 2:47b7a55b0805 | 69 | int Counter = 3; |
OyaideA | 2:47b7a55b0805 | 70 | unsigned int LastSystemTime = 0; |
OyaideA | 2:47b7a55b0805 | 71 | |
OyaideA | 2:47b7a55b0805 | 72 | //Display start message using the LEDs |
OyaideA | 2:47b7a55b0805 | 73 | //uBit.display.scroll("Hello World"); |
OyaideA | 2:47b7a55b0805 | 74 | //Instead, display 3, 2, 1, 0 |
OyaideA | 2:47b7a55b0805 | 75 | uBit.display.printAsync(Counter); |
OyaideA | 2:47b7a55b0805 | 76 | LastSystemTime = uBit.systemTime(); |
OyaideA | 2:47b7a55b0805 | 77 | while (Counter > 0) |
OyaideA | 2:47b7a55b0805 | 78 | { |
OyaideA | 2:47b7a55b0805 | 79 | if( (uBit.systemTime()-LastSystemTime) > 1000) |
OyaideA | 2:47b7a55b0805 | 80 | { |
OyaideA | 2:47b7a55b0805 | 81 | //uBit.serial.printf("Counter=%d, ", Counter); |
OyaideA | 2:47b7a55b0805 | 82 | LastSystemTime = uBit.systemTime(); |
OyaideA | 2:47b7a55b0805 | 83 | Counter = Counter - 1; |
OyaideA | 2:47b7a55b0805 | 84 | uBit.display.printAsync(Counter); |
OyaideA | 2:47b7a55b0805 | 85 | } |
OyaideA | 2:47b7a55b0805 | 86 | } |
OyaideA | 2:47b7a55b0805 | 87 | |
OyaideA | 2:47b7a55b0805 | 88 | while(1) |
OyaideA | 2:47b7a55b0805 | 89 | { |
OyaideA | 2:47b7a55b0805 | 90 | //Run the sonar test for 10 secs |
OyaideA | 2:47b7a55b0805 | 91 | LastSystemTime = uBit.systemTime(); |
OyaideA | 2:47b7a55b0805 | 92 | while( (uBit.systemTime()-LastSystemTime) < 10000) |
OyaideA | 2:47b7a55b0805 | 93 | { |
OyaideA | 2:47b7a55b0805 | 94 | TestSonar(); |
OyaideA | 2:47b7a55b0805 | 95 | } |
OyaideA | 2:47b7a55b0805 | 96 | //Display 0 |
OyaideA | 2:47b7a55b0805 | 97 | uBit.display.printAsync(Counter); |
OyaideA | 2:47b7a55b0805 | 98 | //Run the motor test |
OyaideA | 2:47b7a55b0805 | 99 | RunBasicBuggyMotorTest(800, 1000, 1000); |
OyaideA | 2:47b7a55b0805 | 100 | } |
OyaideA | 2:47b7a55b0805 | 101 | } |
OyaideA | 2:47b7a55b0805 | 102 | |
OyaideA | 2:47b7a55b0805 | 103 | void DisplaySonarTiming() |
OyaideA | 2:47b7a55b0805 | 104 | { |
OyaideA | 2:47b7a55b0805 | 105 | while(1) |
OyaideA | 2:47b7a55b0805 | 106 | { |
OyaideA | 2:47b7a55b0805 | 107 | PrintSonarTiming(); |
OyaideA | 2:47b7a55b0805 | 108 | } |
OyaideA | 2:47b7a55b0805 | 109 | |
OyaideA | 2:47b7a55b0805 | 110 | } |