Dream Team / Mbed 2 deprecated FONA_CellPhone

Dependencies:   mbed

main_withULCD.cpp

Committer:
George windoge T
Date:
2016-03-11
Revision:
7:7f116e2a184b
Parent:
6:15fe38e607ed
Child:
8:3bb66cdfabac

File content as of revision 7:7f116e2a184b:

/*************************************************** 
  This is an example for our Adafruit FONA Cellular Module

  Designed specifically to work with the Adafruit FONA 
  ----> http://www.adafruit.com/products/1946
  ----> http://www.adafruit.com/products/1963
  ----> http://www.adafruit.com/products/2468
  ----> http://www.adafruit.com/products/2542

  These cellular modules use TTL Serial to communicate, 2 pins are 
  required to interface
  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/
 
 /*
  *  Modified by Marc PLOUHINEC 27/06/2015 for use in mbed
  */

/* 
THIS CODE IS STILL IN PROGRESS!

Open up the serial console on the Arduino at 4800 baud to interact with FONA

Note that if you need to set a GPRS APN, username, and password scroll down to
the commented section below just before the main "while (true)" loop.
*/

#include <ctype.h>
//#include "SoftSerial.h" I dont think we need this
#include "Adafruit_FONA.h"
#include "PinDetect.h"
#include "uLCD_4DGL.h"

#define FONA_RST p12
#define FONA_TX p13
#define FONA_RX p14
#define FONA_RI p11

// this is a large buffer for replies
char replybuffer[255];


Serial pcSerial(USBTX, USBRX);
Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
uLCD_4DGL uLCD(p28,p27,p30);

PinDetect pbUp(p20);
PinDetect pbMid(p19);
PinDetect pbDown(p21);
PinDetect pbBACK(p18);

// Turn on a LED when somebody call the FONA
DigitalOut led1(LED1); 
class FonaEventListener : public Adafruit_FONA::EventListener {
    virtual void onRing() {
        led1 = 1;
    }
    
    virtual void onNoCarrier() {
        led1 = 0; 
    }
};
FonaEventListener fonaEventListener;

// Functions defined after main()
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
void printMenu(void);
void printTopLine(void);
void printTime(void);
void flushSerial();
char readBlocking();
uint16_t readnumber();
long map(long x, long in_min, long in_max, long out_min, long out_max);




enum TOPMENU {PHONE, SMS, RADIO, radio_TUNE, radio_OFF, radio_VOLUME,  EXECUTE_TUNE, EXECUTE_radio_OFF};
TOPMENU myTopMenu = PHONE;


bool printFirst = true;
bool blockInterrupts = false;

DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
//Button interrupt functions
void pbUp_hit_callback(void) {
    if (!blockInterrupts){
        printFirst = true;
        switch(myTopMenu) {
            case PHONE: {
                myTopMenu = RADIO;
                uLCD.cls();
                //printTopLine();
                uLCD.locate(0,3);
                uLCD.printf("RADIO");
                break;
            }
            case SMS: {
                myTopMenu = PHONE;
                uLCD.cls();
                //printTopLine();
                uLCD.locate(0,3);
                uLCD.printf("PHONE");
                break;
            }
            case RADIO: {
                myTopMenu = SMS;
                uLCD.cls();
                //printTopLine();
                uLCD.locate(0,3);
                uLCD.printf("SMS");
                break;
            }
            case radio_TUNE: {
                myTopMenu = radio_VOLUME;
                //uLCD.cls();
                //printTopLine();
                //uLCD.locate(0,3);
                //uLCD.printf("RADIO\n->VOLUME");
                break;
            }
            case radio_OFF: {
                myTopMenu = radio_TUNE;
                //uLCD.cls();
                //printTopLine();
                //uLCD.locate(0,3);
                //uLCD.printf("RADIO\n->TUNE");
                break;
            }
            case radio_VOLUME: {
                myTopMenu = radio_OFF;
                //uLCD.cls();
                //printTopLine();
                //uLCD.locate(0,3);
                //uLCD.printf("RADIO\n->OFF");
                break;
            }
        }
    }
}
void pbMid_hit_callback(void) {
    if (!blockInterrupts){
        printFirst = true;
        switch(myTopMenu) {
            case PHONE: {
                led2=!led2;
                break;
            }
            case SMS: {
                led3=!led3;
                break;
            }
            case RADIO: 
                myTopMenu = radio_TUNE;
                break;
                //led4=!led4;
            }
            case radio_TUNE: {
                uLCD.cls();
                //printTopLine()
                myTopMenu = EXECUTE_TUNE;
                break;
                //led4=!led4;
            }
            case radio_OFF: {
                uLCD.cls();
                //printTopLine();
                myTopMenu = EXECUTE_radio_OFF;
                break;
                //led4=!led4;
            }
        }
    }
}
void pbDown_hit_callback(void) {
    if (!blockInterrupts){
        printFirst = true;
        switch(myTopMenu) {
            case PHONE: {
                myTopMenu = SMS;
                uLCD.cls();
                uLCD.locate(0,3);
                uLCD.printf("SMS");
                break;
            }
            case SMS: {
                myTopMenu = RADIO;
                uLCD.cls();
                uLCD.locate(0,3);
                uLCD.printf("RADIO");
                break;
            }
            case RADIO: {
                myTopMenu = PHONE;
                uLCD.cls();
                uLCD.locate(0,3);
                uLCD.printf("PHONE");
                break;
            }
            case radio_TUNE: {
                myTopMenu = radio_OFF;
                //uLCD.cls();
                //printTopLine();
                break;
            }
            case radio_OFF: {
                myTopMenu = radio_VOLUME;
                //uLCD.cls();
                //printTopLine();
                break;
            }
            case radio_VOLUME: {
                myTopMenu = radio_TUNE;
                //uLCD.cls();
                //printTopLine();
                break;
            }
        }
    }
}
void pbBACK_hit_callback(void) {
    led4=!led4;
}


