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.
Dependencies: ABBlind OneButton USBDevice mbed
main.cpp@1:60fb226f2402, 2016-09-21 (annotated)
- Committer:
- def
- Date:
- Wed Sep 21 23:44:05 2016 +0000
- Revision:
- 1:60fb226f2402
- Parent:
- 0:722dcbb1475a
- Child:
- 2:cc3d60dcfbdf
Release 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
def | 0:722dcbb1475a | 1 | /********************************************************** |
def | 1:60fb226f2402 | 2 | * ABBlind |
def | 0:722dcbb1475a | 3 | ********************************************************** |
def | 0:722dcbb1475a | 4 | * |
def | 1:60fb226f2402 | 5 | * ABBlind Firmware |
def | 1:60fb226f2402 | 6 | * |
def | 1:60fb226f2402 | 7 | * How to use: |
def | 1:60fb226f2402 | 8 | * - Long press: toggles mode: |
def | 1:60fb226f2402 | 9 | * * Setup mode: red light means channel A in the |
def | 1:60fb226f2402 | 10 | * output, blue light means channel B in the |
def | 1:60fb226f2402 | 11 | * output, short press toggles channel. |
def | 1:60fb226f2402 | 12 | * * Blind mode: each press makes a random channel |
def | 1:60fb226f2402 | 13 | * to be at the output, after 3 presses it |
def | 1:60fb226f2402 | 14 | * shows the heard sequence of channels. |
def | 0:722dcbb1475a | 15 | * |
def | 0:722dcbb1475a | 16 | ********************************************************** |
def | 0:722dcbb1475a | 17 | * Author: David Estévez Fernández (DEF) |
def | 0:722dcbb1475a | 18 | * License: GPLv3 |
def | 0:722dcbb1475a | 19 | * |
def | 0:722dcbb1475a | 20 | * A UC3Music project! Check out our website: |
def | 0:722dcbb1475a | 21 | * http://uc3music.github.io/ |
def | 0:722dcbb1475a | 22 | */ |
def | 1:60fb226f2402 | 23 | |
def | 0:722dcbb1475a | 24 | #include "mbed.h" |
def | 1:60fb226f2402 | 25 | #include "OneButton.h" |
def | 0:722dcbb1475a | 26 | |
def | 1:60fb226f2402 | 27 | //#define DEBUG |
def | 1:60fb226f2402 | 28 | #ifdef DEBUG |
def | 1:60fb226f2402 | 29 | #include "USBSerial.h" |
def | 1:60fb226f2402 | 30 | USBSerial pc;//--Debug |
def | 1:60fb226f2402 | 31 | #endif |
def | 0:722dcbb1475a | 32 | |
def | 0:722dcbb1475a | 33 | //-- Peripherals definition |
def | 0:722dcbb1475a | 34 | DigitalOut LED_01[] = {P1_24, P1_17}; |
def | 0:722dcbb1475a | 35 | DigitalOut LED_02[] = {P1_27, P1_25}; |
def | 0:722dcbb1475a | 36 | DigitalOut LED_03[] = {P0_9, P0_2}; |
def | 0:722dcbb1475a | 37 | DigitalOut AUDIO_SWITCH(P0_14); |
def | 1:60fb226f2402 | 38 | OneButton PUSH_BUTTON(P0_17, 0); //-- 0 means pullup resistor |
def | 1:60fb226f2402 | 39 | AnalogIn noise(P0_11); |
def | 0:722dcbb1475a | 40 | |
def | 0:722dcbb1475a | 41 | //-- Other constant definitions |
def | 0:722dcbb1475a | 42 | static const uint8_t COLOR1 = 0; |
def | 0:722dcbb1475a | 43 | static const uint8_t COLOR2 = 1; |
def | 1:60fb226f2402 | 44 | enum State { SetupA, SetupB, Rand1, Rand2, Rand3, RandShow}; |
def | 0:722dcbb1475a | 45 | |
def | 0:722dcbb1475a | 46 | |
def | 0:722dcbb1475a | 47 | //-- Other definitions |
def | 1:60fb226f2402 | 48 | volatile State state = SetupA; |
def | 1:60fb226f2402 | 49 | volatile State next_state = state; |
def | 1:60fb226f2402 | 50 | volatile uint8_t state_changed = 1; |
def | 1:60fb226f2402 | 51 | volatile uint8_t mode_changed = 1; |
def | 1:60fb226f2402 | 52 | uint8_t output_log[] = {0,0,0}; |
def | 0:722dcbb1475a | 53 | |
def | 0:722dcbb1475a | 54 | void start_sequence(uint16_t ms = 200) |
def | 0:722dcbb1475a | 55 | { |
def | 0:722dcbb1475a | 56 | DigitalOut* leds[] = {&LED_01[0], &LED_02[0], |
def | 0:722dcbb1475a | 57 | &LED_03[0], &LED_01[1], |
def | 0:722dcbb1475a | 58 | &LED_02[1], &LED_03[1]}; |
def | 0:722dcbb1475a | 59 | |
def | 0:722dcbb1475a | 60 | for (uint8_t i = 0; i < 6; i++) |
def | 0:722dcbb1475a | 61 | { |
def | 0:722dcbb1475a | 62 | for (uint8_t j = 0; j < 6; j++) |
def | 0:722dcbb1475a | 63 | { |
def | 0:722dcbb1475a | 64 | i == j ? *leds[j] = 1 : *leds[j] = 0; |
def | 0:722dcbb1475a | 65 | } |
def | 0:722dcbb1475a | 66 | wait_ms(ms); |
def | 0:722dcbb1475a | 67 | } |
def | 0:722dcbb1475a | 68 | } |
def | 0:722dcbb1475a | 69 | |
def | 1:60fb226f2402 | 70 | uint8_t random() |
def | 1:60fb226f2402 | 71 | { |
def | 1:60fb226f2402 | 72 | #ifdef DEBUG |
def | 1:60fb226f2402 | 73 | pc.printf("%d\n", noise.read_u16()==0x0); |
def | 1:60fb226f2402 | 74 | #endif |
def | 1:60fb226f2402 | 75 | return noise.read_u16()==0x0; |
def | 1:60fb226f2402 | 76 | } |
def | 1:60fb226f2402 | 77 | |
def | 0:722dcbb1475a | 78 | void button_pressed() |
def | 0:722dcbb1475a | 79 | { |
def | 1:60fb226f2402 | 80 | if (!mode_changed) |
def | 1:60fb226f2402 | 81 | { |
def | 1:60fb226f2402 | 82 | if (state==SetupA) |
def | 1:60fb226f2402 | 83 | next_state = SetupB; |
def | 1:60fb226f2402 | 84 | else if (state==SetupB) |
def | 1:60fb226f2402 | 85 | next_state = SetupA; |
def | 1:60fb226f2402 | 86 | else if (state==Rand1) |
def | 1:60fb226f2402 | 87 | next_state = Rand2; |
def | 1:60fb226f2402 | 88 | else if (state==Rand2) |
def | 1:60fb226f2402 | 89 | next_state = Rand3; |
def | 1:60fb226f2402 | 90 | else if (state==Rand3) |
def | 1:60fb226f2402 | 91 | next_state = RandShow; |
def | 1:60fb226f2402 | 92 | else if (state==RandShow) |
def | 1:60fb226f2402 | 93 | next_state = Rand1; |
def | 1:60fb226f2402 | 94 | state_changed = 1; |
def | 1:60fb226f2402 | 95 | } |
def | 1:60fb226f2402 | 96 | } |
def | 1:60fb226f2402 | 97 | |
def | 1:60fb226f2402 | 98 | void long_press() |
def | 1:60fb226f2402 | 99 | { |
def | 1:60fb226f2402 | 100 | if (state==SetupA || state==SetupB) |
def | 1:60fb226f2402 | 101 | { |
def | 1:60fb226f2402 | 102 | //-- Go to Random mode |
def | 1:60fb226f2402 | 103 | next_state = Rand1; |
def | 1:60fb226f2402 | 104 | } |
def | 1:60fb226f2402 | 105 | else |
def | 1:60fb226f2402 | 106 | { |
def | 1:60fb226f2402 | 107 | //-- Go to Setup mode |
def | 1:60fb226f2402 | 108 | next_state = SetupA; |
def | 1:60fb226f2402 | 109 | } |
def | 1:60fb226f2402 | 110 | state_changed = 1; |
def | 1:60fb226f2402 | 111 | mode_changed = 1; |
def | 0:722dcbb1475a | 112 | } |
def | 0:722dcbb1475a | 113 | |
def | 0:722dcbb1475a | 114 | |
def | 0:722dcbb1475a | 115 | int main() { |
def | 0:722dcbb1475a | 116 | |
def | 0:722dcbb1475a | 117 | //-- Setup button |
def | 1:60fb226f2402 | 118 | PUSH_BUTTON.attachClick(&button_pressed); |
def | 1:60fb226f2402 | 119 | PUSH_BUTTON.attachDuringLongPress(&long_press); |
def | 0:722dcbb1475a | 120 | |
def | 0:722dcbb1475a | 121 | //- Setup audio switch |
def | 0:722dcbb1475a | 122 | AUDIO_SWITCH = 0; |
def | 0:722dcbb1475a | 123 | |
def | 0:722dcbb1475a | 124 | start_sequence(); |
def | 0:722dcbb1475a | 125 | start_sequence(); |
def | 0:722dcbb1475a | 126 | |
def | 0:722dcbb1475a | 127 | while(1) { |
def | 1:60fb226f2402 | 128 | if (state_changed) |
def | 1:60fb226f2402 | 129 | { |
def | 1:60fb226f2402 | 130 | state = next_state; |
def | 1:60fb226f2402 | 131 | |
def | 1:60fb226f2402 | 132 | if (state == SetupA) |
def | 1:60fb226f2402 | 133 | { |
def | 1:60fb226f2402 | 134 | LED_01[COLOR1] = 1; |
def | 1:60fb226f2402 | 135 | LED_02[COLOR1] = 1; |
def | 1:60fb226f2402 | 136 | LED_03[COLOR1] = 1; |
def | 1:60fb226f2402 | 137 | LED_01[COLOR2] = 0; |
def | 1:60fb226f2402 | 138 | LED_02[COLOR2] = 0; |
def | 1:60fb226f2402 | 139 | LED_03[COLOR2] = 0; |
def | 1:60fb226f2402 | 140 | AUDIO_SWITCH = 0; |
def | 1:60fb226f2402 | 141 | } |
def | 1:60fb226f2402 | 142 | else if (state == SetupB) |
def | 1:60fb226f2402 | 143 | { |
def | 1:60fb226f2402 | 144 | LED_01[COLOR1] = 0; |
def | 1:60fb226f2402 | 145 | LED_02[COLOR1] = 0; |
def | 1:60fb226f2402 | 146 | LED_03[COLOR1] = 0; |
def | 1:60fb226f2402 | 147 | LED_01[COLOR2] = 1; |
def | 1:60fb226f2402 | 148 | LED_02[COLOR2] = 1; |
def | 1:60fb226f2402 | 149 | LED_03[COLOR2] = 1; |
def | 1:60fb226f2402 | 150 | AUDIO_SWITCH = 1; |
def | 1:60fb226f2402 | 151 | } |
def | 1:60fb226f2402 | 152 | else if (state == Rand1) |
def | 1:60fb226f2402 | 153 | { |
def | 1:60fb226f2402 | 154 | LED_01[COLOR1] = 1; |
def | 1:60fb226f2402 | 155 | LED_02[COLOR1] = 0; |
def | 1:60fb226f2402 | 156 | LED_03[COLOR1] = 0; |
def | 1:60fb226f2402 | 157 | LED_01[COLOR2] = 1; |
def | 1:60fb226f2402 | 158 | LED_02[COLOR2] = 0; |
def | 1:60fb226f2402 | 159 | LED_03[COLOR2] = 0; |
def | 1:60fb226f2402 | 160 | |
def | 1:60fb226f2402 | 161 | output_log[0] = random(); |
def | 1:60fb226f2402 | 162 | AUDIO_SWITCH = output_log[0]; |
def | 1:60fb226f2402 | 163 | } |
def | 1:60fb226f2402 | 164 | else if (state == Rand2) |
def | 1:60fb226f2402 | 165 | { |
def | 1:60fb226f2402 | 166 | LED_01[COLOR1] = 1; |
def | 1:60fb226f2402 | 167 | LED_02[COLOR1] = 1; |
def | 1:60fb226f2402 | 168 | LED_03[COLOR1] = 0; |
def | 1:60fb226f2402 | 169 | LED_01[COLOR2] = 1; |
def | 1:60fb226f2402 | 170 | LED_02[COLOR2] = 1; |
def | 1:60fb226f2402 | 171 | LED_03[COLOR2] = 0; |
def | 1:60fb226f2402 | 172 | |
def | 1:60fb226f2402 | 173 | output_log[1] = random(); |
def | 1:60fb226f2402 | 174 | AUDIO_SWITCH = output_log[1]; |
def | 1:60fb226f2402 | 175 | } |
def | 1:60fb226f2402 | 176 | else if (state == Rand3) |
def | 1:60fb226f2402 | 177 | { |
def | 1:60fb226f2402 | 178 | LED_01[COLOR1] = 1; |
def | 1:60fb226f2402 | 179 | LED_02[COLOR1] = 1; |
def | 1:60fb226f2402 | 180 | LED_03[COLOR1] = 1; |
def | 1:60fb226f2402 | 181 | LED_01[COLOR2] = 1; |
def | 1:60fb226f2402 | 182 | LED_02[COLOR2] = 1; |
def | 1:60fb226f2402 | 183 | LED_03[COLOR2] = 1; |
def | 1:60fb226f2402 | 184 | |
def | 1:60fb226f2402 | 185 | //-- Ensure each channel is played at least once |
def | 1:60fb226f2402 | 186 | if (output_log[0]==output_log[1]) |
def | 1:60fb226f2402 | 187 | ouput_log[2]==!output_log[0]; |
def | 1:60fb226f2402 | 188 | else |
def | 1:60fb226f2402 | 189 | output_log[2] = random(); |
def | 1:60fb226f2402 | 190 | AUDIO_SWITCH = output_log[2]; |
def | 1:60fb226f2402 | 191 | } |
def | 1:60fb226f2402 | 192 | else if (state == RandShow) |
def | 1:60fb226f2402 | 193 | { |
def | 1:60fb226f2402 | 194 | if (output_log[0]) |
def | 1:60fb226f2402 | 195 | { |
def | 1:60fb226f2402 | 196 | LED_01[COLOR1] = 1; |
def | 1:60fb226f2402 | 197 | LED_01[COLOR2] = 0; |
def | 1:60fb226f2402 | 198 | } |
def | 1:60fb226f2402 | 199 | else |
def | 1:60fb226f2402 | 200 | { |
def | 1:60fb226f2402 | 201 | LED_01[COLOR1] = 0; |
def | 1:60fb226f2402 | 202 | LED_01[COLOR2] = 1; |
def | 1:60fb226f2402 | 203 | } |
def | 1:60fb226f2402 | 204 | |
def | 1:60fb226f2402 | 205 | if (output_log[1]) |
def | 1:60fb226f2402 | 206 | { |
def | 1:60fb226f2402 | 207 | LED_02[COLOR1] = 1; |
def | 1:60fb226f2402 | 208 | LED_02[COLOR2] = 0; |
def | 1:60fb226f2402 | 209 | } |
def | 1:60fb226f2402 | 210 | else |
def | 1:60fb226f2402 | 211 | { |
def | 1:60fb226f2402 | 212 | LED_02[COLOR1] = 0; |
def | 1:60fb226f2402 | 213 | LED_02[COLOR2] = 1; |
def | 1:60fb226f2402 | 214 | } |
def | 1:60fb226f2402 | 215 | |
def | 1:60fb226f2402 | 216 | if (output_log[2]) |
def | 1:60fb226f2402 | 217 | { |
def | 1:60fb226f2402 | 218 | LED_03[COLOR1] = 1; |
def | 1:60fb226f2402 | 219 | LED_03[COLOR2] = 0; |
def | 1:60fb226f2402 | 220 | } |
def | 1:60fb226f2402 | 221 | else |
def | 1:60fb226f2402 | 222 | { |
def | 1:60fb226f2402 | 223 | LED_03[COLOR1] = 0; |
def | 1:60fb226f2402 | 224 | LED_03[COLOR2] = 1; |
def | 1:60fb226f2402 | 225 | } |
def | 1:60fb226f2402 | 226 | |
def | 1:60fb226f2402 | 227 | AUDIO_SWITCH = 0; |
def | 1:60fb226f2402 | 228 | } |
def | 1:60fb226f2402 | 229 | state_changed = 0; |
def | 1:60fb226f2402 | 230 | mode_changed = 0; |
def | 1:60fb226f2402 | 231 | } |
def | 0:722dcbb1475a | 232 | |
def | 1:60fb226f2402 | 233 | PUSH_BUTTON.tick(); |
def | 0:722dcbb1475a | 234 | } |
def | 0:722dcbb1475a | 235 | } |