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

Dependencies:   NetServices mbed

Committer:
simon
Date:
Sat Apr 09 08:35:20 2011 +0000
Revision:
1:27b1efbf5a46
Parent:
0:ad2574c88043

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:27b1efbf5a46 1 // Twittering Billy Bass demo example
simon 1:27b1efbf5a46 2 // Hack billy to read out samples and fetch twitters
simon 1:27b1efbf5a46 3 // Based on Steve Ravet's original Billy Bass hack
simon 1:27b1efbf5a46 4
simon 0:ad2574c88043 5 #include "mbed.h"
simon 0:ad2574c88043 6
simon 0:ad2574c88043 7 #include "MSCFileSystem.h"
simon 0:ad2574c88043 8 #include "billy.h"
simon 0:ad2574c88043 9 #include "EthernetNetIf.h"
simon 0:ad2574c88043 10 #include "HTTPClient.h"
simon 0:ad2574c88043 11
simon 1:27b1efbf5a46 12 // Network and USB filesystem
simon 0:ad2574c88043 13 EthernetNetIf eth;
simon 0:ad2574c88043 14 HTTPClient http;
simon 0:ad2574c88043 15 MSCFileSystem usb("usb");
simon 0:ad2574c88043 16
simon 0:ad2574c88043 17 DigitalOut setup_led(LED1);
simon 0:ad2574c88043 18 DigitalOut waiting_led(LED2);
simon 0:ad2574c88043 19 DigitalOut downloading_led(LED3);
simon 0:ad2574c88043 20 DigitalOut button_led(LED4);
simon 0:ad2574c88043 21
simon 1:27b1efbf5a46 22 // Simple HTTP helper functions
simon 0:ad2574c88043 23 int get_file(char *url, char *file) {
simon 0:ad2574c88043 24 printf("Getting url to file [%s]...\n", url);
simon 0:ad2574c88043 25 HTTPFile f(file);
simon 0:ad2574c88043 26 HTTPResult r = http.get(url, &f);
simon 0:ad2574c88043 27 if (r != HTTP_OK) {
simon 0:ad2574c88043 28 printf("HTTPResult error %d\n", r);
simon 0:ad2574c88043 29 return 0;
simon 0:ad2574c88043 30 }
simon 0:ad2574c88043 31 return 1;
simon 0:ad2574c88043 32 }
simon 0:ad2574c88043 33
simon 0:ad2574c88043 34 int get_string(char *url, char *str) {
simon 0:ad2574c88043 35 printf("Getting url [%s] to string...\n", url);
simon 0:ad2574c88043 36 HTTPText t;
simon 1:27b1efbf5a46 37 HTTPResult r = http.get(url, &t);
simon 0:ad2574c88043 38 if (r != HTTP_OK) {
simon 0:ad2574c88043 39 printf("HTTPResult error %d\n", r);
simon 0:ad2574c88043 40 str[0] = 0;
simon 0:ad2574c88043 41 return 0;
simon 0:ad2574c88043 42 }
simon 0:ad2574c88043 43 strcpy(str, t.gets());
simon 0:ad2574c88043 44 return 1;
simon 0:ad2574c88043 45 }
simon 0:ad2574c88043 46
simon 1:27b1efbf5a46 47 // a url to poll that returns the id of newest message
simon 0:ad2574c88043 48 int billy_get_id(char *id) {
simon 1:27b1efbf5a46 49 return get_string("http://example.com/billy/billy.py", id);
simon 0:ad2574c88043 50 }
simon 0:ad2574c88043 51
simon 1:27b1efbf5a46 52 // downloads a wav file with specific id to the usb disk
simon 0:ad2574c88043 53 int billy_get_wav(char *id) {
simon 0:ad2574c88043 54 char url[128];
simon 1:27b1efbf5a46 55 sprintf(url, "http://example.com/billydata/%s.wav", id);
simon 0:ad2574c88043 56 return get_file(url, "/usb/download.wav");
simon 0:ad2574c88043 57 }
simon 0:ad2574c88043 58
simon 1:27b1efbf5a46 59 // downloads a movement file with specific id to the usb disk
simon 0:ad2574c88043 60 int billy_get_mov(char *id) {
simon 0:ad2574c88043 61 char url[128];
simon 1:27b1efbf5a46 62 sprintf(url, "http://example.com/billydata/%s.txt", id);
simon 0:ad2574c88043 63 return get_file(url, "/usb/download.txt");
simon 0:ad2574c88043 64 }
simon 0:ad2574c88043 65
simon 1:27b1efbf5a46 66 // see if there is a new message, and download it if so
simon 0:ad2574c88043 67 int billy_get_new_message() {
simon 0:ad2574c88043 68 static char last[64] = {0};
simon 0:ad2574c88043 69 char id[64];
simon 0:ad2574c88043 70 billy_get_id(id);
simon 0:ad2574c88043 71 if (strcmp(last, id) != 0) { // new message
simon 0:ad2574c88043 72 printf("New message found...\n");
simon 0:ad2574c88043 73 downloading_led = 1;
simon 0:ad2574c88043 74 billy_get_wav(id);
simon 0:ad2574c88043 75 billy_get_mov(id);
simon 0:ad2574c88043 76 strcpy(last, id);
simon 0:ad2574c88043 77 downloading_led = 0;
simon 0:ad2574c88043 78 return 1;
simon 0:ad2574c88043 79 }
simon 0:ad2574c88043 80 return 0;
simon 0:ad2574c88043 81 }
simon 0:ad2574c88043 82
simon 1:27b1efbf5a46 83 // pre-programmed songs on the USB disk
simon 0:ad2574c88043 84 const char *songs[] = {"baddonut", "belcher", "people", "clinton", "coconut", "bushfool", "ecky", "ni", "looney", "lumber"};
simon 0:ad2574c88043 85 int nsongs = 10;
simon 0:ad2574c88043 86
simon 1:27b1efbf5a46 87 // play the next pre-programmed song in the list
simon 1:27b1efbf5a46 88 void billy_play_next() {
simon 0:ad2574c88043 89 static int n = 0;
simon 0:ad2574c88043 90 char wavname[64];
simon 0:ad2574c88043 91 char cmdname[64];
simon 0:ad2574c88043 92 sprintf(wavname, "/usb/%s.wav", songs[n]);
simon 0:ad2574c88043 93 sprintf(cmdname, "/usb/%s.txt", songs[n]);
simon 0:ad2574c88043 94 n = (n + 1) % nsongs;
simon 0:ad2574c88043 95 billy_play(wavname, cmdname);
simon 0:ad2574c88043 96 }
simon 0:ad2574c88043 97
simon 1:27b1efbf5a46 98 // capture button presses
simon 0:ad2574c88043 99 InterruptIn button(p24);
simon 0:ad2574c88043 100 void button_press() {
simon 1:27b1efbf5a46 101 if(button) { // minor debounce
simon 0:ad2574c88043 102 button_led = 1;
simon 0:ad2574c88043 103 }
simon 0:ad2574c88043 104 }
simon 0:ad2574c88043 105
simon 1:27b1efbf5a46 106 // main billy loop, looking for tweets and button presses
simon 0:ad2574c88043 107 int main() {
simon 0:ad2574c88043 108 printf("Twittering billy...\n");
simon 0:ad2574c88043 109
simon 0:ad2574c88043 110 printf("Setup network...\n");
simon 0:ad2574c88043 111 setup_led = 1;
simon 0:ad2574c88043 112 EthernetErr ethErr = eth.setup();
simon 1:27b1efbf5a46 113 if(ethErr) {
simon 1:27b1efbf5a46 114 printf("Error %d in setup\n", ethErr);
simon 1:27b1efbf5a46 115 setup_led = 0;
simon 1:27b1efbf5a46 116 }
simon 0:ad2574c88043 117
simon 1:27b1efbf5a46 118 button.rise(&button_press); // look out for button presses
simon 0:ad2574c88043 119
simon 0:ad2574c88043 120 while (1) {
simon 1:27b1efbf5a46 121 waiting_led = !waiting_led;
simon 1:27b1efbf5a46 122
simon 1:27b1efbf5a46 123 if(setup_led) { // if we have network
simon 1:27b1efbf5a46 124 if(billy_get_new_message()) { // see if there is a new message
simon 1:27b1efbf5a46 125 billy_play("/usb/download.wav", "/usb/download.txt"); // and play it if so
simon 1:27b1efbf5a46 126 }
simon 0:ad2574c88043 127 }
simon 1:27b1efbf5a46 128
simon 1:27b1efbf5a46 129 if(button_led) { // if the button has been pressed
simon 1:27b1efbf5a46 130 billy_play_next(); // play the next song
simon 0:ad2574c88043 131 button_led = 0;
simon 0:ad2574c88043 132 }
simon 0:ad2574c88043 133
simon 0:ad2574c88043 134 wait(1);
simon 0:ad2574c88043 135 }
simon 0:ad2574c88043 136 }
simon 0:ad2574c88043 137