Core51822 module helloworld. BLE disable version.
Dependencies: microbit_ble_disable
Fork of microbit-samples by
Core51822モジュールでmicro:bitのDAL(Device Abstraction Layer)を使います。
注意
BLE(Bluetooth Low Energy)は無効にしています。
source/LogicGates.cpp@2:5b2cd31ac474, 2016-04-08 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Fri Apr 08 16:59:43 2016 +0100
- Revision:
- 2:5b2cd31ac474
Synchronized with git rev 8f9a0bcc
Author: Joe Finney
microbit-samples: Added a suite of basic samples
Also introduced MicroBitSamples.h as a simple, cross IDE mechanism to choose
which sample to build.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LancasterUniversity | 2:5b2cd31ac474 | 1 | /* |
LancasterUniversity | 2:5b2cd31ac474 | 2 | The MIT License (MIT) |
LancasterUniversity | 2:5b2cd31ac474 | 3 | |
LancasterUniversity | 2:5b2cd31ac474 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
LancasterUniversity | 2:5b2cd31ac474 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
LancasterUniversity | 2:5b2cd31ac474 | 6 | |
LancasterUniversity | 2:5b2cd31ac474 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
LancasterUniversity | 2:5b2cd31ac474 | 8 | copy of this software and associated documentation files (the "Software"), |
LancasterUniversity | 2:5b2cd31ac474 | 9 | to deal in the Software without restriction, including without limitation |
LancasterUniversity | 2:5b2cd31ac474 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
LancasterUniversity | 2:5b2cd31ac474 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
LancasterUniversity | 2:5b2cd31ac474 | 12 | Software is furnished to do so, subject to the following conditions: |
LancasterUniversity | 2:5b2cd31ac474 | 13 | |
LancasterUniversity | 2:5b2cd31ac474 | 14 | The above copyright notice and this permission notice shall be included in |
LancasterUniversity | 2:5b2cd31ac474 | 15 | all copies or substantial portions of the Software. |
LancasterUniversity | 2:5b2cd31ac474 | 16 | |
LancasterUniversity | 2:5b2cd31ac474 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
LancasterUniversity | 2:5b2cd31ac474 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
LancasterUniversity | 2:5b2cd31ac474 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
LancasterUniversity | 2:5b2cd31ac474 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
LancasterUniversity | 2:5b2cd31ac474 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
LancasterUniversity | 2:5b2cd31ac474 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
LancasterUniversity | 2:5b2cd31ac474 | 23 | DEALINGS IN THE SOFTWARE. |
LancasterUniversity | 2:5b2cd31ac474 | 24 | */ |
LancasterUniversity | 2:5b2cd31ac474 | 25 | |
LancasterUniversity | 2:5b2cd31ac474 | 26 | #include "MicroBit.h" |
LancasterUniversity | 2:5b2cd31ac474 | 27 | #include "MicroBitSamples.h" |
LancasterUniversity | 2:5b2cd31ac474 | 28 | |
LancasterUniversity | 2:5b2cd31ac474 | 29 | #ifdef MICROBIT_SAMPLE_LOGIC_GATES |
LancasterUniversity | 2:5b2cd31ac474 | 30 | |
LancasterUniversity | 2:5b2cd31ac474 | 31 | #define LOGIC_MODE_NOT 1 |
LancasterUniversity | 2:5b2cd31ac474 | 32 | #define LOGIC_MODE_AND 2 |
LancasterUniversity | 2:5b2cd31ac474 | 33 | #define LOGIC_MODE_OR 3 |
LancasterUniversity | 2:5b2cd31ac474 | 34 | #define LOGIC_MODE_OUTPUT 4 |
LancasterUniversity | 2:5b2cd31ac474 | 35 | |
LancasterUniversity | 2:5b2cd31ac474 | 36 | #define LOGIC_MODE_MIN 1 |
LancasterUniversity | 2:5b2cd31ac474 | 37 | #define LOGIC_MODE_MAX 4 |
LancasterUniversity | 2:5b2cd31ac474 | 38 | |
LancasterUniversity | 2:5b2cd31ac474 | 39 | #define TOOL_SELECT_DELAY 1000 |
LancasterUniversity | 2:5b2cd31ac474 | 40 | |
LancasterUniversity | 2:5b2cd31ac474 | 41 | MicroBit uBit; |
LancasterUniversity | 2:5b2cd31ac474 | 42 | |
LancasterUniversity | 2:5b2cd31ac474 | 43 | MicroBitImage NOT("\ |
LancasterUniversity | 2:5b2cd31ac474 | 44 | 0 0 1 0 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 45 | 0 0 1 1 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 46 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 47 | 0 0 1 1 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 48 | 0 0 1 0 0\n"); |
LancasterUniversity | 2:5b2cd31ac474 | 49 | |
LancasterUniversity | 2:5b2cd31ac474 | 50 | MicroBitImage AND("\ |
LancasterUniversity | 2:5b2cd31ac474 | 51 | 0 0 1 1 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 52 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 53 | 0 0 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 54 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 55 | 0 0 1 1 0\n"); |
LancasterUniversity | 2:5b2cd31ac474 | 56 | |
LancasterUniversity | 2:5b2cd31ac474 | 57 | MicroBitImage OR("\ |
LancasterUniversity | 2:5b2cd31ac474 | 58 | 0 0 0 1 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 59 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 60 | 0 0 0 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 61 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 62 | 0 0 0 1 0\n"); |
LancasterUniversity | 2:5b2cd31ac474 | 63 | |
LancasterUniversity | 2:5b2cd31ac474 | 64 | MicroBitImage OUTPUT_ON("\ |
LancasterUniversity | 2:5b2cd31ac474 | 65 | 0 1 1 1 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 66 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 67 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 68 | 1 1 1 1 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 69 | 0 1 1 1 0\n"); |
LancasterUniversity | 2:5b2cd31ac474 | 70 | |
LancasterUniversity | 2:5b2cd31ac474 | 71 | MicroBitImage OUTPUT_OFF("\ |
LancasterUniversity | 2:5b2cd31ac474 | 72 | 0 1 1 1 0\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 73 | 1 0 0 0 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 74 | 1 0 0 0 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 75 | 1 0 0 0 1\n\ |
LancasterUniversity | 2:5b2cd31ac474 | 76 | 0 1 1 1 0\n"); |
LancasterUniversity | 2:5b2cd31ac474 | 77 | |
LancasterUniversity | 2:5b2cd31ac474 | 78 | int mode = LOGIC_MODE_NOT; |
LancasterUniversity | 2:5b2cd31ac474 | 79 | |
LancasterUniversity | 2:5b2cd31ac474 | 80 | void onShake(MicroBitEvent) |
LancasterUniversity | 2:5b2cd31ac474 | 81 | { |
LancasterUniversity | 2:5b2cd31ac474 | 82 | // The micro:bit has been shaken, so move on to the next logic gate. |
LancasterUniversity | 2:5b2cd31ac474 | 83 | mode++; |
LancasterUniversity | 2:5b2cd31ac474 | 84 | |
LancasterUniversity | 2:5b2cd31ac474 | 85 | // Wrap back to the start if necessary. |
LancasterUniversity | 2:5b2cd31ac474 | 86 | if (mode > LOGIC_MODE_MAX) |
LancasterUniversity | 2:5b2cd31ac474 | 87 | mode = LOGIC_MODE_MIN; |
LancasterUniversity | 2:5b2cd31ac474 | 88 | |
LancasterUniversity | 2:5b2cd31ac474 | 89 | // Update the display to |
LancasterUniversity | 2:5b2cd31ac474 | 90 | switch (mode) |
LancasterUniversity | 2:5b2cd31ac474 | 91 | { |
LancasterUniversity | 2:5b2cd31ac474 | 92 | case LOGIC_MODE_NOT: |
LancasterUniversity | 2:5b2cd31ac474 | 93 | uBit.display.print(NOT); |
LancasterUniversity | 2:5b2cd31ac474 | 94 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 95 | |
LancasterUniversity | 2:5b2cd31ac474 | 96 | case LOGIC_MODE_AND: |
LancasterUniversity | 2:5b2cd31ac474 | 97 | uBit.display.print(AND); |
LancasterUniversity | 2:5b2cd31ac474 | 98 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 99 | |
LancasterUniversity | 2:5b2cd31ac474 | 100 | case LOGIC_MODE_OR: |
LancasterUniversity | 2:5b2cd31ac474 | 101 | uBit.display.print(OR); |
LancasterUniversity | 2:5b2cd31ac474 | 102 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 103 | |
LancasterUniversity | 2:5b2cd31ac474 | 104 | case LOGIC_MODE_OUTPUT: |
LancasterUniversity | 2:5b2cd31ac474 | 105 | uBit.display.print(OUTPUT_OFF); |
LancasterUniversity | 2:5b2cd31ac474 | 106 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 107 | } |
LancasterUniversity | 2:5b2cd31ac474 | 108 | } |
LancasterUniversity | 2:5b2cd31ac474 | 109 | |
LancasterUniversity | 2:5b2cd31ac474 | 110 | int main() |
LancasterUniversity | 2:5b2cd31ac474 | 111 | { |
LancasterUniversity | 2:5b2cd31ac474 | 112 | // Initialise the micro:bit runtime. |
LancasterUniversity | 2:5b2cd31ac474 | 113 | uBit.init(); |
LancasterUniversity | 2:5b2cd31ac474 | 114 | |
LancasterUniversity | 2:5b2cd31ac474 | 115 | // Register to receive events when the micro:bit is shaken. |
LancasterUniversity | 2:5b2cd31ac474 | 116 | uBit.messageBus.listen(MICROBIT_ID_GESTURE, MICROBIT_ACCELEROMETER_EVT_SHAKE, onShake); |
LancasterUniversity | 2:5b2cd31ac474 | 117 | |
LancasterUniversity | 2:5b2cd31ac474 | 118 | // |
LancasterUniversity | 2:5b2cd31ac474 | 119 | // Create a simple logic gate simulator, using the P0, P1 and P2 pins. |
LancasterUniversity | 2:5b2cd31ac474 | 120 | // The micro:bit can then be configured as an NOT / AND / OR gate, by shaking the device. |
LancasterUniversity | 2:5b2cd31ac474 | 121 | // |
LancasterUniversity | 2:5b2cd31ac474 | 122 | int output = 0; |
LancasterUniversity | 2:5b2cd31ac474 | 123 | |
LancasterUniversity | 2:5b2cd31ac474 | 124 | // Our icons are drawn left to right, so rotate the display so the outputs point at the pins on the edge connector. :-) |
LancasterUniversity | 2:5b2cd31ac474 | 125 | uBit.display.rotateTo(MICROBIT_DISPLAY_ROTATION_270); |
LancasterUniversity | 2:5b2cd31ac474 | 126 | |
LancasterUniversity | 2:5b2cd31ac474 | 127 | while (1) |
LancasterUniversity | 2:5b2cd31ac474 | 128 | { |
LancasterUniversity | 2:5b2cd31ac474 | 129 | // Check inputs and update outputs accordingly. |
LancasterUniversity | 2:5b2cd31ac474 | 130 | switch (mode) |
LancasterUniversity | 2:5b2cd31ac474 | 131 | { |
LancasterUniversity | 2:5b2cd31ac474 | 132 | int o1; |
LancasterUniversity | 2:5b2cd31ac474 | 133 | int o2; |
LancasterUniversity | 2:5b2cd31ac474 | 134 | |
LancasterUniversity | 2:5b2cd31ac474 | 135 | case LOGIC_MODE_NOT: |
LancasterUniversity | 2:5b2cd31ac474 | 136 | output = uBit.buttonA.isPressed() ? 0 : !uBit.io.P0.getDigitalValue(); |
LancasterUniversity | 2:5b2cd31ac474 | 137 | uBit.display.print(NOT); |
LancasterUniversity | 2:5b2cd31ac474 | 138 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 139 | |
LancasterUniversity | 2:5b2cd31ac474 | 140 | case LOGIC_MODE_AND: |
LancasterUniversity | 2:5b2cd31ac474 | 141 | o1 = uBit.buttonA.isPressed() || uBit.io.P0.getDigitalValue(); |
LancasterUniversity | 2:5b2cd31ac474 | 142 | o2 = uBit.buttonB.isPressed() || uBit.io.P1.getDigitalValue(); |
LancasterUniversity | 2:5b2cd31ac474 | 143 | output = o1 && o2; |
LancasterUniversity | 2:5b2cd31ac474 | 144 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 145 | |
LancasterUniversity | 2:5b2cd31ac474 | 146 | case LOGIC_MODE_OR: |
LancasterUniversity | 2:5b2cd31ac474 | 147 | output = uBit.buttonA.isPressed() || uBit.io.P0.getDigitalValue() || uBit.buttonB.isPressed() || uBit.io.P1.getDigitalValue(); |
LancasterUniversity | 2:5b2cd31ac474 | 148 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 149 | |
LancasterUniversity | 2:5b2cd31ac474 | 150 | case LOGIC_MODE_OUTPUT: |
LancasterUniversity | 2:5b2cd31ac474 | 151 | if (uBit.io.P0.getDigitalValue()) |
LancasterUniversity | 2:5b2cd31ac474 | 152 | uBit.display.print(OUTPUT_ON); |
LancasterUniversity | 2:5b2cd31ac474 | 153 | else |
LancasterUniversity | 2:5b2cd31ac474 | 154 | uBit.display.print(OUTPUT_OFF); |
LancasterUniversity | 2:5b2cd31ac474 | 155 | |
LancasterUniversity | 2:5b2cd31ac474 | 156 | output = 0; |
LancasterUniversity | 2:5b2cd31ac474 | 157 | break; |
LancasterUniversity | 2:5b2cd31ac474 | 158 | } |
LancasterUniversity | 2:5b2cd31ac474 | 159 | |
LancasterUniversity | 2:5b2cd31ac474 | 160 | // Update output pin value |
LancasterUniversity | 2:5b2cd31ac474 | 161 | uBit.io.P2.setDigitalValue(output); |
LancasterUniversity | 2:5b2cd31ac474 | 162 | |
LancasterUniversity | 2:5b2cd31ac474 | 163 | // Perform a power efficient sleep for a little while. No need to run too quickly! |
LancasterUniversity | 2:5b2cd31ac474 | 164 | uBit.sleep(1000); |
LancasterUniversity | 2:5b2cd31ac474 | 165 | } |
LancasterUniversity | 2:5b2cd31ac474 | 166 | } |
LancasterUniversity | 2:5b2cd31ac474 | 167 | |
LancasterUniversity | 2:5b2cd31ac474 | 168 | #endif |