microbit sender
Revision 0:9a841af2115b, committed 2019-03-03
- Comitter:
- bvnoake
- Date:
- Sun Mar 03 09:17:06 2019 +0000
- Commit message:
- Microbit Sender;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
microbit.lib | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 9a841af2115b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Mar 03 09:17:06 2019 +0000 @@ -0,0 +1,122 @@ +//including library +#include "MicroBit.h" + + +MicroBit uBit; + +bool BP = false; + +//defining times for button pressing +uint64_t readtime, timepressed; + + +//state of when code is running and not +bool mcode = true; + +//button function for seeing whether dot or dash +void button() +{ + while(BP == true) + { + P0.setDigitalValue(1); + }P0.setDigtalValue(0); + + + //if button was pressed once + if (timepressed > 10 && timepressed < 500) + { + //shows a dot on the screen + uBit.display.scroll("."); + //sets digital value as 0 + uBit.io.P0.setDigitalValue(1); + + } + //if button is held down for a brief amount of time + else if (timepressed > 500 && timepressed < 1000) + { + //shows a dash on screen + uBit.display.scroll("-"); + //sets digital value at 1 + uBit.io.P0.setDigitalValue(1); + + } + //if button held down for a longer time + else if (timepressed > 1500) + { + //displays stop along the screen + uBit.display.scroll("STOP"); + //sets mcode as false so buttons stop running + mcode = false; + + } + +} + + +//button event from message bus for as button is being pressed +void onButton(MicroBitEvent e) +{ + //if mcode is true signals program needs to run + if (mcode == true) + { + //if button A is pressed + if (e.source == MICROBIT_ID_BUTTON_A) + { + //calculating reading on button being pressed + BP = true; + readtime = system_timer_current_time(); + } + + } +} + +//button event from message bus for when button being let go of +void offButton(MicroBitEvent e) +{ + //if mcode is true signals program needs to run + if (mcode == true) + { + //if button A is pressed + if (e.source == MICROBIT_ID_BUTTON_A) + { + //sees how long button pressed for + timepressed = system_timer_current_time() - readtime; + //calls button function + button(); + + + } + + } +} + +//main function +int main() +{ + // Initialise the micro:bit runtime. + uBit.init(); + + //if mcode is true signals program needs to run + if (mcode == true) + { + + //message buses for function onButton and offButton for when button clicked + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_DOWN, onButton); + + uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_UP, offButton); + + + + + + + + } + + // If main exits, there may still be other fibers running or + // registered event handlers etc. + // Simply release this fiber, which will mean we enter the + // scheduler. Worse case, we then + // sit in the idle task forever, in a power efficient sleep. + release_fiber(); +} \ No newline at end of file
diff -r 000000000000 -r 9a841af2115b microbit.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/microbit.lib Sun Mar 03 09:17:06 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/Lancaster-University/code/microbit/#4b89e7e3494f