Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem
Revision 12:8297b1d18cb2, committed 2021-04-12
- Comitter:
- carahcamron
- Date:
- Mon Apr 12 19:51:39 2021 +0000
- Parent:
- 11:0309bef74ba8
- Commit message:
- lab 3 code with bluetooth
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Mon Apr 12 19:51:39 2021 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#2cb1845d7681
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Mon Apr 12 19:51:39 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/neilt6/code/SDFileSystem/#e4d2567200db
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h Mon Apr 12 19:51:39 2021 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// a 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
--- a/main.cpp Wed Feb 15 14:04:02 2017 -0600
+++ b/main.cpp Mon Apr 12 19:51:39 2021 +0000
@@ -1,22 +1,143 @@
+
#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
#include "rtos.h"
-
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-Thread thread;
-
-void led2_thread() {
+
+//Create class for RGB LED with 3 PWM out;
+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;
+}
+
+
+//-----------------------------------------------------
+DigitalOut led1(LED1); // MBED Led 1
+DigitalOut led2(LED2); // MBED Led 2
+RGBLed myLED(p23,p24,p25); // RGB PWM pins
+uLCD_4DGL LCD(p9,p10,p11); // serial tx, serial rx, reset pin
+RawSerial bluemod(p28,p27); // bluetooth adafruit serial port
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // SDcard reader
+AnalogOut DACout(p18); // Speaker Analog Out
+wave_player waver(&DACout); // Creates wave player at same address
+
+
+// mutex to make the lcd lib safe
+Mutex lcd_mutex;
+
+Thread thread1;
+Thread thread2;
+Thread thread3;
+
+// Thread 5
+// RGB LED
+
+void led_thread()
+{
+ while(true) {
+ char bred=0;
+ char bgreen=0;
+ char bblue=0;
+ lcd_mutex.lock(); // thread loop
+ bred = 0.5 + (rand() % 11)/20.0;
+ bgreen = 0.5 + (rand() % 11)/20.0;
+ bblue = 0.5 + (rand() % 11)/20.0;
+ myLED.write(bred, bgreen, bblue);
+ lcd_mutex.unlock();
+ Thread::wait(1667); // wait 1.5s
+ }
+}
+
+void led_thread1() {
while (true) {
- led2 = !led2;
+ char bred=0;
+ char bgreen=0;
+ char bblue=0;
+ while(1) {
+ if(bluemod.readable()) {
+ lcd_mutex.lock();
+ if (bluemod.getc()=='!') {
+ if (bluemod.getc()=='C') { // color data packet
+ bred = bluemod.getc(); // RGB color values
+ bgreen = bluemod.getc();
+ bblue = bluemod.getc();
+ if (bluemod.getc()==char(~('!' + 'C' + bred + bgreen + bblue))) { //checksum OK?
+ myLED.write(bred/255.0, bgreen/255.0, bblue/255.0); // send new color to RGB LED PWM outputs
+
+ }
+ }
+ }
+ lcd_mutex.unlock();
+ }
+ Thread::wait(1000);// wait 1s
+ }
+ }
+}
+
+void waveplayer_thread() {
+ while (true) {
+ FILE *wave_file;
+ printf("\n\n\nHello, wave world!\n");
+ wave_file=fopen("/sd/sample.wav","r");
+ waver.play(wave_file);
+ fclose(wave_file);
+ Thread::wait(1500); // wait 0.5s
+ }
+}
+
+void lcd_thread() {
+ while(true) {
+ lcd_mutex.lock();
+ LCD.filled_circle(32,65,25, 0x000000);
+ LCD.circle(65,65,30,0xFF00FF);
+ LCD.filled_circle(97,65,25, 0xFF0000);
+ lcd_mutex.unlock();
+ Thread::wait(1500); // wait 0.5s
+ }
+}
+
+int main() {
+ lcd_mutex.lock();
+ LCD.cls();
+ LCD.background_color(BLACK);
+ LCD.baudrate(3000000);
+ lcd_mutex.unlock();
+
+ thread1.start(led_thread1); //start RGB thread
+ thread2.start(waveplayer_thread); //start LCD thread
+ thread3.start(lcd_thread); //start speaker thread
+
+
+ while (true) {
+ led1 = !led1;
+ lcd_mutex.lock();
+ LCD.filled_circle(97,65,25, 0x000000);
+ LCD.circle(65,65,30,0xFF00FF);
+ LCD.filled_circle(32,65,25, 0x0000FF);
+ lcd_mutex.unlock();
+
Thread::wait(1000);
}
}
-
-int main() {
- thread.start(led2_thread);
-
- while (true) {
- led1 = !led1;
- Thread::wait(500);
- }
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Mon Apr 12 19:51:39 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/sravet/code/wave_player/#acc3e18e77ad