ECE 4180 Lab 1 Part 3

Dependencies:   PinDetect mbed

Committer:
abraha2d
Date:
Tue Oct 09 00:33:50 2018 +0000
Revision:
0:d8abb9cdbd04
Save point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abraha2d 0:d8abb9cdbd04 1 #include "mbed.h"
abraha2d 0:d8abb9cdbd04 2 #include "PinDetect.h"
abraha2d 0:d8abb9cdbd04 3
abraha2d 0:d8abb9cdbd04 4 //Class to control an RGB LED using three PWM pins
abraha2d 0:d8abb9cdbd04 5 class RGBLed
abraha2d 0:d8abb9cdbd04 6 {
abraha2d 0:d8abb9cdbd04 7 public:
abraha2d 0:d8abb9cdbd04 8 RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
abraha2d 0:d8abb9cdbd04 9 void write(float red,float green, float blue);
abraha2d 0:d8abb9cdbd04 10 private:
abraha2d 0:d8abb9cdbd04 11 PwmOut _redpin;
abraha2d 0:d8abb9cdbd04 12 PwmOut _greenpin;
abraha2d 0:d8abb9cdbd04 13 PwmOut _bluepin;
abraha2d 0:d8abb9cdbd04 14 };
abraha2d 0:d8abb9cdbd04 15
abraha2d 0:d8abb9cdbd04 16 RGBLed::RGBLed (PinName redpin, PinName greenpin, PinName bluepin)
abraha2d 0:d8abb9cdbd04 17 : _redpin(redpin), _greenpin(greenpin), _bluepin(bluepin)
abraha2d 0:d8abb9cdbd04 18 {
abraha2d 0:d8abb9cdbd04 19 //50Hz PWM clock default a bit too low, go to 2000Hz (less flicker)
abraha2d 0:d8abb9cdbd04 20 _redpin.period(0.0005);
abraha2d 0:d8abb9cdbd04 21 }
abraha2d 0:d8abb9cdbd04 22
abraha2d 0:d8abb9cdbd04 23 void RGBLed::write(float red,float green, float blue)
abraha2d 0:d8abb9cdbd04 24 {
abraha2d 0:d8abb9cdbd04 25 _redpin = red;
abraha2d 0:d8abb9cdbd04 26 _greenpin = green;
abraha2d 0:d8abb9cdbd04 27 _bluepin = blue;
abraha2d 0:d8abb9cdbd04 28 }
abraha2d 0:d8abb9cdbd04 29
abraha2d 0:d8abb9cdbd04 30 //Setup RGB led using PWM pins and class
abraha2d 0:d8abb9cdbd04 31 RGBLed myRGBled(p21,p22,p23); //RGB PWM pins
abraha2d 0:d8abb9cdbd04 32
abraha2d 0:d8abb9cdbd04 33 DigitalIn r(p24);
abraha2d 0:d8abb9cdbd04 34 DigitalIn g(p25);
abraha2d 0:d8abb9cdbd04 35 DigitalIn b(p26);
abraha2d 0:d8abb9cdbd04 36
abraha2d 0:d8abb9cdbd04 37 PinDetect pb1(p8);
abraha2d 0:d8abb9cdbd04 38 PinDetect pb2(p7);
abraha2d 0:d8abb9cdbd04 39
abraha2d 0:d8abb9cdbd04 40 // Global pwm variable
abraha2d 0:d8abb9cdbd04 41 float volatile pwm=1.0f;
abraha2d 0:d8abb9cdbd04 42
abraha2d 0:d8abb9cdbd04 43 // Callback routine is interrupt activated by a debounced pb1 hit
abraha2d 0:d8abb9cdbd04 44 void pb1_hit_callback (void)
abraha2d 0:d8abb9cdbd04 45 {
abraha2d 0:d8abb9cdbd04 46 pwm -= 0.1f;
abraha2d 0:d8abb9cdbd04 47 if (pwm < 0.0f) {
abraha2d 0:d8abb9cdbd04 48 pwm = 0.0f;
abraha2d 0:d8abb9cdbd04 49 }
abraha2d 0:d8abb9cdbd04 50 }
abraha2d 0:d8abb9cdbd04 51
abraha2d 0:d8abb9cdbd04 52 // Callback routine is interrupt activated by a debounced pb2 hit
abraha2d 0:d8abb9cdbd04 53 void pb2_hit_callback (void)
abraha2d 0:d8abb9cdbd04 54 {
abraha2d 0:d8abb9cdbd04 55 pwm += 0.1f;
abraha2d 0:d8abb9cdbd04 56 if (pwm > 1.0f) {
abraha2d 0:d8abb9cdbd04 57 pwm = 1.0f;
abraha2d 0:d8abb9cdbd04 58 }
abraha2d 0:d8abb9cdbd04 59 }
abraha2d 0:d8abb9cdbd04 60
abraha2d 0:d8abb9cdbd04 61 int main()
abraha2d 0:d8abb9cdbd04 62 {
abraha2d 0:d8abb9cdbd04 63 r.mode(PullUp);
abraha2d 0:d8abb9cdbd04 64 g.mode(PullUp);
abraha2d 0:d8abb9cdbd04 65 b.mode(PullUp);
abraha2d 0:d8abb9cdbd04 66
abraha2d 0:d8abb9cdbd04 67 // Use internal pullups for pushbutton
abraha2d 0:d8abb9cdbd04 68 pb1.mode(PullUp);
abraha2d 0:d8abb9cdbd04 69 pb2.mode(PullUp);
abraha2d 0:d8abb9cdbd04 70 // Delay for initial pullup to take effect
abraha2d 0:d8abb9cdbd04 71 wait(.01);
abraha2d 0:d8abb9cdbd04 72 // Setup Interrupt callback functions for a pb hit
abraha2d 0:d8abb9cdbd04 73 pb1.attach_deasserted(&pb1_hit_callback);
abraha2d 0:d8abb9cdbd04 74 pb2.attach_deasserted(&pb2_hit_callback);
abraha2d 0:d8abb9cdbd04 75 // Start sampling pb inputs using interrupts
abraha2d 0:d8abb9cdbd04 76 pb1.setSampleFrequency();
abraha2d 0:d8abb9cdbd04 77 pb2.setSampleFrequency();
abraha2d 0:d8abb9cdbd04 78
abraha2d 0:d8abb9cdbd04 79 while(1) {
abraha2d 0:d8abb9cdbd04 80 myRGBled.write(!r*pwm,!g*pwm,!b*pwm);
abraha2d 0:d8abb9cdbd04 81 }
abraha2d 0:d8abb9cdbd04 82 }