Streams USB audio with sound effects applied. Sound effect selected by joystick and intensity altered by tilting the mbed. Output to the mbed-application-board phono jack.
Dependencies: C12832_lcd MMA7660 USBDevice mbed
/* Uses the mbed LPC1768 and mbed-application-board to create a USB audio device * that streams audio from a host computer to headphones or powered speakers. * A couple different sound effects can be applied to the stream in real-time, * and tilting the mbed alters intensity of the effect. * * ECHO * The joystick selects ) | * one of three effect ) STRAIGHT - o - STRAIGHT * modes. ) | * REVERB * * * * \\ || * Tilting the mbed ) ====== \\ || * determines intensity ) \\ || * of the effect. ) * 0% 50% 100% * * The LCD display shows the current effect mode, intesity and buffer level. */
user_interface.cpp@2:9429f84ea165, 2014-03-27 (annotated)
- Committer:
- bw
- Date:
- Thu Mar 27 21:43:41 2014 +0000
- Revision:
- 2:9429f84ea165
- Parent:
- 0:bbf6cf0eab95
Clean up comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bw | 0:bbf6cf0eab95 | 1 | /******************************************************************************* |
bw | 0:bbf6cf0eab95 | 2 | * Mangages LCD display, joystick and accelerometer for user interface. |
bw | 0:bbf6cf0eab95 | 3 | * Bryan Wade |
bw | 0:bbf6cf0eab95 | 4 | * 27 MAR 2014 |
bw | 0:bbf6cf0eab95 | 5 | ******************************************************************************/ |
bw | 0:bbf6cf0eab95 | 6 | #include "mbed.h" |
bw | 0:bbf6cf0eab95 | 7 | #include "C12832_lcd.h" |
bw | 0:bbf6cf0eab95 | 8 | #include "MMA7660.h" |
bw | 0:bbf6cf0eab95 | 9 | #include "user_interface.h" |
bw | 0:bbf6cf0eab95 | 10 | #include "effects.h" |
bw | 0:bbf6cf0eab95 | 11 | |
bw | 0:bbf6cf0eab95 | 12 | C12832_LCD lcd; |
bw | 0:bbf6cf0eab95 | 13 | MMA7660 accel(p28, p27, true); |
bw | 0:bbf6cf0eab95 | 14 | BusIn joystick(p15, p12, p13, p16); |
bw | 0:bbf6cf0eab95 | 15 | AnalogIn pot(p19); |
bw | 0:bbf6cf0eab95 | 16 | |
bw | 0:bbf6cf0eab95 | 17 | static const int STICK_IDLE = 0; |
bw | 0:bbf6cf0eab95 | 18 | static const int STICK_UP = 1; |
bw | 0:bbf6cf0eab95 | 19 | static const int STICK_DOWN = 2; |
bw | 0:bbf6cf0eab95 | 20 | static const int STICK_LEFT = 4; |
bw | 0:bbf6cf0eab95 | 21 | static const int STICK_RIGHT = 8; |
bw | 0:bbf6cf0eab95 | 22 | |
bw | 0:bbf6cf0eab95 | 23 | static uint8_t stickPrevious; |
bw | 0:bbf6cf0eab95 | 24 | static effect_mode_t selectedMode; |
bw | 0:bbf6cf0eab95 | 25 | static char *selectedModeName; |
bw | 0:bbf6cf0eab95 | 26 | static uint16_t effectGain; |
bw | 0:bbf6cf0eab95 | 27 | |
bw | 0:bbf6cf0eab95 | 28 | /* |
bw | 0:bbf6cf0eab95 | 29 | * Update the display with mode, gain and buffer level. |
bw | 0:bbf6cf0eab95 | 30 | */ |
bw | 0:bbf6cf0eab95 | 31 | void updateDisplay(int32_t level) |
bw | 0:bbf6cf0eab95 | 32 | { |
bw | 0:bbf6cf0eab95 | 33 | lcd.locate(0,0); |
bw | 0:bbf6cf0eab95 | 34 | lcd.printf("Effect: %s ", selectedModeName); |
bw | 0:bbf6cf0eab95 | 35 | |
bw | 0:bbf6cf0eab95 | 36 | lcd.locate(0,10); |
bw | 0:bbf6cf0eab95 | 37 | lcd.printf("Intensity: %d ", effectGain / (MAX_EFFECT_GAIN / 100)); |
bw | 0:bbf6cf0eab95 | 38 | |
bw | 0:bbf6cf0eab95 | 39 | lcd.locate(0,20); |
bw | 0:bbf6cf0eab95 | 40 | lcd.printf("Buffer: %d ", level); |
bw | 0:bbf6cf0eab95 | 41 | } |
bw | 0:bbf6cf0eab95 | 42 | |
bw | 0:bbf6cf0eab95 | 43 | /* |
bw | 0:bbf6cf0eab95 | 44 | * Get effects mode selected by joystick. |
bw | 0:bbf6cf0eab95 | 45 | */ |
bw | 0:bbf6cf0eab95 | 46 | void serviceJoystick(void) |
bw | 0:bbf6cf0eab95 | 47 | { |
bw | 0:bbf6cf0eab95 | 48 | uint8_t stick = joystick; |
bw | 0:bbf6cf0eab95 | 49 | |
bw | 0:bbf6cf0eab95 | 50 | if (stick != stickPrevious) { |
bw | 0:bbf6cf0eab95 | 51 | stickPrevious = stick; |
bw | 0:bbf6cf0eab95 | 52 | |
bw | 0:bbf6cf0eab95 | 53 | switch (stick) |
bw | 0:bbf6cf0eab95 | 54 | { |
bw | 0:bbf6cf0eab95 | 55 | case STICK_UP: |
bw | 0:bbf6cf0eab95 | 56 | selectedMode = EFFECT_ECHO; |
bw | 0:bbf6cf0eab95 | 57 | selectedModeName = "Echo"; |
bw | 0:bbf6cf0eab95 | 58 | break; |
bw | 0:bbf6cf0eab95 | 59 | |
bw | 0:bbf6cf0eab95 | 60 | case STICK_DOWN: |
bw | 0:bbf6cf0eab95 | 61 | selectedMode = EFFECT_REVERB; |
bw | 0:bbf6cf0eab95 | 62 | selectedModeName = "Reverb"; |
bw | 0:bbf6cf0eab95 | 63 | break; |
bw | 0:bbf6cf0eab95 | 64 | |
bw | 0:bbf6cf0eab95 | 65 | case STICK_LEFT: |
bw | 0:bbf6cf0eab95 | 66 | case STICK_RIGHT: |
bw | 0:bbf6cf0eab95 | 67 | selectedMode = EFFECT_STRAIGHT; |
bw | 0:bbf6cf0eab95 | 68 | selectedModeName = "Straight"; |
bw | 0:bbf6cf0eab95 | 69 | break; |
bw | 0:bbf6cf0eab95 | 70 | } |
bw | 0:bbf6cf0eab95 | 71 | } |
bw | 0:bbf6cf0eab95 | 72 | } |
bw | 0:bbf6cf0eab95 | 73 | |
bw | 0:bbf6cf0eab95 | 74 | /* |
bw | 0:bbf6cf0eab95 | 75 | * Get effects gain from accelerometer tilt. |
bw | 0:bbf6cf0eab95 | 76 | */ |
bw | 0:bbf6cf0eab95 | 77 | void serviceAccel(void) |
bw | 0:bbf6cf0eab95 | 78 | { |
bw | 0:bbf6cf0eab95 | 79 | // Tilt sensitivity selected to produce max effect gain |
bw | 0:bbf6cf0eab95 | 80 | // at about 80 deg tilt. |
bw | 0:bbf6cf0eab95 | 81 | static const int32_t TILT_SENSITIVITY = 2750; |
bw | 0:bbf6cf0eab95 | 82 | int a[3]; |
bw | 0:bbf6cf0eab95 | 83 | accel.readData(a); |
bw | 0:bbf6cf0eab95 | 84 | //Convert x-axis raw accelerometer data to the effect gain. |
bw | 0:bbf6cf0eab95 | 85 | int32_t x = a[0] * TILT_SENSITIVITY; |
bw | 0:bbf6cf0eab95 | 86 | if (x < 0) x = -x; |
bw | 0:bbf6cf0eab95 | 87 | if (x > MAX_EFFECT_GAIN) x = MAX_EFFECT_GAIN; |
bw | 0:bbf6cf0eab95 | 88 | effectGain = (uint16_t)x; |
bw | 0:bbf6cf0eab95 | 89 | } |
bw | 0:bbf6cf0eab95 | 90 | |
bw | 0:bbf6cf0eab95 | 91 | /* |
bw | 0:bbf6cf0eab95 | 92 | * Initialize the joystick and display. |
bw | 0:bbf6cf0eab95 | 93 | */ |
bw | 0:bbf6cf0eab95 | 94 | void UI_Initialize(void) |
bw | 0:bbf6cf0eab95 | 95 | { |
bw | 0:bbf6cf0eab95 | 96 | lcd.cls(); |
bw | 0:bbf6cf0eab95 | 97 | stickPrevious = STICK_IDLE; |
bw | 0:bbf6cf0eab95 | 98 | selectedMode = EFFECT_STRAIGHT; |
bw | 0:bbf6cf0eab95 | 99 | selectedModeName = "Straight"; |
bw | 0:bbf6cf0eab95 | 100 | effectGain = 0; |
bw | 0:bbf6cf0eab95 | 101 | // Set the accelerometer to sample slightly |
bw | 0:bbf6cf0eab95 | 102 | // faster than we poll the user interface. |
bw | 0:bbf6cf0eab95 | 103 | accel.setSampleRate(32); |
bw | 0:bbf6cf0eab95 | 104 | } |
bw | 0:bbf6cf0eab95 | 105 | |
bw | 0:bbf6cf0eab95 | 106 | /* |
bw | 0:bbf6cf0eab95 | 107 | * Updates display, joystick and accelerometer |
bw | 0:bbf6cf0eab95 | 108 | */ |
bw | 0:bbf6cf0eab95 | 109 | void UI_Update(int32_t bufferLevel) |
bw | 0:bbf6cf0eab95 | 110 | { |
bw | 0:bbf6cf0eab95 | 111 | serviceJoystick(); |
bw | 0:bbf6cf0eab95 | 112 | serviceAccel(); |
bw | 0:bbf6cf0eab95 | 113 | updateDisplay(bufferLevel); |
bw | 0:bbf6cf0eab95 | 114 | } |
bw | 0:bbf6cf0eab95 | 115 | |
bw | 0:bbf6cf0eab95 | 116 | |
bw | 0:bbf6cf0eab95 | 117 | effect_mode_t UI_GetEffectMode(void) { return selectedMode; } |
bw | 0:bbf6cf0eab95 | 118 | |
bw | 0:bbf6cf0eab95 | 119 | uint16_t UI_GetEffectGain(void) { return effectGain; } |
bw | 0:bbf6cf0eab95 | 120 | |
bw | 0:bbf6cf0eab95 | 121 | |
bw | 0:bbf6cf0eab95 | 122 |