int main() {
    // Set the LCD screen up
    uLCD.display_control(PORTRAIT);
    uLCD.cls();
    uLCD.baudrate(BAUD_3000000);
    uLCD.background_color(BLACK);

    //set up button interrupts
    pbUp.mode(PullUp);
    pbMid.mode(PullUp);
    pbDown.mode(PullUp);
    pbBACK.mode(PullUp);
    wait(.5);
    pbUp.attach_deasserted(&pbUp_hit_callback);
    pbMid.attach_deasserted(&pbMid_hit_callback);
    pbDown.attach_deasserted(&pbDown_hit_callback);
    pbBACK.attach_deasserted(&pbBACK_hit_callback);
    pbUp.setSampleFrequency(); 
    pbMid.setSampleFrequency();
    pbDown.setSampleFrequency();
    pbBACK.setSampleFrequency();

    //pcSerial.baud(9600);
    wait(1);
    //pcSerial.printf("\r\n");
    
    uLCD.printf("FONA basic test\r\n");
    uLCD.printf("Initializing....\r\n");
    
    // See if the FONA is responding
    if (! fona.begin(9600)) {
        uLCD.cls();
        uLCD.printf("Couldn't find FONA\r\n");
        while (1);
    }
    fona.setEventListener(&fonaEventListener);
    uLCD.cls();
    pcSerial.printf("FONA is OK\r\n");
    wait(1);
    
    // Print SIM card IMEI number.
    char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
    uint8_t imeiLen = fona.getIMEI(imei);
    if (imeiLen > 0) {
        uLCD.printf("SIM card IMEI:\n %s\r\n", imei);
    }
    
    // Optionally configure a GPRS APN, username, and password.
    // You might need to do this to access your network's GPRS/data
    // network.  Contact your provider for the exact APN, username,
    // and password values.  Username and password are optional and
    // can be removed, but APN is required.
    //fona.setGPRSNetworkSettings("your APN", "your username", "your password");
    //fona.setGPRSNetworkSettings("web.pt.lu", "", "");
    
    // Optionally configure HTTP gets to follow redirects over SSL.
    // Default is not to follow SSL redirects, however if you uncomment
    // the following line then redirects over SSL will be followed.
    //fona.setHTTPSRedirect(true);

    //printMenu();
    
    wait(1);
    //set up the time
    if (!fona.enableNetworkTimeSync(true)){
        uLCD.printf("NA\r\n");
    }

    uLCD.cls();
    printTopLine();
    
    
    while (true) {
        switch(myTopMenu){
            case PHONE: {
                if (printFirst){
                    printTopLine();
                    printFirst = false;
                }
                break;
            }
            case SMS:{
                if (printFirst){
                    printTopLine();
                    printFirst = false;
                }
                break;
            }
            case RADIO: {
                if (printFirst){
                    printTopLine();
                    printFirst = false;
                }
                break;
            }
            case radio_TUNE: {
                if (printFirst){
                    uLCD.cls();
                    printTopLine();
                    uLCD.locate(0,3);
                    uLCD.printf("RADIO\n->TUNE");
                    printFirst = false;
                    wait(.5);
                }
                break;
            }
            case radio_OFF: {
                if (printFirst){
                    uLCD.cls();
                    printTopLine();
                    uLCD.locate(0,3);
                    uLCD.printf("RADIO\n->OFF");
                    printFirst = false;
                }
                break;
            }
            case radio_VOLUME: {
                if (printFirst){
                    uLCD.cls();
                    printTopLine();
                    uLCD.locate(0,3);
                    uLCD.printf("RADIO\n->VOLUME");
                    printFirst = false;
                }
                break;
            }
            case EXECUTE_TUNE: {
                blockInterrupts = true;
                uLCD.cls();
                // get freq
                flushSerial();
                uLCD.printf("COMPUTER PORT->");
                pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): ");
                uint16_t station = readnumber();
                pcSerial.printf("\r\n");
                // FM radio ON using headset
                if (fona.FMradio(true, FONA_HEADSETAUDIO)) {
                    pcSerial.printf("Opened\r\n");
                }
                if (! fona.tuneFMradio(station)) {
                    pcSerial.printf("Failed\r\n");
                } else {
                    pcSerial.printf("Tuned\r\n");
                }
                uLCD.printf("\n\nTuned!");
                wait(1);
                printFirst = true;
                blockInterrupts = false;
                myTopMenu = radio_TUNE;
                break;
            }
            case EXECUTE_radio_OFF: {
                blockInterrupts = true;
                if (printFirst){
                    if (! fona.FMradio(false)) {
                        uLCD.printf("\n\nFailed\r\n");
                        wait(1);
                    } 
                    else {
                        uLCD.printf("\n\nOK!\r\n");
                        wait(1);
                    }
                    printFirst = true;
                    blockInterrupts = false;
                    myTopMenu = radio_OFF;
                }
                break;
            }




        }
    }
}





void printTopLine(void) {
    uLCD.locate(5,0);
    // read the battery voltage and percentage
    uint16_t vbat;
    if (! fona.getBattPercent(&vbat)) {
        uLCD.printf("BATT:NA\r\n");
    } 
    else {
        uLCD.printf("b=%d%%\r\n", vbat);
    }
}



