ece 4180 lab 3

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem X_NUCLEO_53L0A1 HC_SR04_Ultrasonic_Library

Files at this revision

API Documentation at this revision

Comitter:
emilywilson
Date:
Wed Feb 19 18:48:09 2020 +0000
Parent:
1:e4d7342be507
Child:
3:94d9434576af
Commit message:
led rtos

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
Color.h Show annotated file Show diff for this revision Revisions of this file
HC_SR04_Ultrasonic_Library.lib Show annotated file Show diff for this revision Revisions of this file
RGBLed.h Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_53L0A1.lib Show annotated file Show diff for this revision Revisions of this file
lidar_theremin.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-rtos.lib Show annotated file Show diff for this revision Revisions of this file
part2.h Show annotated file Show diff for this revision Revisions of this file
part3.h Show annotated file Show diff for this revision Revisions of this file
part4.h Show annotated file Show diff for this revision Revisions of this file
part4_led.h Show annotated file Show diff for this revision Revisions of this file
wave_player.lib 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	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Color.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,14 @@
+class Color {
+    public:
+        Color(float r, float g, float b);
+    public:
+        float red;
+        float green;
+        float blue;
+};
+
+Color::Color (float r, float g, float b) {
+    red = r;
+    green = g;
+    blue = b;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HC_SR04_Ultrasonic_Library.lib	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RGBLed.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,24 @@
+class RGBLed
+{
+public:
+    RGBLed(PinName redpin, PinName greenpin, PinName bluepin);
+    void write(float red,float green, float blue);
+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;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_53L0A1.lib	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/X_NUCLEO_53L0A1/#27d3d95c8593
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lidar_theremin.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,76 @@
+#include "mbed.h"
+#include "XNucleo53L0A1.h"
+#include <stdio.h>
+Serial pc(USBTX,USBRX);
+DigitalOut shdn(p25);
+// This VL53L0X board test application performs a range measurement in polling mode
+// Use 3.3(Vout) for Vin, p28 for SDA, p27 for SCL, P26 for shdn on mbed LPC1768
+
+//I2C sensor pins
+#define VL53L0_I2C_SDA   p28
+#define VL53L0_I2C_SCL   p27
+
+static XNucleo53L0A1 *board=NULL;
+
+DigitalOut audio(p26); //output to speaker amp or audio jack
+DigitalOut led(LED1); 
+DigitalOut led2(LED2);
+
+//Timeout cycle;
+Ticker cycle;
+ 
+volatile int half_cycle_time = 1;
+ 
+//two calls to this interrupt routine generates a square wave
+void toggle_interrupt()
+{
+    if (half_cycle_time>22000) audio=0; //mute if nothing in range
+    else audio = !audio; //toggle to make half a square wave
+    led = !led;
+    cycle.detach();
+    //update time for interrupt activation -change frequency of square wave
+    cycle.attach_us(&toggle_interrupt, half_cycle_time);
+}
+
+uint32_t distance;
+void newdist()
+{
+    //update frequency based on new sonar data
+    led2 = !led2;
+    half_cycle_time = (distance*10)<<3;
+}  
+
+//Set the trigger pin to p6 and the echo pin to p7
+//have updates every .07 seconds and a timeout after 1
+//second, and call newdist when the distance changes
+int run_lidar_theremin()
+{
+    int status;
+    DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
+    /* creates the 53L0A1 expansion board singleton obj */
+    board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
+    shdn = 0; //must reset sensor for an mbed reset to work
+    wait(0.1);
+    shdn = 1;
+    wait(0.1);
+    /* init the 53L0A1 board with default values */
+    status = board->init_board();
+    
+    while (status) {
+        pc.printf("Failed to init board! \r\n");
+        status = board->init_board();
+    }
+    //loop taking and printing distance
+    
+    audio = 0;
+    led = 0;
+    cycle.attach(&toggle_interrupt, half_cycle_time);
+    while(1) {
+        //Do something else here
+        status = board->sensor_centre->get_distance(&distance);
+        newdist();
+        pc.printf("distance %d", distance);    
+        //call checkDistance() as much as possible, as this is where
+        //the class checks if dist needs to be called.
+    }
+}
--- a/main.cpp	Tue May 16 05:18:55 2017 +0000
+++ b/main.cpp	Wed Feb 19 18:48:09 2020 +0000
@@ -1,19 +1,67 @@
 #include "mbed.h"
-#include "SDFileSystem.h"
- 
-SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
- 
+#include "rtos.h"
+//#include "part2.h"
+//#include "part3.h"
+#include "part4_led.h"
+//#include "lidar_theremin.h"
+
+Serial pc(USBTX, USBRX);
+RawSerial  dev(p13,p14);
+
+DigitalOut myled1(LED1);
+RGBLed led = RGBLed(p21, p22, p23);
+Color colors[] = { _red, _pink, _orange, _yellow };
+
+Thread led_thread;
+
+void changeColor(char c) {
+    if (c == 'r') {
+        colors[0] = _red;
+        colors[1] = _pink;
+        colors[2] = _orange;
+        colors[3] = _yellow;
+    } else if (c == 'b') {
+        colors[0] = _teal;
+        colors[1] = _light_blue;
+        colors[2] = _blue;
+        colors[3] = _purple;
+    } else if (c == 'g') {
+        colors[0] = _yellow;
+        colors[1] = _light_green;
+        colors[2] = _green;
+        colors[3] = _teal;
+    }
+}
+
+void led_handler() {
+    Color c = colors[0];
+    int i = 0;
+    while (1) {
+        led.write(c.red, c.green, c.blue);
+        i = (i + 1) % 4;
+        c = colors[i];
+        
+        Thread::wait(500);
+    }
+}
+
 int main() {
-    printf("Hello World!\n");   
- 
-    mkdir("/sd/mydir", 0777);
+//    run_part2();
+//    run_part2_EC();
+//    run_lidar_theremin();
     
-    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
-    if(fp == NULL) {
-        error("Could not open file for write\n");
+//    run_part3();
+    
+    led_thread.start(led_handler);
+
+    while (1) {
+        myled1 = !myled1;
+        
+        if (dev.readable()) {
+//            changeColor(dev.getc());
+            pc.putc(dev.getc());
+        }
+
+        Thread::wait(500);
     }
-    fprintf(fp, "Hello fun SD Card World!");
-    fclose(fp); 
- 
-    printf("Goodbye World!\n");
-}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part2.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "ultrasonic.h"
+ 
+Serial pc(USBTX, USBRX);
+
+void dist(int distance)
+{
+    //put code here to execute when the distance has changed
+    pc.printf("Distance %d mm\r\n", distance);
+}
+ 
+ultrasonic mu(p6, p7, .1, 1, &dist);    //Set the trigger pin to p6 and the echo pin to p7
+                                        //have updates every .1 seconds and a timeout after 1
+                                        //second, and call dist when the distance changes
+ 
+int run_part2()
+{
+    mu.startUpdates();//start measuring the distance
+    while(1)
+    {
+        //Do something else here
+        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
+                                //the class checks if dist needs to be called.
+    }
+}
+
+//Theremin style demo using HC-SR04 Sonar and a speaker
+// moving a hand away/towards sonar changes audio frequency
+ 
+DigitalOut audio(p26); //output to speaker amp or audio jack
+DigitalOut led(LED1); 
+DigitalOut led2(LED2);
+ 
+Timeout cycle;
+ 
+volatile int half_cycle_time = 1;
+ 
+//two calls to this interrupt routine generates a square wave
+void toggle_interrupt()
+{
+    if (half_cycle_time>22000) audio=0; //mute if nothing in range
+    else audio = !audio; //toggle to make half a square wave
+    led = !led;
+    cycle.detach();
+    //update time for interrupt activation -change frequency of square wave
+    cycle.attach_us(&toggle_interrupt, half_cycle_time);
+}
+void newdist(int distance)
+{
+    //update frequency based on new sonar data
+    led2 = !led2;
+    half_cycle_time = distance<<3;
+}
+//HC-SR04 Sonar module
+ultrasonic mu2(p6, p7, .07, 1, &newdist);    
+//Set the trigger pin to p6 and the echo pin to p7
+//have updates every .07 seconds and a timeout after 1
+//second, and call newdist when the distance changes
+int run_part2_EC()
+{
+    audio = 0;
+    led = 0;
+    cycle.attach(&toggle_interrupt, half_cycle_time);
+    mu2.startUpdates();//start measuring the distance with the sonar
+    while(1) {
+        //Do something else here
+        mu2.checkDistance();     
+        //call checkDistance() as much as possible, as this is where
+        //the class checks if dist needs to be called.
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part3.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+Ticker ticker1;
+Ticker ticker2;
+Ticker ticker3;
+Ticker ticker4;
+
+void flipLED1() {
+    led1 = !led1;
+}
+
+void flipLED2() {
+    led2 = !led2;
+}
+
+void flipLED3() {
+    led3 = !led3;
+}
+
+void flipLED4() {
+    led4 = !led4;
+}
+
+void run_part3() {
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+    
+    ticker1.attach(&flipLED1, 1.0);
+    ticker2.attach(&flipLED2, 2.0);
+    ticker3.attach(&flipLED3, 4.0);
+    ticker4.attach(&flipLED4, 8.0);
+    
+    while(1);
+}
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part4.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,114 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+#include "uLCD_4DGL.h"
+#include "RGBLed.h"
+#include "Color.h"
+
+#define _pink Color(255.0, 0.0, (119.0/255.0))
+#define _red Color(255.0, 0.0, (119.0/255.0))
+#define _orange Color(255.0/255.0, 98.0/255.0, 0.0)
+#define _yellow Color(255.0/255.0, 166.0/255.0, 0.0)
+#define _light_green Color(187.0/255.0, 255.0/255.0, 0.0)
+#define _green Color(17.0/255.0, 201.0/255.0, 0.0)
+#define _teal Color(0.0, 255.0/255.0, 170.0/255.0)
+#define _light_blue Color(0.0, 255.0/255.0, 247.0/255.0)
+#define _blue Color(0.0, 128.0/255.0, 255.0/255.0)
+#define _purple Color(106.0/255.0, 0.0, 255.0/255.0)
+
+RGBLed led = RGBLed(p21, p22, p23);
+
+Color colors[] = { _red, _pink, _orange, _yellow };
+
+void led_handler() {
+//    Color colors[] = { _red, _pink, _orange, _yellow };
+    
+    Color c = colors[0];
+    int i = 0;
+    while (1) {
+//        led.write(c.red, c.green, c.blue);
+        //i = (i + 1) % 4;
+//        Color colors[] = { _red, _pink, _orange, _yellow };
+        led.write(1.0, 1.0, 1.0);
+        
+        Thread::wait(500);
+    }
+}
+
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
+
+void audio_handler() {
+    FILE *wave_file;
+    printf("\n\n\nHello, wave world!\n");
+    wave_file=fopen("/sd/sample.wav","r");
+    while (1) {
+        waver.play(wave_file);
+        Thread::wait(1);
+    }
+    fclose(wave_file);
+}
+
+Mutex lcd_mutex;
+uLCD_4DGL lcd(p28,p27,p30);
+
+void lcd1_handler() {
+    int i = 0;
+    while (1) {
+        lcd_mutex.lock();
+        lcd.locate(0, 0);
+        lcd.printf("Elapsed time: %d s", i);
+        lcd_mutex.unlock();
+        
+        i++;
+        Thread::wait(1000);
+    }
+}
+
+void lcd2_handler() {
+    while (1) {
+        lcd_mutex.lock();
+        
+        lcd_mutex.unlock();
+        Thread::wait(0);
+    }
+}
+
+void changeColor(char c) {
+    if (c == 'r') {
+        colors[0] = _red;
+        colors[1] = _pink;
+        colors[2] = _orange;
+        colors[3] = _yellow;
+    } else if (c == 'b') {
+        colors[0] = _teal;
+        colors[1] = _light_blue;
+        colors[2] = _blue;
+        colors[3] = _purple;
+    } else if (c == 'g') {
+        colors[0] = _yellow;
+        colors[1] = _light_green;
+        colors[2] = _green;
+        colors[3] = _teal;
+    }
+}
+
+Thread led_thread, audio_thread, lcd1_thread, lcd2_thread;
+
+
+void run_part4() {
+    led_thread.start(led_handler);
+//    audio_thread.start(audio_handler);
+//    lcd1_thread.start(lcd1_handler);
+//    lcd2_thread.start(lcd2_handler);
+    
+    //while (1) {
+//        if (dev.readable()) {
+//            changeColor(dev.getc());
+//        }
+//    }
+    while (1) ;
+            
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part4_led.h	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,13 @@
+#include "RGBLed.h"
+#include "Color.h"
+
+#define _pink Color(255.0, 0.0, (119.0/255.0))
+#define _red Color(255.0, 0.0, (119.0/255.0))
+#define _orange Color(255.0/255.0, 98.0/255.0, 0.0)
+#define _yellow Color(255.0/255.0, 166.0/255.0, 0.0)
+#define _light_green Color(187.0/255.0, 255.0/255.0, 0.0)
+#define _green Color(17.0/255.0, 201.0/255.0, 0.0)
+#define _teal Color(0.0, 255.0/255.0, 170.0/255.0)
+#define _light_blue Color(0.0, 255.0/255.0, 247.0/255.0)
+#define _blue Color(0.0, 128.0/255.0, 255.0/255.0)
+#define _purple Color(106.0/255.0, 0.0, 255.0/255.0)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Wed Feb 19 18:48:09 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad