Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MicroBitDALImageRewrite by
MicroBitButton.cpp@1:3e0360107f98, 2015-04-16 (annotated)
- Committer:
- finneyj
- Date:
- Thu Apr 16 13:50:24 2015 +0000
- Revision:
- 1:3e0360107f98
- Child:
- 4:f998ee705a20
Updates to include:; ; - Asynchronous Scrolltext(); - First iteration of MessageBus implementation; - Hardware configuration for SquareBoard protoype
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
finneyj | 1:3e0360107f98 | 1 | #include "inc/MicroBit.h" |
finneyj | 1:3e0360107f98 | 2 | #include "inc/MicroBitButton.h" |
finneyj | 1:3e0360107f98 | 3 | #include "inc/MicroBitMessageBus.h" |
finneyj | 1:3e0360107f98 | 4 | /** |
finneyj | 1:3e0360107f98 | 5 | * Constructor. |
finneyj | 1:3e0360107f98 | 6 | * Create a button representation with the given ID. |
finneyj | 1:3e0360107f98 | 7 | * @param id the ID of the new LED object. |
finneyj | 1:3e0360107f98 | 8 | * @param name the physical pin on the processor that this butotn is connected to. |
finneyj | 1:3e0360107f98 | 9 | */ |
finneyj | 1:3e0360107f98 | 10 | MicroBitButton::MicroBitButton(int id, PinName name) : pin(name), irq(name) |
finneyj | 1:3e0360107f98 | 11 | { |
finneyj | 1:3e0360107f98 | 12 | this->id = id; |
finneyj | 1:3e0360107f98 | 13 | this->name = name; |
finneyj | 1:3e0360107f98 | 14 | irq.rise(this, &MicroBitButton::rising); |
finneyj | 1:3e0360107f98 | 15 | irq.fall(this, &MicroBitButton::falling); |
finneyj | 1:3e0360107f98 | 16 | } |
finneyj | 1:3e0360107f98 | 17 | |
finneyj | 1:3e0360107f98 | 18 | /** |
finneyj | 1:3e0360107f98 | 19 | * Interrupt on change handler for this button. |
finneyj | 1:3e0360107f98 | 20 | */ |
finneyj | 1:3e0360107f98 | 21 | void MicroBitButton::rising() |
finneyj | 1:3e0360107f98 | 22 | { |
finneyj | 1:3e0360107f98 | 23 | // TODO: FIX THIS. This many of the mbed libraries aren't re-entrant... |
finneyj | 1:3e0360107f98 | 24 | MicroBitEvent *evt = new MicroBitEvent(); |
finneyj | 1:3e0360107f98 | 25 | evt->source = id; |
finneyj | 1:3e0360107f98 | 26 | evt->context = NULL; |
finneyj | 1:3e0360107f98 | 27 | evt->timestamp = 0; |
finneyj | 1:3e0360107f98 | 28 | evt->value = MICROBIT_BUTTON_EVT_UP; |
finneyj | 1:3e0360107f98 | 29 | |
finneyj | 1:3e0360107f98 | 30 | MicroBit::MessageBus.send(evt); |
finneyj | 1:3e0360107f98 | 31 | } |
finneyj | 1:3e0360107f98 | 32 | |
finneyj | 1:3e0360107f98 | 33 | /** |
finneyj | 1:3e0360107f98 | 34 | * Interrupt on change handler for this button. |
finneyj | 1:3e0360107f98 | 35 | */ |
finneyj | 1:3e0360107f98 | 36 | void MicroBitButton::falling() |
finneyj | 1:3e0360107f98 | 37 | { |
finneyj | 1:3e0360107f98 | 38 | // TODO: FIX THIS. This many of the mbed libraries aren't re-entrant... |
finneyj | 1:3e0360107f98 | 39 | MicroBitEvent *evt = new MicroBitEvent(); |
finneyj | 1:3e0360107f98 | 40 | evt->source = id; |
finneyj | 1:3e0360107f98 | 41 | evt->context = NULL; |
finneyj | 1:3e0360107f98 | 42 | evt->timestamp = 0; |
finneyj | 1:3e0360107f98 | 43 | evt->value = MICROBIT_BUTTON_EVT_DOWN; |
finneyj | 1:3e0360107f98 | 44 | |
finneyj | 1:3e0360107f98 | 45 | MicroBit::MessageBus.send(evt); |
finneyj | 1:3e0360107f98 | 46 | } |
finneyj | 1:3e0360107f98 | 47 | |
finneyj | 1:3e0360107f98 | 48 | |
finneyj | 1:3e0360107f98 | 49 | /** |
finneyj | 1:3e0360107f98 | 50 | * Tests if this Button is currently pressed. |
finneyj | 1:3e0360107f98 | 51 | * @return 1 if this button is pressed, 0 otherwise. |
finneyj | 1:3e0360107f98 | 52 | */ |
finneyj | 1:3e0360107f98 | 53 | int MicroBitButton::isPressed() |
finneyj | 1:3e0360107f98 | 54 | { |
finneyj | 1:3e0360107f98 | 55 | return !pin; |
finneyj | 1:3e0360107f98 | 56 | } |
finneyj | 1:3e0360107f98 | 57 | |
finneyj | 1:3e0360107f98 | 58 |