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

Dependencies:   NetServices mbed

Committer:
simon
Date:
Wed Apr 06 15:16:45 2011 +0000
Revision:
0:ad2574c88043
Child:
1:27b1efbf5a46

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:ad2574c88043 1 #include "mbed.h"
simon 0:ad2574c88043 2
simon 0:ad2574c88043 3 #include "MSCFileSystem.h"
simon 0:ad2574c88043 4 #include "billy.h"
simon 0:ad2574c88043 5 #include "EthernetNetIf.h"
simon 0:ad2574c88043 6 #include "HTTPClient.h"
simon 0:ad2574c88043 7
simon 0:ad2574c88043 8 EthernetNetIf eth;
simon 0:ad2574c88043 9 HTTPClient http;
simon 0:ad2574c88043 10
simon 0:ad2574c88043 11 MSCFileSystem usb("usb");
simon 0:ad2574c88043 12
simon 0:ad2574c88043 13 DigitalOut setup_led(LED1);
simon 0:ad2574c88043 14 DigitalOut waiting_led(LED2);
simon 0:ad2574c88043 15 DigitalOut downloading_led(LED3);
simon 0:ad2574c88043 16 DigitalOut button_led(LED4);
simon 0:ad2574c88043 17
simon 0:ad2574c88043 18 int get_file(char *url, char *file) {
simon 0:ad2574c88043 19 printf("Getting url to file [%s]...\n", url);
simon 0:ad2574c88043 20 HTTPFile f(file);
simon 0:ad2574c88043 21 HTTPResult r = http.get(url, &f);
simon 0:ad2574c88043 22 if (r != HTTP_OK) {
simon 0:ad2574c88043 23 printf("HTTPResult error %d\n", r);
simon 0:ad2574c88043 24 return 0;
simon 0:ad2574c88043 25 }
simon 0:ad2574c88043 26 return 1;
simon 0:ad2574c88043 27 }
simon 0:ad2574c88043 28
simon 0:ad2574c88043 29 int get_string(char *url, char *str) {
simon 0:ad2574c88043 30 printf("Getting url [%s] to string...\n", url);
simon 0:ad2574c88043 31 HTTPText t;
simon 0:ad2574c88043 32 HTTPResult r = http.get("http://danros.org.uk/billy/billy.py", &t);
simon 0:ad2574c88043 33 if (r != HTTP_OK) {
simon 0:ad2574c88043 34 printf("HTTPResult error %d\n", r);
simon 0:ad2574c88043 35 str[0] = 0;
simon 0:ad2574c88043 36 return 0;
simon 0:ad2574c88043 37 }
simon 0:ad2574c88043 38 strcpy(str, t.gets());
simon 0:ad2574c88043 39 return 1;
simon 0:ad2574c88043 40 }
simon 0:ad2574c88043 41
simon 0:ad2574c88043 42 int billy_get_id(char *id) {
simon 0:ad2574c88043 43 return get_string("http://danros.org.uk/billy/billy.py", id);
simon 0:ad2574c88043 44 }
simon 0:ad2574c88043 45
simon 0:ad2574c88043 46 int billy_get_wav(char *id) {
simon 0:ad2574c88043 47 char url[128];
simon 0:ad2574c88043 48 sprintf(url, "http://danros.org.uk/billydata/%s.wav", id);
simon 0:ad2574c88043 49 return get_file(url, "/usb/download.wav");
simon 0:ad2574c88043 50 }
simon 0:ad2574c88043 51
simon 0:ad2574c88043 52 int billy_get_mov(char *id) {
simon 0:ad2574c88043 53 char url[128];
simon 0:ad2574c88043 54 sprintf(url, "http://danros.org.uk/billydata/%s.txt", id);
simon 0:ad2574c88043 55 return get_file(url, "/usb/download.txt");
simon 0:ad2574c88043 56 }
simon 0:ad2574c88043 57
simon 0:ad2574c88043 58 int billy_get_new_message() {
simon 0:ad2574c88043 59 static char last[64] = {0};
simon 0:ad2574c88043 60 char id[64];
simon 0:ad2574c88043 61 billy_get_id(id);
simon 0:ad2574c88043 62 if (strcmp(last, id) != 0) { // new message
simon 0:ad2574c88043 63 printf("New message found...\n");
simon 0:ad2574c88043 64 downloading_led = 1;
simon 0:ad2574c88043 65 billy_get_wav(id);
simon 0:ad2574c88043 66 billy_get_mov(id);
simon 0:ad2574c88043 67 strcpy(last, id);
simon 0:ad2574c88043 68 downloading_led = 0;
simon 0:ad2574c88043 69 return 1;
simon 0:ad2574c88043 70 }
simon 0:ad2574c88043 71 return 0;
simon 0:ad2574c88043 72 }
simon 0:ad2574c88043 73
simon 0:ad2574c88043 74 const char *songs[] = {"baddonut", "belcher", "people", "clinton", "coconut", "bushfool", "ecky", "ni", "looney", "lumber"};
simon 0:ad2574c88043 75 int nsongs = 10;
simon 0:ad2574c88043 76
simon 0:ad2574c88043 77 int billy_play_next() {
simon 0:ad2574c88043 78 static int n = 0;
simon 0:ad2574c88043 79 char wavname[64];
simon 0:ad2574c88043 80 char cmdname[64];
simon 0:ad2574c88043 81 sprintf(wavname, "/usb/%s.wav", songs[n]);
simon 0:ad2574c88043 82 sprintf(cmdname, "/usb/%s.txt", songs[n]);
simon 0:ad2574c88043 83 n = (n + 1) % nsongs;
simon 0:ad2574c88043 84 billy_play(wavname, cmdname);
simon 0:ad2574c88043 85 }
simon 0:ad2574c88043 86
simon 0:ad2574c88043 87 // Capture button presses
simon 0:ad2574c88043 88 InterruptIn button(p24);
simon 0:ad2574c88043 89 void button_press() {
simon 0:ad2574c88043 90 if(button) {
simon 0:ad2574c88043 91 button_led = 1;
simon 0:ad2574c88043 92 }
simon 0:ad2574c88043 93 }
simon 0:ad2574c88043 94
simon 0:ad2574c88043 95 int main() {
simon 0:ad2574c88043 96 printf("Twittering billy...\n");
simon 0:ad2574c88043 97
simon 0:ad2574c88043 98 printf("Setup network...\n");
simon 0:ad2574c88043 99 setup_led = 1;
simon 0:ad2574c88043 100 EthernetErr ethErr = eth.setup();
simon 0:ad2574c88043 101 if (ethErr) {
simon 0:ad2574c88043 102 error("Error %d in setup\n", ethErr);
simon 0:ad2574c88043 103 }
simon 0:ad2574c88043 104 printf("Network setup OK!\n");
simon 0:ad2574c88043 105 setup_led = 0;
simon 0:ad2574c88043 106
simon 0:ad2574c88043 107 button.rise(&button_press);
simon 0:ad2574c88043 108
simon 0:ad2574c88043 109 while (1) {
simon 0:ad2574c88043 110 if(billy_get_new_message()) {
simon 0:ad2574c88043 111 billy_play("/usb/download.wav", "/usb/download.txt");
simon 0:ad2574c88043 112 }
simon 0:ad2574c88043 113
simon 0:ad2574c88043 114 if (button_led) {
simon 0:ad2574c88043 115 billy_play_next();
simon 0:ad2574c88043 116 button_led = 0;
simon 0:ad2574c88043 117 }
simon 0:ad2574c88043 118
simon 0:ad2574c88043 119 waiting_led = !waiting_led;
simon 0:ad2574c88043 120 wait(1);
simon 0:ad2574c88043 121 }
simon 0:ad2574c88043 122 }
simon 0:ad2574c88043 123