mbed Starter Kit / mbed Starter Kit Demo Programs

Files at this revision

API Documentation at this revision

Comitter:
ShawnHymel
Date:
Mon Jul 28 20:29:28 2014 +0000
Commit message:
Added main.cpp files from each of the demo programs. Libraries are not included.

Changed in this revision

internet_clock/main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_blinky/main.cpp Show annotated file Show diff for this revision Revisions of this file
pwm_song/main.cpp Show annotated file Show diff for this revision Revisions of this file
rgb_buttons/main.cpp Show annotated file Show diff for this revision Revisions of this file
soundboard/main.cpp Show annotated file Show diff for this revision Revisions of this file
temp_logging/main.cpp Show annotated file Show diff for this revision Revisions of this file
ulcd_accel/main.cpp Show annotated file Show diff for this revision Revisions of this file
ulcd_demo/main.cpp Show annotated file Show diff for this revision Revisions of this file
usb_device/main.cpp Show annotated file Show diff for this revision Revisions of this file
usb_host/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/internet_clock/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,63 @@
+// Internet Clock with LCD based on the work by Jim Hamblen and Tyler Lisowski
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "NTPClient.h"
+#include "uLCD_4DGL.h"
+
+// Parameters
+char* domain_name = "0.uk.pool.ntp.org";
+int port_number = 123;
+
+// Networking
+EthernetInterface eth;
+NTPClient ntp_client;
+
+// Graphic LCD - TX, RX, and RES pins
+uLCD_4DGL uLCD(p9,p10,p11);
+
+int main() {
+        
+    time_t ct_time;
+    char time_buffer[80];    
+        
+    // Initialize LCD
+    uLCD.baudrate(115200);
+    uLCD.background_color(BLACK);
+    uLCD.cls();
+    
+    // Connect to network and wait for DHCP
+    uLCD.locate(0,0); 
+    uLCD.printf("Getting IP Address\n");
+    eth.init();
+    if ( eth.connect() == -1 ) {
+        uLCD.printf("ERROR: Could not\nget IP address");
+        return -1;
+    }
+    uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress());
+    wait(1);
+    
+    // Read time from server
+    uLCD.printf("Reading time...\n\r");
+    ntp_client.setTime(domain_name, port_number);
+    uLCD.printf("Time set\n");
+    wait(2);
+    eth.disconnect();
+    
+    // Reset LCD
+    uLCD.background_color(WHITE);
+    uLCD.textbackground_color(WHITE);
+    uLCD.color(RED);
+    uLCD.cls();
+    uLCD.text_height(2);
+    
+    // Loop and update clock
+    while (1) {
+        uLCD.locate(0, 1);
+        ct_time = time(NULL);
+        strftime(time_buffer, 80, "    %a %b %d\n    %T %p %z\n    %Z\n", \
+                                                localtime(&ct_time));
+        uLCD.printf("    UTC/GMT:\n%s", time_buffer);
+        wait(0.1);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_blinky/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+
+int main() {
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pwm_song/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,67 @@
+// Plays a familiar melody using PWM to the headphones. To find the frequencies
+// of notes, see http://en.wikipedia.org/wiki/Piano_key_frequencies
+// Based on the "speaker_demo_PWM" program by Jim Hamblen
+
+#include "mbed.h"
+
+#define VOLUME 0.01
+#define BPM 100.0
+
+PwmOut pwm_pin(p21);
+
+// Plays a sound with the defined frequency, duration, and volume
+void playNote(float frequency, float duration, float volume) {
+    pwm_pin.period(1.0/frequency);
+    pwm_pin = volume/2.0;
+    wait(duration);
+    pwm_pin = 0.0;
+}
+
+int main()
+{
+    float beat_duration;
+
+    // Calculate duration of a quarter note from bpm
+    beat_duration = 60.0 / BPM;
+    
+    // Loop forever
+    while(1) {
+        
+        // First measure
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(311.127, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        
+        // Second measure
+        playNote(391.995, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(311.127, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME);
+        wait(0.1);
+        
+        // Third measure
+        playNote(587.330, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(587.330, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(587.330, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(622.254, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        
+        // Fourth measure
+        playNote(369.994, (beat_duration - 0.1), VOLUME);
+        wait(0.1);
+        playNote(311.127, (0.75 * beat_duration), VOLUME);
+        playNote(466.164, (0.25 * beat_duration), VOLUME);
+        playNote(391.995, ((2 * beat_duration) - 0.1), VOLUME);
+        wait(0.1);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rgb_buttons/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+
+// Define buttons
+InterruptIn button_red(p5);
+InterruptIn button_green(p6);
+InterruptIn button_blue(p7);
+
+// Define LED colors
+PwmOut led_red(p21);
+PwmOut led_green(p22);
+PwmOut led_blue(p23);
+
+// Interrupt Service Routine to increment the red color
+void inc_red() {
+    
+    float pwm;
+    
+    // Read in current PWM value and increment it
+    pwm = led_red.read();
+    pwm += 0.1f;
+    if (pwm > 1.0f) {
+        pwm = 0.0f;
+    }
+    led_red.write(pwm);
+}
+
+// Interrupt Service Routine to increment the green color
+void inc_green() {
+    
+    float pwm;
+    
+    // Read in current PWM value and increment it
+    pwm = led_green.read();
+    pwm += 0.1f;
+    if (pwm > 1.0f) {
+        pwm = 0.0f;
+    }
+    led_green.write(pwm);
+}
+
+// Interrupt Service Routine to increment the blue color
+void inc_blue() {
+    
+    float pwm;
+    
+    // Read in current PWM value and increment it
+    pwm = led_blue.read();
+    pwm += 0.1f;
+    if (pwm > 1.0f) {
+        pwm = 0.0f;
+    }
+    led_blue.write(pwm);
+}
+
+// Main loop
+int main() {
+    
+    // Initialize all LED colors as off
+    led_red.write(0.0f);
+    led_green.write(0.0f);
+    led_blue.write(0.0f);
+    
+    // Define three interrupts - one for each color
+    button_red.fall(&inc_red);
+    button_green.fall(&inc_green);
+    button_blue.fall(&inc_blue);
+    
+    // Do nothing! We wait for an interrupt to happen
+    while(1) {
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/soundboard/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,82 @@
+// Soundboard that plays 1 of 4 .wav files stored on the SD card based on 1 of
+// 4 buttons pressed
+
+#include "mbed.h"
+#include "wave_player.h"
+#include "SDFileSystem.h"
+
+// .wav files to play
+char *filenames[4] = {  "/sd/good_morning.wav", \
+                        "/sd/questions.wav", \
+                        "/sd/lack_discipline.wav", \
+                        "/sd/stop_whining.wav"};
+
+// Define buttons
+DigitalIn button_1(p11);
+DigitalIn button_2(p12);
+DigitalIn button_3(p13);
+DigitalIn button_4(p14);
+
+// USB serial (tx, rx)
+Serial pc(USBTX, USBRX);
+
+// SD card
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+
+// Audio out (DAC)
+AnalogOut       DACout(p18);
+wave_player     waver(&DACout);
+
+// Play a .wav file
+int playSound(int file_num) {
+    
+    FILE *file;
+    
+    // Open sound file for reading
+    file = fopen(filenames[file_num], "r");
+    if ( file == NULL ) {
+        error("ERROR: Could not open file for reading!\n");
+        return -1;
+    }
+    
+    // Play the sound file
+    pc.printf("Playing sound clip %i\r\n", (file_num + 1));
+    waver.play(file);
+    
+    // Reset to beginning of file and close it
+    fseek(file, 0, SEEK_SET);
+    fclose(file);
+    
+    return 0;
+}
+
+int main() {
+
+    // Use internal pull-up resistors
+    button_1.mode(PullUp);
+    button_2.mode(PullUp);
+    button_3.mode(PullUp);
+    button_4.mode(PullUp);
+    
+    pc.printf("\r\nHardware Soundboard\r\n");
+    
+    while(1) {
+        
+        // Figure out which button was pressed and play that file
+        if ( button_1 == 0 ) {
+            playSound(0);
+        }
+        if ( button_2 == 0 ) {
+            playSound(1);
+        }
+        if ( button_3 == 0 ) {
+            playSound(2);
+        }
+        if ( button_4 == 0 ) {
+            playSound(3);
+        }
+        
+        // Wait 10ms before sampling the buttons again
+        wait(0.01);
+    }
+} 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/temp_logging/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,72 @@
+// Temperature logging demo - record temperatures to SD card and print them to
+// the console every 10 seconds
+
+#include "mbed.h"
+#include "SDFileSystem.h"
+
+// Analog input (pin 15)
+AnalogIn ain(p15);
+
+// USB serial (tx, rx)
+Serial pc(USBTX, USBRX);
+
+// SD card (SPI pins)
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+
+// Timer for our timestamps
+Timer timer;
+
+int main() {
+    
+    FILE *file;
+    float voltage_in;
+    float degrees_c;
+    int i;
+    int c;
+    
+    // Start our timer
+    timer.start();
+        
+    // Open file for writing
+    file = fopen("/sd/temp_data.txt", "w");
+    if ( file == NULL ) {
+        error("ERROR: Could not open file for writing!\n");
+        return -1;
+    }
+    
+    // Tell the user we need to wait while we collect some data
+    pc.printf("\nCollecting data (Do not remove SD Card!) ...\n");
+    
+    // Collect temperatures with timestamps every second
+    for(i = 0; i < 10; i++) {
+        voltage_in = ain * 3.3;
+        degrees_c = (voltage_in - 0.5) * 100.0;
+        fprintf(file, "%2.2fs: %3.1f deg C\n", timer.read(), degrees_c);
+        wait(1);
+    }
+    
+    // Close file and re-open it for reading
+    fclose(file);
+    file = fopen("/sd/temp_data.txt", "r");
+    if ( file == NULL ) {
+        error("ERROR: Could not open file for reading!\n");
+        return -1;
+    }
+    
+    // Print results to console
+    printf("Temperature data:\n");
+    while(1) {
+       c = fgetc(file);
+       if ( c == EOF ) {
+           break;
+       }
+       pc.putc(c);
+    }
+    
+    // Close the file and finish
+    fclose(file);
+    pc.printf("Done!\n");
+    
+    return 0;
+}
+ 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ulcd_accel/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,57 @@
+// Demo for the uLCD-144-G2 and MMA8452Q 3-axis accelerometer
+
+#include "mbed.h"
+#include "MMA8452Q.h"
+#include "uLCD_4DGL.h"
+
+// Graphic LCD - TX, RX, and RES pins
+uLCD_4DGL uLCD(p9,p10,p11);
+
+// Accelerometer - SDA, SCL, and I2C address
+MMA8452Q accel(p28, p27, 0x1D);
+
+int main() {
+
+    // Initialize uLCD
+    uLCD.baudrate(115200);
+    uLCD.background_color(BLACK);
+    uLCD.cls();
+
+    // Initialize accelerometer
+    accel.init();
+
+    // Initial parameters for the circle
+    float x = 64;
+    float y = 64;
+    int radius = 4;
+    int speed = 4;
+
+    // Make a ball "fall" in direction of accelerometer
+    while (1) {
+
+        // Draw a red circle
+        uLCD.filled_circle((int)x, (int)y, radius, RED);
+
+        // Wait before erasing old circle
+        wait(0.02);         // In seconds
+
+        // Erase old circle
+        uLCD.filled_circle((int)x, (int)y, radius, BLACK);
+
+        // Move circle. IMPORTANT! Notice how we adjust for sensor orientation!
+        x -= (speed * accel.readY());
+        y -= (speed * accel.readX());
+
+        // Make circle sit on edges
+        if ( x <= radius + 1 ) {
+            x = radius + 1;
+        } else if ( x >= 126 - radius ) {
+            x = 126 - radius;
+        }
+        if ( y <= radius + 1 ) {
+            y = radius + 1;
+        } else if ( y >= 126 - radius ) {
+            y = 126 - radius;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ulcd_demo/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,69 @@
+// Demo for the uLCD-144-G2 based on the work by Jim Hamblen
+
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+ 
+// TX, RX, and RES pins
+uLCD_4DGL uLCD(p9,p10,p11);
+ 
+int main() {
+    
+    int x;
+    int y;
+    int radius;
+    int vx;
+    
+    // Set our UART baudrate to something reasonable
+    uLCD.baudrate(115200);
+    
+    // Change background color (must be called before cls)
+    uLCD.background_color(WHITE);
+    
+    // Clear screen with background color
+    uLCD.cls();
+    
+    // Change background color of text
+    uLCD.textbackground_color(WHITE);
+    
+    // Make some colorful text
+    uLCD.locate(4, 1);      // Move cursor
+    uLCD.color(BLUE);
+    uLCD.printf("This is a\n");
+    uLCD.locate(5, 3);      // Move cursor
+    uLCD.text_width(2);     // 2x normal size
+    uLCD.text_height(2);    // 2x normal size
+    uLCD.color(RED);        // Change text color
+    uLCD.printf("TEST");
+    uLCD.text_width(1);     // Normal size
+    uLCD.text_height(1);    // Normal size
+    uLCD.locate(3, 6);      // Move cursor   
+    uLCD.color(BLACK);      // Change text color
+    uLCD.printf("of my new LCD");
+    
+    // Initial parameters for the circle
+    x = 50;
+    y = 100;
+    radius = 4;
+    vx = 1;
+    
+    // Make a ball bounce back and forth
+    while (1) {
+        
+        // Draw a dark green
+        uLCD.filled_circle(x, y, radius, 0x008000);
+        
+        // Bounce off the edges
+        if ((x <= radius + 1) || (x >= 126 - radius)) {
+            vx = -1 * vx;
+        }
+        
+        // Wait before erasing old circle
+        wait(0.02);         // In seconds
+        
+        // Erase old circle
+        uLCD.filled_circle(x, y, radius, WHITE);
+        
+        // Move circle
+        x = x + vx;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usb_device/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,42 @@
+// USB Device demo - control mouse pointer with buttons
+
+#include "mbed.h"
+#include "USBMouse.h"
+
+// USB Mouse object
+USBMouse mouse;
+
+// Define buttons
+DigitalIn button_up(p5);
+DigitalIn button_down(p6);
+DigitalIn button_left(p7);
+DigitalIn button_right(p8);
+
+DigitalOut myled(LED1);
+
+int main() {
+    int x = 0;
+    int y = 0;
+
+
+    while (1) {
+        
+        // Determine mouse pointer horizontal direction
+        x = button_left ^ button_right;
+        if ( button_right ) {
+            x = -1 * x;
+        }
+        
+        // Determine mouse pointer vertical direction
+        y = button_up ^ button_down;
+        if ( button_down ) {
+            y = -1 * y;
+        }
+        
+        // Move mouse
+        mouse.move(x, y);
+        
+        // Wait for next cycle
+        wait(0.001);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usb_host/main.cpp	Mon Jul 28 20:29:28 2014 +0000
@@ -0,0 +1,60 @@
+// USB host keyboard and LCD demo
+
+#include "mbed.h"
+#include "USBHostKeyboard.h"
+#include "uLCD_4DGL.h"
+
+// LED to demonstrate multi-threading
+DigitalOut led(LED1);
+
+// Graphic LCD - TX, RX, and RES pins
+uLCD_4DGL uLCD(p9,p10,p11);
+
+// Callback function from thread
+void onKey(uint8_t key) {
+    uLCD.printf("%c", key);
+}
+
+// Function that runs continuously in the thread
+void keyboard_task(void const *) {
+    
+    USBHostKeyboard keyboard;
+    
+    while(1) {
+        
+        // Try to connect a USB keyboard
+        uLCD.printf("Waiting...\n");
+        while(!keyboard.connect()) {
+            Thread::wait(500);
+        }
+        uLCD.printf("Connected!\n");
+    
+        // When connected, attach handler called on keyboard event
+        keyboard.attach(onKey);
+        
+        // Wait until the keyboard is disconnected
+        while(keyboard.connected()) {
+            Thread::wait(500);
+        }
+        uLCD.printf("\nDisconnected!\n");
+    }
+}
+
+// Main - the program enters here
+int main() {
+    
+    // Initialize LCD
+    uLCD.baudrate(115200);
+    uLCD.background_color(BLACK);
+    uLCD.cls();
+    uLCD.locate(0,0); 
+    
+    // Create a thread that runs a function (keyboard_task)
+    Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
+    
+    // Flash an LED forever
+    while(1) {
+        led=!led;
+        Thread::wait(500);
+    }
+}