Twittering Billy Bass plays back samples and looks out for and reads twitters!

Dependencies:   NetServices mbed

Revision:
1:27b1efbf5a46
Parent:
0:ad2574c88043
--- a/main.cpp	Wed Apr 06 15:16:45 2011 +0000
+++ b/main.cpp	Sat Apr 09 08:35:20 2011 +0000
@@ -1,3 +1,7 @@
+// Twittering Billy Bass demo example
+// Hack billy to read out samples and fetch twitters
+// Based on Steve Ravet's original Billy Bass hack
+
 #include "mbed.h"
 
 #include "MSCFileSystem.h"
@@ -5,9 +9,9 @@
 #include "EthernetNetIf.h"
 #include "HTTPClient.h"
 
+// Network and USB filesystem
 EthernetNetIf eth;
 HTTPClient http;
-
 MSCFileSystem usb("usb");
 
 DigitalOut setup_led(LED1);
@@ -15,6 +19,7 @@
 DigitalOut downloading_led(LED3);
 DigitalOut button_led(LED4);
 
+// Simple HTTP helper functions
 int get_file(char *url, char *file) {
     printf("Getting url to file [%s]...\n", url);
     HTTPFile f(file);
@@ -29,7 +34,7 @@
 int get_string(char *url, char *str) {
     printf("Getting url [%s] to string...\n", url);
     HTTPText t;
-    HTTPResult r = http.get("http://danros.org.uk/billy/billy.py", &t);
+    HTTPResult r = http.get(url, &t);
     if (r != HTTP_OK) {
         printf("HTTPResult error %d\n", r);
         str[0] = 0;
@@ -39,22 +44,26 @@
     return 1;
 }
 
+// a url to poll that returns the id of newest message
 int billy_get_id(char *id) {
-    return get_string("http://danros.org.uk/billy/billy.py", id);
+    return get_string("http://example.com/billy/billy.py", id);
 }
 
+// downloads a wav file with specific id to the usb disk
 int billy_get_wav(char *id) {
     char url[128];
-    sprintf(url, "http://danros.org.uk/billydata/%s.wav", id);
+    sprintf(url, "http://example.com/billydata/%s.wav", id);
     return get_file(url, "/usb/download.wav");
 }
 
+// downloads a movement file with specific id to the usb disk
 int billy_get_mov(char *id) {
     char url[128];
-    sprintf(url, "http://danros.org.uk/billydata/%s.txt", id);
+    sprintf(url, "http://example.com/billydata/%s.txt", id);
     return get_file(url, "/usb/download.txt");
 }
 
+// see if there is a new message, and download it if so
 int billy_get_new_message() {
     static char last[64] = {0};
     char id[64];
@@ -71,10 +80,12 @@
     return 0;
 }
 
+// pre-programmed songs on the USB disk
 const char *songs[] = {"baddonut", "belcher", "people", "clinton", "coconut", "bushfool", "ecky", "ni", "looney", "lumber"};
 int nsongs = 10;
 
-int billy_play_next() {
+// play the next pre-programmed song in the list
+void billy_play_next() {
     static int n = 0;
     char wavname[64];
     char cmdname[64];
@@ -84,39 +95,42 @@
     billy_play(wavname, cmdname);
 }
 
-// Capture button presses
+// capture button presses
 InterruptIn button(p24);
 void button_press() {
-    if(button) {
+    if(button) {    // minor debounce
         button_led = 1;
     }
 }
 
+// main billy loop, looking for tweets and button presses
 int main() {
     printf("Twittering billy...\n");
 
     printf("Setup network...\n");
     setup_led = 1;
     EthernetErr ethErr = eth.setup();
-    if (ethErr) {
-        error("Error %d in setup\n", ethErr);
-    }
-    printf("Network setup OK!\n");
-    setup_led = 0;
+    if(ethErr) {
+        printf("Error %d in setup\n", ethErr);
+        setup_led = 0;
+    } 
 
-    button.rise(&button_press);
+    button.rise(&button_press); // look out for button presses
 
     while (1) {
-        if(billy_get_new_message()) {
-            billy_play("/usb/download.wav", "/usb/download.txt");
+        waiting_led = !waiting_led;
+        
+        if(setup_led) {                                                 // if we have network
+            if(billy_get_new_message()) {                               // see if there is a new message
+                billy_play("/usb/download.wav", "/usb/download.txt");   // and play it if so
+            }
         }
-
-        if (button_led) {
-            billy_play_next();
+        
+        if(button_led) {        // if the button has been pressed
+            billy_play_next();  // play the next song
             button_led = 0;
         }
 
-        waiting_led = !waiting_led;
         wait(1);
     }
 }