Joe Shotton / ll16j23s_test_docs

Files at this revision

API Documentation at this revision

Comitter:
eencae
Date:
Sat Feb 04 16:13:18 2017 +0000
Parent:
0:a6288c29b936
Child:
2:ea5538fcfe2f
Commit message:
Added more methods. Still untested.

Changed in this revision

Gamepad.cpp Show annotated file Show diff for this revision Revisions of this file
Gamepad.h Show annotated file Show diff for this revision Revisions of this file
--- a/Gamepad.cpp	Sat Feb 04 15:31:44 2017 +0000
+++ b/Gamepad.cpp	Sat Feb 04 16:13:18 2017 +0000
@@ -1,5 +1,6 @@
 #include "Gamepad.h"
 
+//////////// constructor/destructor ////////////
 Gamepad::Gamepad()
 {
     led_1 = new PwmOut(PTA1);
@@ -24,30 +25,223 @@
     buzzer = new PwmOut(PTC10);
     pot = new AnalogIn(PTB2);
 
+    timeout = new Timeout();
+
 }
 
+Gamepad::~Gamepad()
+{
+    delete led_1,led_2,led_3,led_4,led_5,led_6;
+    delete lcd,joystick,button_A,button_B;
+    delete button_X, button_Y, button_back, button_start;
+    delete button_L, button_R, buzzer, pot, timeout;
+}
+
+///////////////// public methods /////////////////
+
 void Gamepad::init()
 {
     lcd->init();
     joystick->init();
+    leds_off();
+
+    buzzer->period(1.0/1000.0);  // 1 kHz
+
+    // clear all flags
+    a_flag=0,b_flag=0,x_flag=0,y_flag=0,l_flag=0,r_flag=0,back_flag=0,start_flag=0;
+
 }
 
 void Gamepad::leds_off()
 {
-    led_1->write(1.0);
-    led_2->write(1.0);
-    led_3->write(1.0);
-    led_4->write(1.0);
-    led_5->write(1.0);
-    led_6->write(1.0);
+    fade_leds(0.0);
 }
 
 void Gamepad::leds_on()
 {
-    led_1->write(0.0);
-    led_2->write(0.0);
-    led_3->write(0.0);
-    led_4->write(0.0);
-    led_5->write(0.0);
-    led_6->write(0.0);
+    fade_leds(1.0);
+}
+
+void Gamepad::fade_leds(float val)
+{
+    if (val < 0.0f) {
+        val = 0.0f;
+    }
+    if (val > 1.0f) {
+        val = 1.0f;
+    }
+
+    // leds are active-low, so subtract from 1.0
+    // 0.0 corresponds to fully-off, 1.0 to fully-on
+    val = 1.0f - val;
+
+    led_1->write(val);
+    led_2->write(val);
+    led_3->write(val);
+    led_4->write(val);
+    led_5->write(val);
+    led_6->write(val);
+}
+
+float Gamepad::read_pot()
+{
+    return pot->read();
+}
+
+void Gamepad::tone(float frequency, float duration)
+{
+    buzzer->period(1.0f/frequency);
+    buzzer->write(0.5);  // 50% duty cycle - square wave
+    timeout->attach(callback(this, &Gamepad::tone_off), duration );
+}
+
+bool Gamepad::a_pressed()
+{
+    // ISR must have been triggered
+    if (a_flag) {
+        a_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool Gamepad::b_pressed()
+{
+    // ISR must have been triggered
+    if (b_flag) {
+        b_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool Gamepad::x_pressed()
+{
+    // ISR must have been triggered
+    if (x_flag) {
+        x_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool Gamepad::y_pressed()
+{
+    // ISR must have been triggered
+    if (y_flag) {
+        y_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool Gamepad::l_pressed()
+{
+    // ISR must have been triggered
+    if (l_flag) {
+        l_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
 }
+
+bool Gamepad::r_pressed()
+{
+    // ISR must have been triggered
+    if (r_flag) {
+        r_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool Gamepad::back_pressed()
+{
+    // ISR must have been triggered
+    if (back_flag) {
+        back_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool Gamepad::start_pressed()
+{
+    // ISR must have been triggered
+    if (a_flag) {
+        a_flag = 0;  // clear flag
+        return true;
+    } else {
+        return false;
+    }
+}
+///////////////////// private methods ////////////////////////
+
+void Gamepad::tone_off()
+{
+    buzzer->write(0.0);
+}
+
+void Gamepad::init_buttons()
+{
+    // turn on pull-downs as other side of button is connected to 3V3
+    // button is 0 when not pressed and 1 when pressed
+    button_A->mode(PullDown);
+    button_B->mode(PullDown);
+    button_X->mode(PullDown);
+    button_Y->mode(PullDown);
+    button_back->mode(PullDown);
+    button_start->mode(PullDown);
+    button_L->mode(PullDown);
+    button_R->mode(PullDown);
+    // therefore setup rising edge interrupts
+    button_A->rise(callback(this,&Gamepad::a_isr));
+    button_B->rise(callback(this,&Gamepad::b_isr));
+    button_X->rise(callback(this,&Gamepad::x_isr));
+    button_Y->rise(callback(this,&Gamepad::y_isr));
+    button_L->rise(callback(this,&Gamepad::l_isr));
+    button_R->rise(callback(this,&Gamepad::r_isr));
+    button_start->rise(callback(this,&Gamepad::start_isr));
+    button_back->rise(callback(this,&Gamepad::back_isr));
+}
+
+// button interrupts ISRs
+void Gamepad::a_isr()
+{
+    a_flag=1;
+}
+void Gamepad::b_isr()
+{
+    b_flag=1;
+}
+void Gamepad::x_isr()
+{
+    x_flag=1;
+}
+void Gamepad::y_isr()
+{
+    y_flag=1;
+}
+void Gamepad::l_isr()
+{
+    l_flag=1;
+}
+void Gamepad::r_isr()
+{
+    r_flag=1;
+}
+void Gamepad::back_isr()
+{
+    back_flag=1;
+}
+void Gamepad::start_isr()
+{
+    start_flag=1;
+}
\ No newline at end of file
--- a/Gamepad.h	Sat Feb 04 15:31:44 2017 +0000
+++ b/Gamepad.h	Sat Feb 04 16:13:18 2017 +0000
@@ -11,12 +11,26 @@
 public:
 
     Gamepad();
+    ~Gamepad();
     N5110 *lcd;
     Joystick *joystick;
 
     void init();
     void leds_on();
     void leds_off();
+    void fade_leds(float val);
+
+    float read_pot();
+    void tone(float frequency, float duration);
+
+    bool a_pressed();
+    bool b_pressed();
+    bool x_pressed();
+    bool y_pressed();
+    bool l_pressed();
+    bool r_pressed();
+    bool back_pressed();
+    bool start_pressed();
 
 private:
 
@@ -27,17 +41,32 @@
     PwmOut *led_5;
     PwmOut *led_6;
 
-    InterruptIn *button_A(PTB9);
-    InterruptIn *button_B(D10);
-    InterruptIn *button_X(PTC17);
-    InterruptIn *button_Y(PTC12);  // changed pin
-    InterruptIn *button_back(PTB19);
-    InterruptIn *button_start(PTC5);
-    InterruptIn *button_L(PTB18);
-    InterruptIn *button_R(PTB3);
-    
-    PwmOut *buzzer(PTC10);
-    AnalogIn *pot(PTB2);
+    InterruptIn *button_A;
+    InterruptIn *button_B;
+    InterruptIn *button_X;
+    InterruptIn *button_Y;  // changed pin
+    InterruptIn *button_back;
+    InterruptIn *button_start;
+    InterruptIn *button_L;
+    InterruptIn *button_R;
+
+    PwmOut *buzzer;
+    AnalogIn *pot;
+
+    Timeout *timeout;
+
+    void init_buttons();
+    void tone_off();
+    void a_isr();
+    void b_isr();
+    void x_isr();
+    void y_isr();
+    void l_isr();
+    void r_isr();
+    void back_isr();
+    void start_isr();
+
+    bool a_flag,b_flag,x_flag,y_flag,l_flag,r_flag,back_flag,start_flag;
 
 };