Dependencies: MCP23S17 PinDetect mbed
Revision 0:9dc33481ce1b, committed 2017-03-01
- Comitter:
- jderemer3
- Date:
- Wed Mar 01 16:36:55 2017 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r 9dc33481ce1b LEDColor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LEDColor.h Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,25 @@ +#include "mbed.h" + +//class for 3 PWM color values for RGBLED +class LEDColor +{ +public: + LEDColor(float r, float g, float b); + float red; + float green; + float blue; +}; +LEDColor:: LEDColor(float r, float g, float b) + : red(r), green(g), blue(b) +{ +} +//Operator overload to adjust brightness with no color change +LEDColor operator * (const LEDColor& x, const float& b) +{ + return LEDColor(x.red*b,x.green*b,x.blue*b); +} +//Operator overload to add colors +LEDColor operator + (const LEDColor& x, const LEDColor& y) +{ + return LEDColor(x.red+y.red,x.green+y.green,x.blue+y.blue); +} \ No newline at end of file
diff -r 000000000000 -r 9dc33481ce1b MCP23S17.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP23S17.lib Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/romilly/code/MCP23S17/#068b1e8909bb
diff -r 000000000000 -r 9dc33481ce1b Nav_Switch.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Nav_Switch.h Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,73 @@ +#include "mbed.h" + +BusOut mbedleds(LED1,LED2,LED3,LED4); +//BusOut/In is faster than multiple DigitalOut/Ins + +class Nav_Switch +{ +public: + Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); + int read(); +//boolean functions to test each switch + bool up(); + bool down(); + bool left(); + bool right(); + bool fire(); +//automatic read on RHS + operator int (); +//index to any switch array style + bool operator[](int index) { + return _pins[index]; + }; +private: + BusIn _pins; + +}; +Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): + _pins(up, down, left, right, fire) +{ + _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise + wait(0.001); //delays just a bit for pullups to pull inputs high +} +inline bool Nav_Switch::up() +{ + return !(_pins[0]); +} +inline bool Nav_Switch::down() +{ + return !(_pins[1]); +} +inline bool Nav_Switch::left() +{ + return !(_pins[2]); +} +inline bool Nav_Switch::right() +{ + return !(_pins[3]); +} +inline bool Nav_Switch::fire() +{ + return !(_pins[4]); +} +inline int Nav_Switch::read() +{ + return _pins.read(); +} +inline Nav_Switch::operator int () +{ + return _pins.read(); +} + +//Nav_Switch myNav( p9, p6, p7, p5, p8); //pin order on Sparkfun breakout + +//int main() +//{ +// while(1) { +// //with pullups a button hit is a "0" - "~" inverts data to leds +// mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs +// if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) +// //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this +// wait(0.02); +// } +//} \ No newline at end of file
diff -r 000000000000 -r 9dc33481ce1b PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r 9dc33481ce1b RGBLed.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RGBLed.h Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,76 @@ +#include "mbed.h" + +//Class to control an RGB LED using three PWM pins +class RGBLed +{ +public: + RGBLed(PinName redpin, PinName greenpin, PinName bluepin); + void write(float red,float green, float blue); + void write(LEDColor c); + RGBLed operator = (LEDColor c) { + write(c); + return *this; + }; +private: + PwmOut _redpin; + PwmOut _greenpin; + PwmOut _bluepin; +}; + +RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin) + : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin) +{ + //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker) + _redpin.period(0.0005); +} + +void RGBLed::write(float red,float green, float blue) +{ + _redpin = red; + _greenpin = green; + _bluepin = blue; +} +void RGBLed::write(LEDColor c) +{ + _redpin = c.red; + _greenpin = c.green; + _bluepin = c.blue; +} + +////classes could be moved to include file +// +// +////Setup RGB led using PWM pins and class +//RGBLed myRGBled(p23,p22,p21); //RGB PWM pins +// +////setup some color objects in flash using const's +//const LEDColor red(1.0,0.0,0.0); +//const LEDColor green(0.0,0.2,0.0); +////brighter green LED is scaled down to same as red and +////blue LED outputs on Sparkfun RGBLED +//const LEDColor blue(0.0,0.0,1.0); +//const LEDColor yellow(1.0,0.2,0.0); +//const LEDColor white(1.0,0.2,1.0); +// +//int main() +//{ +// while(1) { +// myRGBled = red; +// //myRGBled.write(1.0,0.0,0,0); //red +// wait(2.0); +// myRGBled = green; +// //myRGBled.write(0.0,1.0,0.0); //green +// wait(2.0); +// myRGBled = blue; +// //myRGBled.write(0.0,0.0,1.0); //blue +// wait(2.0); +// myRGBled = red + green; //yellow = red + green +// wait(2.0); +// //white with a slow fade to black dimming effect +// for (float brightness=1.0; brightness>=0.0001; brightness*=0.99) { +// myRGBled = white * brightness; +// wait(0.005); +// } +// wait(2.0); +// } +//} \ No newline at end of file
diff -r 000000000000 -r 9dc33481ce1b lab1.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lab1.cpp Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,123 @@ +// ECE 4180 +// Lab/Project 1 +// Jeremy M. Deremer +// Due 26 January, 2017 + +#include "mbed.h" +#include "PinDetect.h" +#include "LEDColor.h" +#include "RGBLed.h" +#include "MCP23S17.h" +#include "Nav_Switch.h" + +PwmOut led(p24); + +// Instantiate pushbuttons on analog pins +PinDetect pb1(p17); +PinDetect pb2(p18); +PinDetect pb3(p19); + +// Instantiate RGBLed and allocate default colors +//RGBLed myRGBled(p23,p22,p21); //RGB PWM pins +//const LEDColor off(0.0,0.0,0.0); +//const LEDColor blue(0.0,0.0,1.0); + +// Instantiate I/O Expander +// Create SPI bus +SPI spi(p5, p6, p7); +char Opcode = 0x40; +// Next create a MCP23S17 +MCP23S17 chip = MCP23S17(spi, p20, Opcode); +DigitalOut led1(LED1); // mbed LED1 is used for test status display + +// Instantiate Nav Joystick +Nav_Switch myNav(p15, p12, p13, p11, p8); //pin order on Sparkfun breakout + +float inc = 0.1f; +float val = 0.5f; + +// Callback routine is interrupt activated by a debounced pb1 hit +void pb1_hit_callback (void) +{ + val = led.read(); + if (val == 0.00f) + { + led.write(0.50f); // 50% duty cycle + } + else + { + led.write(0.00f); // 0% duty cycle + } +} + +// Callback routine is interrupt activated by a debounced pb2 hit +void pb2_hit_callback (void) +{ + // add brightness + val = led.read(); + if (val < 1.00f) + { + val += inc; + led.write(val); + } +} + +// Callback routine is interrupt activated by a debounced pb3 hit +void pb3_hit_callback (void) +{ + // subtract brightness + val = led.read(); + if (val > 0.0f) + { + val -= inc; + led.write(val); + } +} + +int main() { + + led.write(0.5f); // 50% duty cycle + + // Use internal pullups for the three pushbuttons + pb1.mode(PullUp); + pb2.mode(PullUp); + pb3.mode(PullUp); + // Delay for initial pullup to take effect + wait(.01); + // Setup Interrupt callback functions for a pb hit + pb1.attach_deasserted(&pb1_hit_callback); + pb2.attach_deasserted(&pb2_hit_callback); + pb3.attach_deasserted(&pb3_hit_callback); + // Start sampling pb inputs using interrupts + pb1.setSampleFrequency(); + pb2.setSampleFrequency(); + pb3.setSampleFrequency(); + // pushbuttons now setup and running + + // Set all 8 Port A bits to output direction + chip.direction(PORT_A, 0x00); + // Set all 8 Port B bits to input direction + chip.direction(PORT_B, 0xFF); + + while(1) // led flashing + { + // write 0xAA to MCP23S17 Port A + chip.write(PORT_A, 0xAA); + + // read back value from MCP23S17 Port B and display B0 on mbed led1 + if ((chip.read(PORT_B)& 0x01) == 0x01) + { + chip.write(PORT_A, 0x55); + } + + //chip.write(PORT_A, (chip.read(PORT_B)& 0x01)); + + + //with pullups a button hit is a "0" - "~" inverts data to leds + mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs + if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) + //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this + //wait(0.02); + + } +} \ No newline at end of file
diff -r 000000000000 -r 9dc33481ce1b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Mar 01 16:36:55 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/ad3be0349dc5 \ No newline at end of file