![](/media/cache/profiles/c51ce410c124a10e0db5e4b97fc2af39.50x50_q85.jpg)
Twittering Billy Bass plays back samples and looks out for and reads twitters!
Dependencies: NetServices mbed
Diff: main.cpp
- Revision:
- 0:ad2574c88043
- Child:
- 1:27b1efbf5a46
diff -r 000000000000 -r ad2574c88043 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 06 15:16:45 2011 +0000 @@ -0,0 +1,123 @@ +#include "mbed.h" + +#include "MSCFileSystem.h" +#include "billy.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" + +EthernetNetIf eth; +HTTPClient http; + +MSCFileSystem usb("usb"); + +DigitalOut setup_led(LED1); +DigitalOut waiting_led(LED2); +DigitalOut downloading_led(LED3); +DigitalOut button_led(LED4); + +int get_file(char *url, char *file) { + printf("Getting url to file [%s]...\n", url); + HTTPFile f(file); + HTTPResult r = http.get(url, &f); + if (r != HTTP_OK) { + printf("HTTPResult error %d\n", r); + return 0; + } + return 1; +} + +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); + if (r != HTTP_OK) { + printf("HTTPResult error %d\n", r); + str[0] = 0; + return 0; + } + strcpy(str, t.gets()); + return 1; +} + +int billy_get_id(char *id) { + return get_string("http://danros.org.uk/billy/billy.py", id); +} + +int billy_get_wav(char *id) { + char url[128]; + sprintf(url, "http://danros.org.uk/billydata/%s.wav", id); + return get_file(url, "/usb/download.wav"); +} + +int billy_get_mov(char *id) { + char url[128]; + sprintf(url, "http://danros.org.uk/billydata/%s.txt", id); + return get_file(url, "/usb/download.txt"); +} + +int billy_get_new_message() { + static char last[64] = {0}; + char id[64]; + billy_get_id(id); + if (strcmp(last, id) != 0) { // new message + printf("New message found...\n"); + downloading_led = 1; + billy_get_wav(id); + billy_get_mov(id); + strcpy(last, id); + downloading_led = 0; + return 1; + } + return 0; +} + +const char *songs[] = {"baddonut", "belcher", "people", "clinton", "coconut", "bushfool", "ecky", "ni", "looney", "lumber"}; +int nsongs = 10; + +int billy_play_next() { + static int n = 0; + char wavname[64]; + char cmdname[64]; + sprintf(wavname, "/usb/%s.wav", songs[n]); + sprintf(cmdname, "/usb/%s.txt", songs[n]); + n = (n + 1) % nsongs; + billy_play(wavname, cmdname); +} + +// Capture button presses +InterruptIn button(p24); +void button_press() { + if(button) { + button_led = 1; + } +} + +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; + + button.rise(&button_press); + + while (1) { + if(billy_get_new_message()) { + billy_play("/usb/download.wav", "/usb/download.txt"); + } + + if (button_led) { + billy_play_next(); + button_led = 0; + } + + waiting_led = !waiting_led; + wait(1); + } +} +