Joseph Boettcher / Mbed 2 deprecated uLCD_Robot

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Files at this revision

API Documentation at this revision

Comitter:
jboettcher
Date:
Sun Nov 06 21:57:20 2016 +0000
Commit message:
Complete

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
Robot.h Show annotated file Show diff for this revision Revisions of this file
Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Sun Nov 06 21:57:20 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Sun Nov 06 21:57:20 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Robot.h	Sun Nov 06 21:57:20 2016 +0000
@@ -0,0 +1,31 @@
+#include <iostream>
+using namespace std;
+
+class Robot
+{
+public:
+    void draw() {
+        uLCD.circle(xPosition - 8, yPosition - 8, radius, BLACK);    
+        uLCD.circle(xPosition + 8, yPosition - 8, radius, BLACK);
+        uLCD.filled_circle(xPosition - 8, yPosition - 8, 2, BLACK);    
+        uLCD.filled_circle(xPosition + 8, yPosition - 8, 2, BLACK);  }  
+    void erase() {
+        uLCD.filled_rectangle(0, 126, 126, 26, RED);    }
+    void moveForward(int distance) {
+        radius++;    }
+    void moveBackward(int distance) {
+        radius--;    }
+    void moveLeft(int distance) {
+        xPosition--;    }
+    void moveRight(int distance) {
+        xPosition++;    }
+    Robot() {
+        radius = 5;
+        xPosition = 63;
+        yPosition = 63;
+    }
+private:
+    int radius;
+    int xPosition;
+    int yPosition;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Sun Nov 06 21:57:20 2016 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Nov 06 21:57:20 2016 +0000
@@ -0,0 +1,136 @@
+
+#include "mbed.h"
+#include "Speaker.h"
+#include "PinDetect.h"
+#include "uLCD_4DGL.h"
+// setup builtin LEDs
+DigitalOut myLed1(LED1);
+DigitalOut myLed2(LED2);
+DigitalOut myLed3(LED3);
+DigitalOut myLed4(LED4);
+// setup pushbuttons for debounced interrupt use
+PinDetect pb1(p23);
+PinDetect pb2(p24);
+PinDetect pb3(p25);
+PinDetect pb4(p26);
+
+// setup LCD
+uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
+
+//moving include down here allows use in uLCD in Robot class draw and erase member functions
+//easier for now than passing a new uLCD_4DGL object argument to the class
+#include "Robot.h"
+
+// setup instance of new Speaker class, mySpeaker using pin 21
+// the pin must be a PWM output pin
+Speaker mySpeaker(p21);
+
+// pushbutton status - need volatile for safe interrupt R/W
+volatile int pbStatus = 0;
+
+// Callback routine is interrupt activated by a debounced pb1 hit
+void pb1_hit_callback (void)
+{
+    // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED
+    pbStatus = pbStatus | 0x01;
+
+}
+// Callback routine is interrupt activated by a debounced pb2 hit
+void pb2_hit_callback (void)
+{
+    // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED
+    pbStatus = pbStatus | 0x02;
+}
+// Callback routine is interrupt activated by a debounced pb3 hit
+void pb3_hit_callback (void)
+{
+    // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED
+    pbStatus = pbStatus | 0x04;
+}
+// Callback routine is interrupt activated by a debounced pb4 hit
+void pb4_hit_callback (void)
+{
+    // CODE HERE WILL RUN WHEN INTERUPT IS GENERATED
+    pbStatus = pbStatus | 0x08;
+}
+
+// main program
+int main()
+{
+    uLCD.printf("\nmyTitties!\nJoseph Boettcher");
+//setup four SPST push buttons
+    pb1.mode(PullUp); //add internal pullup resistor
+    pb2.mode(PullUp);
+    pb3.mode(PullUp);
+    pb4.mode(PullUp);
+    // need a small delay for initial pullup to take effect due to capacitance
+    wait(.01);
+    // Setup Interrupt callback functions for each pb hit
+    pb1.attach_deasserted(&pb1_hit_callback);
+    pb2.attach_deasserted(&pb2_hit_callback);
+    pb3.attach_deasserted(&pb3_hit_callback);
+    pb4.attach_deasserted(&pb4_hit_callback);
+    // Start sampling the pb inputs using interrupts
+    pb1.setSampleFrequency();
+    pb2.setSampleFrequency();
+    pb3.setSampleFrequency();
+    pb4.setSampleFrequency();
+// pushbuttons are all now setup with pullups and running with a software decounce filter
+
+// use your modified robot class to move and display robot's new location on LCD
+    Robot myRobot;
+
+    myRobot.draw(); //initial robot display in center of LCD
+//main loop to control and display robot
+    while(1) {
+        //check pushbutton status for new input
+        switch (pbStatus) {
+                //move forward
+            case 0x01 :
+                myLed1 = 1;
+                pbStatus = 0;
+                mySpeaker.PlayNote(200.0,0.1,0.8);
+                myRobot.erase();
+                myRobot.moveForward(1);
+                myRobot.draw();
+                break;
+                //move backwards
+            case 0x02 :
+                myLed2 = 1;
+                pbStatus = 0;
+                mySpeaker.PlayNote(400.0,0.1,0.8);
+                myRobot.erase();
+                myRobot.moveBackward(1);
+                myRobot.draw();
+                break;
+                //move left
+            case 0x04 :
+                myLed3 = 1;
+                pbStatus = 0;
+                mySpeaker.PlayNote(600.0,0.1,0.8);
+                myRobot.erase();
+                myRobot.moveLeft(1);
+                myRobot.draw();
+                break;
+                //move right
+            case 0x08 :
+                myLed4 = 1;
+                pbStatus = 0;
+                mySpeaker.PlayNote(800.0,0.1,0.8);
+                myRobot.erase();
+                myRobot.moveRight(1);
+                myRobot.draw();
+                break;
+                //no pb or multiple pbs hit
+            default :
+                myLed1 = 0;
+                myLed2 = 0;
+                myLed3 = 0;
+                myLed4 = 0;
+                pbStatus = 0;
+        } //end switch
+        //myLed1 = !myLed1; //blink to show loop is still running
+        wait(.05);
+    } //end while
+} //end main
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Nov 06 21:57:20 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file