void printMenu(void) {
    pcSerial.printf("-------------------------------------\r\n");
    pcSerial.printf("[?] Print this menu\r\n");
    pcSerial.printf("[a] read the ADC (2.8V max)\r\n");
    pcSerial.printf("[b] read the Battery V and %% charged\r\n");
    pcSerial.printf("[C] read the SIM CCID\r\n");
    pcSerial.printf("[U] Unlock SIM with PIN code\r\n");
    pcSerial.printf("[i] read RSSI\r\n");
    pcSerial.printf("[n] get Network status\r\n");
    pcSerial.printf("[v] set audio Volume\r\n");
    pcSerial.printf("[V] get Volume\r\n");
    pcSerial.printf("[H] set Headphone audio\r\n");
    pcSerial.printf("[e] set External audio\r\n");
    pcSerial.printf("[T] play audio Tone\r\n");
    pcSerial.printf("[P] PWM/Buzzer out\r\n");
    
    // FM (SIM800 only)
    pcSerial.printf("[f] tune FM radio\r\n");
    pcSerial.printf("[F] turn off FM\r\n");
    pcSerial.printf("[m] set FM volume\r\n");
    pcSerial.printf("[M] get FM volume\r\n");
    pcSerial.printf("[q] get FM station signal level\r\n");
    
    // Phone
    pcSerial.printf("[c] make phone Call\r\n");
    pcSerial.printf("[h] Hang up phone\r\n");
    pcSerial.printf("[p] Pick up phone\r\n");
    
    // SMS
    pcSerial.printf("[N] Number of SMSs\r\n");
    pcSerial.printf("[r] Read SMS #\r\n");
    pcSerial.printf("[R] Read All SMS\r\n");
    pcSerial.printf("[d] Delete SMS #\r\n");
    pcSerial.printf("[s] Send SMS\r\n");
    
    // Time
    pcSerial.printf("[y] Enable network time sync\r\n");   
    pcSerial.printf("[Y] Enable NTP time sync (GPRS)\r\n");   
    pcSerial.printf("[t] Get network time\r\n");
    
    // GPRS
    pcSerial.printf("[G] Enable GPRS\r\n");
    pcSerial.printf("[g] Disable GPRS\r\n");
    pcSerial.printf("[l] Query GSMLOC (GPRS)\r\n");
    pcSerial.printf("[w] Read webpage (GPRS)\r\n");
    pcSerial.printf("[W] Post to website (GPRS)\r\n");
    
    // GPS
    pcSerial.printf("[O] Turn GPS on (SIM808)\r\n");
    pcSerial.printf("[o] Turn GPS off (SIM808)\r\n");
    pcSerial.printf("[x] GPS fix status (SIM808)\r\n");
    pcSerial.printf("[L] Query GPS location (SIM808)\r\n");
    pcSerial.printf("[E] Raw NMEA out (SIM808)\r\n");
    
    pcSerial.printf("[S] create Serial passthru tunnel\r\n");
    pcSerial.printf("-------------------------------------\r\n");
    pcSerial.printf("\r\n");
}

void flushSerial() {
    while (pcSerial.readable()) 
        pcSerial.getc();
}

char readBlocking() {
    while (!pcSerial.readable());
    return pcSerial.getc();
}

uint16_t readnumber() {
    uint16_t x = 0;
    char c;
    while (! isdigit(c = readBlocking())) {
        //pcSerial.putc(c);
    }
    pcSerial.putc(c);
    x = c - '0';
    while (isdigit(c = readBlocking())) {
        pcSerial.putc(c);
        x *= 10;
        x += c - '0';
    }
    return x;
}
  
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
    uint16_t buffidx = 0;
    bool timeoutvalid = true;
    if (timeout == 0) timeoutvalid = false;
    
    while (true) {
        if (buffidx > maxbuff) {
            //pcSerial.printf("SPACE\r\n");
            break;
        }
        
        while(pcSerial.readable()) {
            char c =  pcSerial.getc();
            
            //pcSerial.printf("%02x#%c\r\n", c, c);
            
            if (c == '\r') continue;
            if (c == 0xA) {
                if (buffidx == 0)   // the first 0x0A is ignored
                    continue;
                
                timeout = 0;         // the second 0x0A is the end of the line
                timeoutvalid = true;
                break;
            }
            buff[buffidx] = c;
            buffidx++;
        }
        
        if (timeoutvalid && timeout == 0) {
            //pcSerial.printf("TIMEOUT\r\n");
            break;
        }
        wait_ms(1);
    }
    buff[buffidx] = 0;  // null term
    return buffidx;
}

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}