![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ECE 4180 Lab 1
Dependencies: MCP23S17 ShiftBrite mbed-rtos mbed
main.cpp@0:d5e1eec2689a, 2014-01-28 (annotated)
- Committer:
- aswild
- Date:
- Tue Jan 28 16:50:08 2014 +0000
- Revision:
- 0:d5e1eec2689a
testing 123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:d5e1eec2689a | 1 | /* |
aswild | 0:d5e1eec2689a | 2 | * Allen Wild |
aswild | 0:d5e1eec2689a | 3 | * Joseph Guinta |
aswild | 0:d5e1eec2689a | 4 | * |
aswild | 0:d5e1eec2689a | 5 | * ECE 4180 Lab 1 |
aswild | 0:d5e1eec2689a | 6 | */ |
aswild | 0:d5e1eec2689a | 7 | |
aswild | 0:d5e1eec2689a | 8 | #include "mbed.h" |
aswild | 0:d5e1eec2689a | 9 | #include "rtos.h" |
aswild | 0:d5e1eec2689a | 10 | #include "MCP23S17.h" |
aswild | 0:d5e1eec2689a | 11 | #include "ShiftBrite.h" |
aswild | 0:d5e1eec2689a | 12 | |
aswild | 0:d5e1eec2689a | 13 | #define PB0_PIN p20 |
aswild | 0:d5e1eec2689a | 14 | #define LED_PIN p29 |
aswild | 0:d5e1eec2689a | 15 | #define PWM_PIN p21 |
aswild | 0:d5e1eec2689a | 16 | #define EX_CS_PIN p18 |
aswild | 0:d5e1eec2689a | 17 | |
aswild | 0:d5e1eec2689a | 18 | void updateSBColor(void const *args); |
aswild | 0:d5e1eec2689a | 19 | |
aswild | 0:d5e1eec2689a | 20 | DigitalIn pb1(PB0_PIN); |
aswild | 0:d5e1eec2689a | 21 | DigitalOut led(LED_PIN); |
aswild | 0:d5e1eec2689a | 22 | PwmOut pwm(PWM_PIN); |
aswild | 0:d5e1eec2689a | 23 | |
aswild | 0:d5e1eec2689a | 24 | SPI ex_spi(p11, p12, p13); |
aswild | 0:d5e1eec2689a | 25 | MCP23S17 ex(ex_spi, EX_CS_PIN, 0x40); |
aswild | 0:d5e1eec2689a | 26 | |
aswild | 0:d5e1eec2689a | 27 | // flags set when reading/writing the SPI extender, |
aswild | 0:d5e1eec2689a | 28 | // helpful for knowing what's happening in the logic analyzer |
aswild | 0:d5e1eec2689a | 29 | DigitalOut ex_read(p24); |
aswild | 0:d5e1eec2689a | 30 | DigitalOut ex_write(p23); |
aswild | 0:d5e1eec2689a | 31 | |
aswild | 0:d5e1eec2689a | 32 | ShiftBrite sb(p5, p6, p7, p8); |
aswild | 0:d5e1eec2689a | 33 | const int sbcolors[] = {0xff0000, 0xffff00, 0x00ff00, 0x00ffff, 0x0000ff, 0xff00ff}; |
aswild | 0:d5e1eec2689a | 34 | int sbci = 0; |
aswild | 0:d5e1eec2689a | 35 | |
aswild | 0:d5e1eec2689a | 36 | // brightness of PWM output |
aswild | 0:d5e1eec2689a | 37 | float bright = 0.0f; |
aswild | 0:d5e1eec2689a | 38 | bool pb1p = false; |
aswild | 0:d5e1eec2689a | 39 | |
aswild | 0:d5e1eec2689a | 40 | int main() |
aswild | 0:d5e1eec2689a | 41 | { |
aswild | 0:d5e1eec2689a | 42 | // set extender as inputs, configure internal pullups |
aswild | 0:d5e1eec2689a | 43 | ex.direction(PORT_A, 0xFF); |
aswild | 0:d5e1eec2689a | 44 | ex.configurePullUps(PORT_A, 0x90); |
aswild | 0:d5e1eec2689a | 45 | // port B as outputs (for LED) |
aswild | 0:d5e1eec2689a | 46 | ex.direction(PORT_B, 0x00); |
aswild | 0:d5e1eec2689a | 47 | |
aswild | 0:d5e1eec2689a | 48 | ex_read = 0; |
aswild | 0:d5e1eec2689a | 49 | ex_write = 0; |
aswild | 0:d5e1eec2689a | 50 | |
aswild | 0:d5e1eec2689a | 51 | sb.writeInit(32, 0, 0); |
aswild | 0:d5e1eec2689a | 52 | sb.fadeColor(0, 0xff0000, 0.5); |
aswild | 0:d5e1eec2689a | 53 | |
aswild | 0:d5e1eec2689a | 54 | // set PWM to 100Hz |
aswild | 0:d5e1eec2689a | 55 | pwm.period_ms(10); |
aswild | 0:d5e1eec2689a | 56 | |
aswild | 0:d5e1eec2689a | 57 | // oh no, I'm multithreading this |
aswild | 0:d5e1eec2689a | 58 | // god help us all... |
aswild | 0:d5e1eec2689a | 59 | Thread sbThread(updateSBColor); |
aswild | 0:d5e1eec2689a | 60 | |
aswild | 0:d5e1eec2689a | 61 | while (true) |
aswild | 0:d5e1eec2689a | 62 | { |
aswild | 0:d5e1eec2689a | 63 | // read button and turn on LED if pressed |
aswild | 0:d5e1eec2689a | 64 | pb1p = !pb1; |
aswild | 0:d5e1eec2689a | 65 | led = pb1p; |
aswild | 0:d5e1eec2689a | 66 | if (pb1p) |
aswild | 0:d5e1eec2689a | 67 | sbThread.signal_set(0x1); |
aswild | 0:d5e1eec2689a | 68 | while (!pb1); |
aswild | 0:d5e1eec2689a | 69 | |
aswild | 0:d5e1eec2689a | 70 | // use pushbuttons on extender to control PWM brightness |
aswild | 0:d5e1eec2689a | 71 | // invert because active-low switches |
aswild | 0:d5e1eec2689a | 72 | ex_read = 1; |
aswild | 0:d5e1eec2689a | 73 | char edata = ~(ex.read(PORT_A)); |
aswild | 0:d5e1eec2689a | 74 | ex_read = 0; |
aswild | 0:d5e1eec2689a | 75 | |
aswild | 0:d5e1eec2689a | 76 | // if either button pressed, turn on ex LED |
aswild | 0:d5e1eec2689a | 77 | if (edata & 0x90) |
aswild | 0:d5e1eec2689a | 78 | { |
aswild | 0:d5e1eec2689a | 79 | ex_write = 1; // note: debugging signal only |
aswild | 0:d5e1eec2689a | 80 | ex.write(PORT_B, 1); |
aswild | 0:d5e1eec2689a | 81 | ex_write = 0; |
aswild | 0:d5e1eec2689a | 82 | } |
aswild | 0:d5e1eec2689a | 83 | else |
aswild | 0:d5e1eec2689a | 84 | { |
aswild | 0:d5e1eec2689a | 85 | ex_write = 1; |
aswild | 0:d5e1eec2689a | 86 | ex.write(PORT_B, 0); |
aswild | 0:d5e1eec2689a | 87 | ex_write = 0; |
aswild | 0:d5e1eec2689a | 88 | } |
aswild | 0:d5e1eec2689a | 89 | |
aswild | 0:d5e1eec2689a | 90 | // if button 1 and below max brightness, increment by 1% |
aswild | 0:d5e1eec2689a | 91 | if ((edata&0x80) && bright < 1.0) |
aswild | 0:d5e1eec2689a | 92 | bright += 0.01; |
aswild | 0:d5e1eec2689a | 93 | // likewise for button 2 to decrement |
aswild | 0:d5e1eec2689a | 94 | if ((edata&0x10) && bright > 0) |
aswild | 0:d5e1eec2689a | 95 | bright -= 0.01; |
aswild | 0:d5e1eec2689a | 96 | pwm = bright; |
aswild | 0:d5e1eec2689a | 97 | |
aswild | 0:d5e1eec2689a | 98 | Thread::wait(10); |
aswild | 0:d5e1eec2689a | 99 | } |
aswild | 0:d5e1eec2689a | 100 | } |
aswild | 0:d5e1eec2689a | 101 | |
aswild | 0:d5e1eec2689a | 102 | // SB color fading happens in a different thread |
aswild | 0:d5e1eec2689a | 103 | void updateSBColor(void const *args) |
aswild | 0:d5e1eec2689a | 104 | { |
aswild | 0:d5e1eec2689a | 105 | while (true) |
aswild | 0:d5e1eec2689a | 106 | { |
aswild | 0:d5e1eec2689a | 107 | //Thread::wait(100); |
aswild | 0:d5e1eec2689a | 108 | Thread::signal_wait(0x1); |
aswild | 0:d5e1eec2689a | 109 | sb.fadeColor(sbcolors[(sbci != 0) ? sbci-1 : 5], sbcolors[sbci], 0.5); |
aswild | 0:d5e1eec2689a | 110 | if (++sbci > 5) |
aswild | 0:d5e1eec2689a | 111 | sbci = 0; |
aswild | 0:d5e1eec2689a | 112 | } |
aswild | 0:d5e1eec2689a | 113 | } |