Program that uses Adafruit FONA 800h and resistive touch uLCD display to track roommates and send and receive text messages.

Dependencies:   mbed-rtos mbed uLCD_4D_Picaso

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4D_Picaso.h"
00003 #include "Adafruit_FONA.h"
00004 #include "rtos.h"
00005 #include <string>
00006 #include <vector>
00007 #include <ctype.h>
00008 
00009 #define FONA_RST p12
00010 #define FONA_TX p13
00011 #define FONA_RX p14
00012 #define FONA_RI p11
00013 
00014 //using namespace std;
00015 Serial pcSerial(USBTX, USBRX);
00016 Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
00017 
00018 Mutex mut;
00019 uLCD_4D_Picaso lcd(p28, p27, p30);
00020 
00021 // this is a large buffer for replies
00022 char replybuffer[255];
00023 
00024 // Turn on a LED when somebody calls the FONA (we need to somehow make this apply to texts instead of call
00025 // Ok so this works!! just change where indicated
00026 DigitalOut led1(LED1);
00027 DigitalOut led2(LED2);
00028 volatile bool newText = false; 
00029 class FonaEventListener : public Adafruit_FONA::EventListener {
00030     virtual void onRing() { //<- Upon arrival of new text here is what happens
00031         //this is what should happen when a text comes in
00032         
00033         led1 = 1;
00034         wait_ms(500);
00035         led1 = 0;
00036         newText = true; //global 
00037     }
00038     
00039     virtual void onNoCarrier() {
00040         led1 = 0;
00041     }
00042 };
00043 FonaEventListener fonaEventListener;
00044 
00045 // Functions defined after main()
00046 uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
00047 void flushSerial();
00048 char readBlocking();
00049 uint16_t readnumber();
00050 long map(long x, long in_min, long in_max, long out_min, long out_max);
00051 
00052 // I don't think this global variable is defined correctly
00053 volatile bool whoshome[4] = {0}; // Cole = whoshome[0], brandon = whoshome[1], George = whoshome[2], Jesse = whoshome[3]
00054 
00055 // This member function needs to run when a text message has been received by the FONA module.
00056 // textout() reads the phone number of the person who sent the text message from text message slot 1,
00057 // deletes that text message, checks the boolean array that indicates who is home and then sends out a text
00058 // message for each person that is home.
00059 
00060 void textout();    
00061    
00062 // The ___home() member functions below should be called when the ____home buttons on the LCD touchscreen are pressed.
00063 // For example, when cole comes in the house he will push his button to indicate that he is home. This will update his
00064 // status from away (0) to home (1) and text everyone that is HOME the text message "Cole is home." The functionality is 
00065 // the same for brandonshome, georgeshome, jesseshome
00066 
00067 void coleshome();
00068 void brandonshome();
00069 void georgeshome();
00070 void jesseshome();
00071 
00072 // These functions should be called when one of the roommates pushes the ____away button on the lcd touchscreen.
00073 // For example, if Cole is leaving the house, then he would touch the COLE away box and it would change his value
00074 // in the boolean array whoshome from home (1) to away (0). Then, the next time the LCD DISPLAY thread is run,
00075 // Cole's name would disappear from the display because he is away.
00076 
00077 void colesaway(void);
00078 void brandonsaway(void);
00079 void georgesaway(void);
00080 void jessesaway(void);
00081 
00082 // This is the display function that updates the names based on the boolean array whoshome. Let me know if you
00083 // have questions about this.
00084 
00085 void update_display(){
00086     mut.lock();
00087     
00088     //print names - if home, print name, if not, print blank spaces 
00089     
00090     lcd.txt_MoveCursor(4, 0);
00091     lcd.txt_Attributes(Picaso::UNDERLINED);
00092     
00093     if (whoshome[0]) { //cole
00094         lcd.putStr("Cole\r\n");
00095         }
00096     else lcd.putStr("       \r\n");
00097     if (whoshome[1]) {  //Brandon
00098         lcd.putStr("Brandon\r\n");
00099         }
00100     else lcd.putStr("       \r\n");
00101     if (whoshome[2]) { //george
00102         lcd.putStr("George\r\n");
00103         }
00104     else lcd.putStr("       \r\n");
00105     if (whoshome[3]) { //Jesse
00106         lcd.putStr("Jesse\r\n");
00107         }
00108     else lcd.putStr("       \r\n");
00109     
00110     mut.unlock();
00111 }
00112 
00113 void display_func(void const *args){ //this is what is going to be responsible for updating to the screen
00114  
00115     // this may need to be in a separate thread, i'm not sure. we also may need to use a mutex
00116     mut.lock();
00117     pcSerial.printf("i'm in the display function\r\n");
00118     mut.unlock();
00119     int status = 0;
00120     int x = 0;
00121     int y = 0;
00122     while (1) {
00123         status = lcd.touch_Get(0);
00124         if (status) {
00125             mut.lock();
00126             pcSerial.printf("i got touched\r\n");
00127             mut.unlock();
00128             x = lcd.touch_Get(1);
00129             y = lcd.touch_Get(2);
00130             
00131                 if (status == 1 && y >= 180 && y <= 240 && x >= 0 && x <= 55) {
00132                     //update_display();
00133                     coleshome();
00134                 } 
00135                 if (status == 1 && y >= 180 && y <= 240 && x >= 65 && x <= 115) {
00136                     //update_display();
00137                     brandonshome();
00138                 } 
00139                 if (status == 1 && y >= 180 && y <= 240 && x >= 125 && x <= 175) {
00140                     //update_display();
00141                     georgeshome();
00142                 } 
00143                 if (status == 1 && y >= 180 && y <= 240 && x >= 185 && x <= 240) {
00144                     //update_display();
00145                     jesseshome();
00146                 } 
00147             
00148             // AWAY
00149             
00150                 if (status == 1 && y >= 250 && x >= 0 && x <= 55) {
00151                     //update_display();
00152                     colesaway();
00153                 } 
00154                 if (status == 1 && y >= 250 && x >= 65 && x <= 115) {
00155                     //update_display();
00156                     brandonsaway();
00157                 } 
00158                 if (status == 1 && y >= 250 && x >= 125 && x <= 175) {
00159                     //update_display();
00160                     georgesaway();
00161                 } 
00162                 if (status == 1 && y >= 250 && x >= 185 && x <= 240) {
00163                     jessesaway();
00164                     //update_display();
00165                 } 
00166         }
00167     }  
00168 } 
00169 
00170 void textout(void const *args){ // this is responsible for texting a list of people who are home 
00171     
00172     while(true) {
00173         while (!newText) Thread::yield(); //GEORGE!!!
00174         mut.lock();
00175         pcSerial.printf("i received a text message\r\n");
00176         mut.unlock();
00177         newText = false; //update global variable
00178         //read in txt message
00179         fona.getSMSSender(1, replybuffer, 250); // store phone number in reply buffer
00180         
00181         fona.deleteSMS(1); // delete text message to free up sms #1
00182         mut.lock();
00183         pcSerial.printf("text message deleted\r\n");
00184         mut.unlock();
00185         //reply to number with texts of who is home
00186         if (whoshome[0]) { //cole
00187             fona.sendSMS(replybuffer, "Cole is home");
00188             //wait_ms(250);
00189         }
00190         if (whoshome[1]) { //brandon
00191             fona.sendSMS(replybuffer, "Brandon is home");
00192             //wait_ms(250);
00193         }
00194         if (whoshome[2]) { //george
00195             fona.sendSMS(replybuffer, "George is home");
00196             //wait_ms(250);
00197         }
00198         if (whoshome[3]) { //jesse
00199             fona.sendSMS(replybuffer, "Jesse is home");
00200             //wait_ms(250);
00201         }
00202         if (whoshome[0] == 0 && whoshome[1] == 0 && whoshome[2] == 0 && whoshome[3] == 0) {
00203             fona.sendSMS(replybuffer, "No one is home :(");
00204             //wait_ms(250);
00205         }
00206     }
00207 }    
00208 
00209 void coleshome(){
00210     // update bool array
00211     whoshome[0] = 1;
00212     update_display();
00213     if (whoshome[1]) {
00214         fona.sendSMS("6789936332", "Cole is home."); //text brandon
00215         //wait_ms(250);
00216     }
00217     if (whoshome[2]) {
00218         fona.sendSMS("4045189567", "Cole is home."); //text george
00219         //wait_ms(250);
00220     }
00221     if (whoshome[3]) {
00222         fona.sendSMS("7067664360", "Cole is home."); //text jesse
00223         //wait_ms(250);
00224     }
00225 }
00226 
00227 void brandonshome(){
00228     // update bool array
00229     whoshome[1] = 1;
00230     update_display();
00231     if (whoshome[0]) {
00232         fona.sendSMS("8476022588", "Brandon is home."); //text cole
00233         //wait_ms(250);
00234     }
00235     if (whoshome[2]) {
00236         fona.sendSMS("4045189567", "Brandon is home."); //text george
00237         //wait_ms(250);
00238     }
00239     if (whoshome[3]) {
00240         fona.sendSMS("7067664360", "Brandon is home."); //text jesse
00241         //wait_ms(250);
00242     }
00243 }
00244 
00245 void georgeshome(){
00246     // update bool array
00247     whoshome[2] = 1;
00248     update_display();
00249     if (whoshome[0]) {
00250         fona.sendSMS("8476022588", "George is home."); //text cole
00251         //wait_ms(250);
00252     }
00253     if (whoshome[1]) {
00254         fona.sendSMS("6789936332", "George is home."); //text brandon
00255         //wait_ms(250);
00256     }
00257     if (whoshome[3]) {
00258         fona.sendSMS("7067664360", "George is home."); //text jesse
00259         //wait_ms(250);
00260     }
00261 }
00262 
00263 void jesseshome(){
00264     // update bool array
00265     whoshome[3] = 1;
00266     update_display();
00267     if (whoshome[0]) {
00268         fona.sendSMS("8476022588", "Jesse is home."); //text cole
00269         //wait_ms(250);
00270     }
00271     if (whoshome[1]) {
00272         fona.sendSMS("6789936332", "Jesse is home."); //text brandon
00273         //wait_ms(250);
00274     }
00275     if (whoshome[2]) {
00276         fona.sendSMS("4045189567", "Jesse is home."); //text george
00277         //wait_ms(250);
00278     }
00279 }
00280 
00281 void colesaway(){
00282     whoshome[0] = 0;
00283     update_display();
00284 }
00285 
00286 void brandonsaway(){
00287     whoshome[1] = 0;
00288     update_display();
00289 }
00290 
00291 void georgesaway(){
00292     whoshome[2] = 0;
00293     update_display();
00294 }
00295 
00296 void jessesaway(){
00297     whoshome[3] = 0;
00298     update_display();
00299 }
00300 
00301 
00302 void flushSerial() {
00303     while (pcSerial.readable()) 
00304         pcSerial.getc();
00305 }
00306 
00307 char readBlocking() {
00308     while (!pcSerial.readable());
00309     return pcSerial.getc();
00310 }
00311 
00312 uint16_t readnumber() {
00313     uint16_t x = 0;
00314     char c;
00315     while (! isdigit(c = readBlocking())) {
00316         //pcSerial.putc(c);
00317     }
00318     pcSerial.putc(c);
00319     x = c - '0';
00320     while (isdigit(c = readBlocking())) {
00321         pcSerial.putc(c);
00322         x *= 10;
00323         x += c - '0';
00324     }
00325     return x;
00326 }
00327   
00328 uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
00329     uint16_t buffidx = 0;
00330     bool timeoutvalid = true;
00331     if (timeout == 0) timeoutvalid = false;
00332     
00333     while (true) {
00334         if (buffidx > maxbuff) {
00335             //pcSerial.printf("SPACE\r\n");
00336             break;
00337         }
00338         
00339         while(pcSerial.readable()) {
00340             char c =  pcSerial.getc();
00341             
00342             //pcSerial.printf("%02x#%c\r\n", c, c);
00343             
00344             if (c == '\r') continue;
00345             if (c == 0xA) {
00346                 if (buffidx == 0)   // the first 0x0A is ignored
00347                     continue;
00348                 
00349                 timeout = 0;         // the second 0x0A is the end of the line
00350                 timeoutvalid = true;
00351                 break;
00352             }
00353             buff[buffidx] = c;
00354             buffidx++;
00355         }
00356         
00357         if (timeoutvalid && timeout == 0) {
00358             //pcSerial.printf("TIMEOUT\r\n");
00359             break;
00360         }
00361         wait_ms(1);
00362     }
00363     buff[buffidx] = 0;  // null term
00364     return buffidx;
00365 }
00366 
00367 long map(long x, long in_min, long in_max, long out_min, long out_max)
00368 {
00369     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00370 }
00371 
00372 void initialize_everything(){
00373         //initiate whatever
00374    
00375     // LCD touchscreen
00376     lcd.setbaudWait(Picaso::BAUD_600000);
00377     lcd.touch_Set(0);
00378     lcd.txt_Opacity(Picaso::OPAQUE);
00379     
00380     //Set up header
00381     
00382     lcd.txt_MoveCursor(0, 6);
00383     lcd.txt_Attributes(Picaso::BOLD);
00384     lcd.txt_FGcolour(Picaso::RED);
00385     lcd.putStr("mbed Roomate Tracker");
00386     lcd.txt_MoveCursor(2, 0);
00387     lcd.txt_FGcolour(Picaso::CYAN);
00388     lcd.putStr("Who's home?");
00389     
00390     //Set up touch boxes
00391     lcd.gfx_Rectangle(0, 180, 55, 240, Picaso::RED);
00392     lcd.gfx_RectangleFilled(1, 181, 54, 239, Picaso::WHITE);
00393     lcd.gfx_Rectangle(65, 180, 115, 240, Picaso::RED);
00394     lcd.gfx_RectangleFilled(66, 181, 114, 239, Picaso::WHITE);
00395     lcd.gfx_Rectangle(125, 180, 175, 240, Picaso::RED);
00396     lcd.gfx_RectangleFilled(126, 181, 174, 239, Picaso::WHITE);
00397     lcd.gfx_Rectangle(185, 180, 240, 240, Picaso::RED);
00398     lcd.gfx_RectangleFilled(186, 181, 239, 239, Picaso::WHITE);
00399     lcd.txt_MoveCursor(17, 2);
00400     lcd.putStr("COLE\r\nhome");
00401     lcd.txt_MoveCursor(17, 8);
00402     lcd.putStr("BRANDON\r\nhome");
00403     lcd.txt_MoveCursor(17, 16);
00404     lcd.putStr("GEORGE\r\nhome");
00405     lcd.txt_MoveCursor(17, 24);
00406     lcd.putStr("JESSE\r\nhome");
00407     
00408     lcd.gfx_Rectangle(0, 250, 55, 310, Picaso::RED);
00409     lcd.gfx_RectangleFilled(1, 251, 54, 309, Picaso::WHITE);
00410     lcd.gfx_Rectangle(65, 250, 115, 310, Picaso::RED);
00411     lcd.gfx_RectangleFilled(66, 251, 114, 309, Picaso::WHITE);
00412     lcd.gfx_Rectangle(125, 250, 175, 310, Picaso::RED);
00413     lcd.gfx_RectangleFilled(126, 251, 174, 309, Picaso::WHITE);
00414     lcd.gfx_Rectangle(185, 250, 240, 310, Picaso::RED);
00415     lcd.gfx_RectangleFilled(186, 251, 239, 309, Picaso::WHITE);
00416     lcd.txt_FGcolour(Picaso::WHITE);
00417     lcd.txt_Attributes(Picaso::UNDERLINED);
00418     lcd.txt_MoveCursor(23, 2);
00419     lcd.putStr("COLE\r\naway");
00420     lcd.txt_MoveCursor(23, 8);
00421     lcd.putStr("BRANDON\r\naway");
00422     lcd.txt_MoveCursor(23, 16);
00423     lcd.putStr("GEORGE\r\naway");
00424     lcd.txt_MoveCursor(23, 24);
00425     lcd.putStr("JESSE\r\naway");
00426     
00427     
00428     flushSerial();
00429     //FONA initialization (copied from FONA code (may only need the fona.begin(9600) but all of this stuff won't hurt to have
00430     pcSerial.baud(9600);
00431     wait(1);
00432     mut.lock();
00433     pcSerial.printf("\r\n");
00434     
00435     pcSerial.printf("FONA basic test\r\n");
00436     pcSerial.printf("Initializing....(May take 3 seconds)\r\n");
00437     wait(2);
00438     // See if the FONA is responding
00439     if (! fona.begin(9600)) {
00440         pcSerial.printf("Couldn't find FONA\r\n");
00441         while (1);
00442     }
00443     pcSerial.printf("Can I get an Amen\r\n");
00444     fona.setEventListener(&fonaEventListener);
00445     pcSerial.printf("FONA is OK\r\n");
00446     for( int i = 1; i < 10; i++){
00447         fona.deleteSMS(i);
00448         wait_ms(10);
00449     }
00450     
00451     // Print SIM card IMEI number.
00452     char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
00453     uint8_t imeiLen = fona.getIMEI(imei);
00454     if (imeiLen > 0) {
00455         pcSerial.printf("SIM card IMEI: %s\r\n", imei);
00456     }
00457     mut.unlock();
00458 }
00459 
00460 
00461 int main() {
00462    
00463     initialize_everything(); //does all the setup stuff
00464  ///   //start threads
00465     pcSerial.printf("everything is initialized\r\n");
00466     Thread thread1(display_func); //this is what waits for something to touch it
00467     pcSerial.printf("display thread good\r\n");
00468     Thread thread2(textout);
00469     pcSerial.printf("textout thread good\r\n");
00470     //Thread thread3(texting);
00471     while (true){
00472     led2 = !led2;
00473     Thread::wait(500);
00474     }
00475    
00476     //wait
00477    
00478 }