program to test exporting mbed-dev
Fork of FONA_CellPhone by
Revision 20:7af85396d035, committed 2018-02-28
- Comitter:
- michaelVisimid
- Date:
- Wed Feb 28 00:11:36 2018 +0000
- Parent:
- 19:06dd156420a5
- Commit message:
- Initial commit
Changed in this revision
diff -r 06dd156420a5 -r 7af85396d035 Adafruit_FONA.cpp --- a/Adafruit_FONA.cpp Fri Jan 27 00:22:31 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1532 +0,0 @@ -/*************************************************** - This is a library 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 - - These displays 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 George Tzintzarov & Jesse Baker 03/14/2016 for use in mbed LPC1768 - */ -#include <algorithm> -#include "Adafruit_FONA.h" - -#define HIGH 1 -#define LOW 0 - -/* -Notes George is taking: -sendCheckReply returns a boolean where its true when FONA responds with the string of its second input - -*/ - - -bool Adafruit_FONA::begin(int baudrate) { - mySerial.baud(baudrate); //set the baud rate of the fona serial connection - mySerial.attach(Callback<void()>(this, &Adafruit_FONA::onSerialDataReceived), Serial::RxIrq); - - // INIT Reboot process - _rstpin = HIGH; - wait_ms(10); - _rstpin = LOW; - wait_ms(100); - _rstpin = HIGH; - - // give 3 seconds to reboot - wait_ms(3000); - - // flushes the serial port - while (readable()) getc(); - - // Make sure FONA is alive - sendCheckReply("AT", "OK"); - wait_ms(100); - sendCheckReply("AT", "OK"); - wait_ms(100); - sendCheckReply("AT", "OK"); - wait_ms(100); - - // turn off Echo! - sendCheckReply("ATE0", "OK"); - wait_ms(100); - - // Just checks if the FONA even responds, if it doesnt, then return false - if (! sendCheckReply("ATE0", "OK")) { - return false; - } - sendCheckReply("AT+CVHU=0", "OK"); - wait_ms(100); - _incomingCall = false; - return true; -} - -void Adafruit_FONA::setEventListener(EventListener *eventListener) { - this->eventListener = eventListener; -} - -bool Adafruit_FONA::setBaudrate(uint16_t baud) { - return sendCheckReply("AT+IPREX=", baud, "OK"); -} - -/********* Stream ********************************************/ - -int Adafruit_FONA::_putc(int value) { - return mySerial.putc(value); -} - -int Adafruit_FONA::_getc() { - __disable_irq(); // Start Critical Section - don't interrupt while changing global buffer variables - - // Wait for data if the buffer is empty - if (isRxBufferEmpty()) { - __enable_irq(); // End Critical Section - need to allow rx interrupt to get new characters for buffer - - while(isRxBufferEmpty()); - - __disable_irq(); // Start Critical Section - don't interrupt while changing global buffer variables - } - - int data = rxBuffer[rxBufferOutIndex]; - incrementRxBufferOutIndex(); - - __enable_irq(); // End Critical Section - - return data; -} - -int Adafruit_FONA::readable() { - return !isRxBufferEmpty(); -} - -void Adafruit_FONA::onSerialDataReceived() { - while (mySerial.readable() && !isRxBufferFull()) { - int data = mySerial.getc(); - rxBuffer[rxBufferInIndex] = data; - - // - // Analyze the received data in order to detect events like RING or NO CARRIER - // - - // Copy the data in the current line - if (currentReceivedLineSize < RX_BUFFER_SIZE && data != '\r' && data != '\n') { - currentReceivedLine[currentReceivedLineSize] = (char) data; - currentReceivedLineSize++; - } - - // Check if the line is complete - if (data == '\n') { - currentReceivedLine[currentReceivedLineSize] = 0; - - if (eventListener != NULL) { - // Check if we have a special event - if (strcmp(currentReceivedLine, "RING") == 0) { - eventListener->onRing(); - } else if (strcmp(currentReceivedLine, "NO CARRIER") == 0) { - eventListener->onNoCarrier(); - } - } - - currentReceivedLineSize = 0; - } - - incrementRxBufferInIndex(); - } -} - -/********* Real Time Clock ********************************************/ -bool Adafruit_FONA::readRTC(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *hr, uint8_t *min, uint8_t *sec) { - uint16_t v; - sendParseReply("AT+CCLK?", "+CCLK: ", &v, '/', 0); - *year = v; - return true; -} -bool Adafruit_FONA::enableRTC(uint8_t i) { - if (! sendCheckReply("AT+CLTS=", i, "OK")) - return false; - return sendCheckReply("AT&W", "OK"); -} - -/********* BATTERY & ADC ********************************************/ - -/* returns value in mV (uint16_t) */ -bool Adafruit_FONA::getBattVoltage(uint16_t *v) { - return sendParseReply("AT+CBC", "+CBC: ", v, ',', 2); -} - -/* returns the percentage charge of battery as reported by sim800 */ -bool Adafruit_FONA::getBattPercent(uint16_t *p) { - return sendParseReply("AT+CBC", "+CBC: ", p, ',', 1); -} - -bool Adafruit_FONA::getADCVoltage(uint16_t *v) { - return sendParseReply("AT+CADC?", "+CADC: 1,", v); -} - -/********* SIM ***********************************************************/ - -bool Adafruit_FONA::unlockSIM(char *pin) -{ - char sendbuff[14] = "AT+CPIN="; - sendbuff[8] = pin[0]; - sendbuff[9] = pin[1]; - sendbuff[10] = pin[2]; - sendbuff[11] = pin[3]; - sendbuff[12] = NULL; - - return sendCheckReply(sendbuff, "OK"); -} - -uint8_t Adafruit_FONA::getSIMCCID(char *ccid) { - getReply("AT+CCID"); - // up to 20 chars - strncpy(ccid, replybuffer, 20); - ccid[20] = 0; - - readline(); // eat 'OK' - - return strlen(ccid); -} - -/********* IMEI **********************************************************/ - -uint8_t Adafruit_FONA::getIMEI(char *imei) { - getReply("AT+GSN"); - - // up to 15 chars - strncpy(imei, replybuffer, 15); - imei[15] = 0; - - readline(); // eat 'OK' - - return strlen(imei); -} - -/********* NETWORK *******************************************************/ - -uint8_t Adafruit_FONA::getNetworkStatus(void) { - uint16_t status; - - if (! sendParseReply("AT+CREG?", "+CREG: ", &status, ',', 1)) return 0; - - return status; -} - - -uint8_t Adafruit_FONA::getRSSI(void) { - uint16_t reply; - - if (! sendParseReply("AT+CSQ", "+CSQ: ", &reply) ) return 0; - - return reply; -} - -/********* AUDIO *******************************************************/ - -bool Adafruit_FONA::setAudio(uint8_t a) { - // 0 is headset, 1 is external audio - if (a > 1) return false; - - return sendCheckReply("AT+CHFA=", a, "OK"); -} - -uint8_t Adafruit_FONA::getVolume(void) { - uint16_t reply; - - if (! sendParseReply("AT+CLVL?", "+CLVL: ", &reply) ) return 0; - - return reply; -} - -bool Adafruit_FONA::setVolume(uint8_t i) { - return sendCheckReply("AT+CLVL=", i, "OK"); -} - - -bool Adafruit_FONA::playDTMF(char dtmf) { - char str[4]; - str[0] = '\"'; - str[1] = dtmf; - str[2] = '\"'; - str[3] = 0; - return sendCheckReply("AT+CLDTMF=3,", str, "OK"); -} - -bool Adafruit_FONA::playToolkitTone(uint8_t t, uint16_t len) { - return sendCheckReply("AT+STTONE=1,", t, len, "OK"); -} - -bool Adafruit_FONA::setMicVolume(uint8_t a, uint8_t level) { - // 0 is headset, 1 is external audio - if (a > 1) return false; - - return sendCheckReply("AT+CMIC=", a, level, "OK"); -} - -/********* FM RADIO *******************************************************/ - - -bool Adafruit_FONA::FMradio(bool onoff, uint8_t a) { - if (! onoff) { - return sendCheckReply("AT+FMCLOSE", "OK"); - } - - // 0 is headset, 1 is external audio - if (a > 1) return false; - - return sendCheckReply("AT+FMOPEN=", a, "OK"); -} - -bool Adafruit_FONA::tuneFMradio(uint16_t station) { - // Fail if FM station is outside allowed range. - if ((station < 870) || (station > 1090)) - return false; - - return sendCheckReply("AT+FMFREQ=", station, "OK"); -} - -bool Adafruit_FONA::setFMVolume(uint8_t i) { - // Fail if volume is outside allowed range (0-6). - if (i > 6) { - return false; - } - // Send FM volume command and verify response. - return sendCheckReply("AT+FMVOLUME=", i, "OK"); -} - -int8_t Adafruit_FONA::getFMVolume() { - uint16_t level; - - if (! sendParseReply("AT+FMVOLUME?", "+FMVOLUME: ", &level) ) return 0; - - return level; -} - -int8_t Adafruit_FONA::getFMSignalLevel(uint16_t station) { - // Fail if FM station is outside allowed range. - if ((station < 875) || (station > 1080)) { - return -1; - } - - // Send FM signal level query command. - // Note, need to explicitly send timeout so right overload is chosen. - getReply("AT+FMSIGNAL=", station, FONA_DEFAULT_TIMEOUT_MS); - // Check response starts with expected value. - char *p = strstr(replybuffer, "+FMSIGNAL: "); - if (p == 0) return -1; - p+=11; - // Find second colon to get start of signal quality. - p = strchr(p, ':'); - if (p == 0) return -1; - p+=1; - // Parse signal quality. - int8_t level = atoi(p); - readline(); // eat the "OK" - return level; -} - -/********* PWM/BUZZER **************************************************/ - -bool Adafruit_FONA::setPWM(uint16_t period, uint8_t duty) { - if (period > 2000) return false; - if (duty > 100) return false; - - return sendCheckReply("AT+SPWM=0,", period, duty, "OK"); -} - -/********* CALL PHONES **************************************************/ -bool Adafruit_FONA::callPhone(char *number) { - char sendbuff[35] = "ATD"; - strncpy(sendbuff+3, number, min((int)30, (int)strlen(number))); - uint8_t x = strlen(sendbuff); - sendbuff[x] = ';'; - sendbuff[x+1] = 0; - - return sendCheckReply(sendbuff, "OK"); -} -uint8_t Adafruit_FONA::getCallStatus(void) { - uint16_t phoneStatus; - - if (! sendParseReply("AT+CPAS", "+CPAS: ", &phoneStatus)) - return FONA_CALL_FAILED; // 1, since 0 is actually a known, good reply - - return phoneStatus; // 0 ready, 2 unkown, 3 ringing, 4 call in progress -} - -bool Adafruit_FONA::hangUp(void) { - return sendCheckReply("ATH0", "OK"); -} - -bool Adafruit_FONA::pickUp(void) { - return sendCheckReply("ATA", "OK"); -} - -void Adafruit_FONA::onIncomingCall() { -#ifdef ADAFRUIT_FONA_DEBUG - printf("> Incoming call...\r\n"); -#endif - _incomingCall = true; -} -bool Adafruit_FONA::callerIdNotification(bool enable) { - if(enable){ - _ringIndicatorInterruptIn.fall(Callback<void()>(this, &Adafruit_FONA::onIncomingCall)); - return sendCheckReply("AT+CLIP=1", "OK"); - } - - _ringIndicatorInterruptIn.fall(NULL); - return sendCheckReply("AT+CLIP=0", "OK"); -} - -bool Adafruit_FONA::incomingCallNumber(char* phonenum) { - //+CLIP: "<incoming phone number>",145,"",0,"",0 - if(!_incomingCall) - return false; - - readline(); - while(!strcmp(replybuffer, "RING") == 0) { - flushInput(); - readline(); - } - - readline(); //reads incoming phone number line - - parseReply("+CLIP: \"", phonenum, '"'); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("Phone Number: %s\r\n", replybuffer); -#endif - - _incomingCall = false; - return true; -} - -/********* SMS **********************************************************/ - -uint8_t Adafruit_FONA::getSMSInterrupt(void) { - uint16_t reply; - - if (! sendParseReply("AT+CFGRI?", "+CFGRI: ", &reply) ) return 0; - - return reply; -} - -bool Adafruit_FONA::setSMSInterrupt(uint8_t i) { - return sendCheckReply("AT+CFGRI=", i, "OK"); -} - -int8_t Adafruit_FONA::getNumSMS(void) { - uint16_t numsms; - - if (! sendCheckReply("AT+CMGF=1", "OK")) return -1; - // ask how many sms are stored - if (sendParseReply("AT+CPMS?", "\"SM\",", &numsms)) - return numsms; - if (sendParseReply("AT+CPMS?", "\"SM_P\",", &numsms)) - return numsms; - - return -1; - -} - -// Reading SMS's is a bit involved so we don't use helpers that may cause delays or debug -// printouts! -bool Adafruit_FONA::readSMS(uint8_t i, char *smsbuff, uint16_t maxlen, uint16_t *readlen) { - // text mode - if (! sendCheckReply("AT+CMGF=1", "OK")) return false; - - // show all text mode parameters - if (! sendCheckReply("AT+CSDH=1", "OK")) return false; - - // parse out the SMS len - uint16_t thesmslen = 0; - - //getReply(F("AT+CMGR="), i, 1000); // do not print debug! - mySerial.printf("AT+CMGR=%d\r\n", i); - readline(1000); // timeout - - // parse it out... - if (! parseReply("+CMGR:", &thesmslen, ',', 11)) { - *readlen = 0; - return false; - } - - readRaw(thesmslen); - - flushInput(); - - uint16_t thelen = min(maxlen, (uint16_t)strlen(replybuffer)); - strncpy(smsbuff, replybuffer, thelen); - smsbuff[thelen] = 0; // end the string - -#ifdef ADAFRUIT_FONA_DEBUG - printf("%s\r\n", replybuffer); -#endif - *readlen = thelen; - return true; -} - -// Retrieve the sender of the specified SMS message and copy it as a string to -// the sender buffer. Up to senderlen characters of the sender will be copied -// and a null terminator will be added if less than senderlen charactesr are -// copied to the result. Returns true if a result was successfully retrieved, -// otherwise false. -bool Adafruit_FONA::getSMSSender(uint8_t i, char *sender, int senderlen) { - // Ensure text mode and all text mode parameters are sent. - if (! sendCheckReply("AT+CMGF=1", "OK")) return false; - if (! sendCheckReply("AT+CSDH=1", "OK")) return false; - // Send command to retrieve SMS message and parse a line of response. - mySerial.printf("AT+CMGR=%d\r\n", i); - readline(1000); - // Parse the second field in the response. - bool result = parseReplyQuoted("+CMGR:", sender, senderlen, ',', 1); - // Drop any remaining data from the response. - flushInput(); - return result; -} - -bool Adafruit_FONA::sendSMS(char *smsaddr, char *smsmsg) { - if (! sendCheckReply("AT+CMGF=1", "OK")) return -1; - - char sendcmd[30] = "AT+CMGS=\""; - strncpy(sendcmd+9, smsaddr, 30-9-2); // 9 bytes beginning, 2 bytes for close quote + null - sendcmd[strlen(sendcmd)] = '\"'; - - if (! sendCheckReply(sendcmd, "> ")) return false; -#ifdef ADAFRUIT_FONA_DEBUG - printf("> %s\r\n", smsmsg); -#endif - mySerial.printf("%s\r\n\r\n", smsmsg); - mySerial.putc(0x1A); -#ifdef ADAFRUIT_FONA_DEBUG - printf("^Z\r\n"); -#endif - readline(10000); // read the +CMGS reply, wait up to 10 seconds!!! - //Serial.print("* "); Serial.println(replybuffer); - if (strstr(replybuffer, "+CMGS") == 0) { - return false; - } - readline(1000); // read OK - //Serial.print("* "); Serial.println(replybuffer); - - if (strcmp(replybuffer, "OK") != 0) { - return false; - } - - return true; -} - - -bool Adafruit_FONA::deleteSMS(uint8_t i) { - if (! sendCheckReply("AT+CMGF=1", "OK")) return -1; - // read an sms - char sendbuff[12] = "AT+CMGD=000"; - sendbuff[8] = (i / 100) + '0'; - i %= 100; - sendbuff[9] = (i / 10) + '0'; - i %= 10; - sendbuff[10] = i + '0'; - - return sendCheckReply(sendbuff, "OK", 2000); -} - -/********* TIME **********************************************************/ -bool Adafruit_FONA::sendUSSD(char *ussdmsg, char *ussdbuff, uint16_t maxlen, uint16_t *readlen) { - if (! sendCheckReply("AT+CUSD=1", "OK")) return false; - - char sendcmd[30] = "AT+CUSD=1,\""; - strncpy(sendcmd+11, ussdmsg, 30-11-2); // 11 bytes beginning, 2 bytes for close quote + null - sendcmd[strlen(sendcmd)] = '\"'; - - if (! sendCheckReply(sendcmd, "OK")) { - *readlen = 0; - return false; - } else { - readline(10000); // read the +CUSD reply, wait up to 10 seconds!!! - //DEBUG_PRINT("* "); DEBUG_PRINTLN(replybuffer); - char *p = strstr(replybuffer, "+CUSD: "); - if (p == 0) { - *readlen = 0; - return false; - } - p+=7; //+CUSD - // Find " to get start of ussd message. - p = strchr(p, '\"'); - if (p == 0) { - *readlen = 0; - return false; - } - p+=1; //" - // Find " to get end of ussd message. - char *strend = strchr(p, '\"'); - - uint16_t lentocopy = min(maxlen-1, strend - p); - strncpy(ussdbuff, p, lentocopy+1); - ussdbuff[lentocopy] = 0; - *readlen = lentocopy; - } - return true; -} - -bool Adafruit_FONA::enableNetworkTimeSync(bool onoff) { - if (onoff) { - if (! sendCheckReply("AT+CLTS=1", "OK")) - return false; - } else { - if (! sendCheckReply("AT+CLTS=0", "OK")) - return false; - } - - flushInput(); // eat any 'Unsolicted Result Code' - - return true; -} - -bool Adafruit_FONA::enableNTPTimeSync(bool onoff, const char* ntpserver) { - if (onoff) { - if (! sendCheckReply("AT+CNTPCID=1", "OK")) - return false; - - mySerial.printf("AT+CNTP=\""); - if (ntpserver != 0) { - mySerial.printf(ntpserver); - } else { - mySerial.printf("pool.ntp.org"); - } - mySerial.printf("\",0\r\n"); - readline(FONA_DEFAULT_TIMEOUT_MS); - if (strcmp(replybuffer, "OK") != 0) - return false; - - if (! sendCheckReply("AT+CNTP", "OK", 10000)) - return false; - - uint16_t status; - readline(10000); - if (! parseReply("+CNTP:", &status)) - return false; - } else { - if (! sendCheckReply("AT+CNTPCID=0", "OK")) - return false; - } - - return true; -} - -bool Adafruit_FONA::getTime(char* buff, uint16_t maxlen) { - getReply("AT+CCLK?", (uint16_t) 10000); - if (strncmp(replybuffer, "+CCLK: ", 7) != 0) - return false; - - char *p = replybuffer+7; - uint16_t lentocopy = min((uint16_t)(maxlen-1), (uint16_t)strlen(p)); - strncpy(buff, p, lentocopy+1); - buff[lentocopy] = 0; - - readline(); // eat OK - - return true; -} - -/********* GPS **********************************************************/ - - -bool Adafruit_FONA::enableGPS(bool onoff) { - uint16_t state; - - // first check if its already on or off - if (! sendParseReply("AT+CGPSPWR?", "+CGPSPWR: ", &state) ) - return false; - - if (onoff && !state) { - if (! sendCheckReply("AT+CGPSPWR=1", "OK")) - return false; - } else if (!onoff && state) { - if (! sendCheckReply("AT+CGPSPWR=0", "OK")) - return false; - } - return true; -} - -int8_t Adafruit_FONA::GPSstatus(void) { - getReply("AT+CGPSSTATUS?"); - - char *p = strstr(replybuffer, "+CGPSSTATUS: Location "); - if (p == 0) return -1; - - p+=22; - - readline(); // eat 'OK' - - - if (p[0] == 'U') return 0; - if (p[0] == 'N') return 1; - if (p[0] == '2') return 2; - if (p[0] == '3') return 3; - - // else - return 0; -} - -uint8_t Adafruit_FONA::getGPS(uint8_t arg, char *buffer, uint8_t maxbuff) { - int32_t x = arg; - - getReply("AT+CGPSINF=", x); - - char *p = strstr(replybuffer, "CGPSINF: "); - if (p == 0){ - buffer[0] = 0; - return 0; - } - p+=9; - uint8_t len = max((uint8_t)(maxbuff-1), (uint8_t)strlen(p)); - strncpy(buffer, p, len); - buffer[len] = 0; - - readline(); // eat 'OK' - return len; -} - -bool Adafruit_FONA::getGPS(float *lat, float *lon, float *speed_kph, float *heading, float *altitude) { - char gpsbuffer[120]; - - // we need at least a 2D fix - if (GPSstatus() < 2) - return false; - - // grab the mode 2^5 gps csv from the sim808 - uint8_t res_len = getGPS(32, gpsbuffer, 120); - - // make sure we have a response - if (res_len == 0) - return false; - - // skip mode - char *tok = strtok(gpsbuffer, ","); - if (! tok) return false; - - // skip date - tok = strtok(NULL, ","); - if (! tok) return false; - - // skip fix - tok = strtok(NULL, ","); - if (! tok) return false; - - // grab the latitude - char *latp = strtok(NULL, ","); - if (! latp) return false; - - // grab latitude direction - char *latdir = strtok(NULL, ","); - if (! latdir) return false; - - // grab longitude - char *longp = strtok(NULL, ","); - if (! longp) return false; - - // grab longitude direction - char *longdir = strtok(NULL, ","); - if (! longdir) return false; - - double latitude = atof(latp); - double longitude = atof(longp); - - // convert latitude from minutes to decimal - float degrees = floor(latitude / 100); - double minutes = latitude - (100 * degrees); - minutes /= 60; - degrees += minutes; - - // turn direction into + or - - if (latdir[0] == 'S') degrees *= -1; - - *lat = degrees; - - // convert longitude from minutes to decimal - degrees = floor(longitude / 100); - minutes = longitude - (100 * degrees); - minutes /= 60; - degrees += minutes; - - // turn direction into + or - - if (longdir[0] == 'W') degrees *= -1; - - *lon = degrees; - - // only grab speed if needed - if (speed_kph != NULL) { - - // grab the speed in knots - char *speedp = strtok(NULL, ","); - if (! speedp) return false; - - // convert to kph - *speed_kph = atof(speedp) * 1.852; - - } - - // only grab heading if needed - if (heading != NULL) { - - // grab the speed in knots - char *coursep = strtok(NULL, ","); - if (! coursep) return false; - - *heading = atof(coursep); - - } - - // no need to continue - if (altitude == NULL) - return true; - - // we need at least a 3D fix for altitude - if (GPSstatus() < 3) - return false; - - // grab the mode 0 gps csv from the sim808 - res_len = getGPS(0, gpsbuffer, 120); - - // make sure we have a response - if (res_len == 0) - return false; - - // skip mode - tok = strtok(gpsbuffer, ","); - if (! tok) return false; - - // skip lat - tok = strtok(NULL, ","); - if (! tok) return false; - - // skip long - tok = strtok(NULL, ","); - if (! tok) return false; - - // grab altitude - char *altp = strtok(NULL, ","); - if (! altp) return false; - - *altitude = atof(altp); - - return true; -} - -bool Adafruit_FONA::enableGPSNMEA(uint8_t i) { - char sendbuff[15] = "AT+CGPSOUT=000"; - sendbuff[11] = (i / 100) + '0'; - i %= 100; - sendbuff[12] = (i / 10) + '0'; - i %= 10; - sendbuff[13] = i + '0'; - - return sendCheckReply(sendbuff, "OK", 2000); -} - - -/********* GPRS **********************************************************/ -#define CIPSHUT_DELAY (uint16_t)65000 -#define CGATT_DELAY (uint16_t)10000 -#define SAPBR_DELAY (uint16_t)85000 -#define CIICR_DELAY (uint16_t)85000 - - -bool Adafruit_FONA::enableGPRS(bool onoff) { - if (onoff) { - // disconnect all sockets - sendCheckReply("AT+CIPSHUT", "SHUT OK", CIPSHUT_DELAY); - - if (! sendCheckReply("AT+CGATT=1", "OK", CGATT_DELAY)) - return false; - - // set bearer profile! connection type GPRS - if (! sendCheckReply("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", SAPBR_DELAY)) - return false; - - // set bearer profile access point name - if (apn) { - // Send command AT+SAPBR=3,1,"APN","<apn value>" where <apn value> is the configured APN value. - if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"APN\",", apn, "OK", SAPBR_DELAY)) - return false; - - flushInput(); - - mySerial.printf("AT+CSTT=\""); - mySerial.printf(apn); - if (apnusername) { - mySerial.printf("\",\""); - mySerial.printf(apnusername); - } - if (apnpassword) { - mySerial.printf("\",\""); - mySerial.printf(apnpassword); - } - mySerial.printf("\""); - mySerial.printf("\r\n"); - - - - if (! expectReply("OK")) return false; - - // set username/password - if (apnusername) { - // Send command AT+SAPBR=3,1,"USER","<user>" where <user> is the configured APN username. - if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"USER\",", apnusername, "OK", SAPBR_DELAY)) - return false; - } - if (apnpassword) { - // Send command AT+SAPBR=3,1,"PWD","<password>" where <password> is the configured APN password. - if (! sendCheckReplyQuoted("AT+SAPBR=3,1,\"PWD\",", apnpassword, "OK", SAPBR_DELAY)) - return false; - } - } - - // open GPRS context - if (! sendCheckReply("AT+SAPBR=1,1", "OK", SAPBR_DELAY)) - return false; - - if (! sendCheckReply("AT+CIICR", "OK", CIICR_DELAY)) - return false; - - } else { - // disconnect all sockets - if (! sendCheckReply("AT+CIPSHUT", "SHUT OK", CIPSHUT_DELAY)) - return false; - - // close GPRS context - if (! sendCheckReply("AT+SAPBR=0,1", "OK", SAPBR_DELAY)) - return false; - - if (! sendCheckReply("AT+CGATT=0", "OK", CGATT_DELAY)) - return false; - } - return true; -} - -uint8_t Adafruit_FONA::GPRSstate(void) { - uint16_t state; - - if (! sendParseReply("AT+CGATT?", "+CGATT: ", &state) ) - return 0xFF; - - return state; -} - -void Adafruit_FONA::setGPRSNetworkSettings(const char* apn, const char* ausername, const char* apassword) { - this->apn = (char*) apn; - this->apnusername = (char*) ausername; - this->apnpassword = (char*) apassword; -} - -bool Adafruit_FONA::getGSMLoc(uint16_t *errorcode, char *buff, uint16_t maxlen) { - getReply("AT+CIPGSMLOC=1,1", CIPGSMLOC_DELAY); - - if (! parseReply("+CIPGSMLOC: ", errorcode)) - return false; - - char *p = replybuffer+14; - uint16_t lentocopy = min((uint16_t)(maxlen-1), (uint16_t)strlen(p)); - strncpy(buff, p, lentocopy+1); - - readline(); // eat OK - - return true; -} - -bool Adafruit_FONA::getGSMLoc(float *lat, float *lon) { - uint16_t returncode; - char gpsbuffer[120]; - - // make sure we could get a response - if (! getGSMLoc(&returncode, gpsbuffer, 120)) - return false; - - // make sure we have a valid return code - if (returncode != 0) - return false; - - // tokenize the gps buffer to locate the lat & long - char *latp = strtok(gpsbuffer, ","); - if (! latp) return false; - - char *longp = strtok(NULL, ","); - if (! longp) return false; - - *lat = atof(latp); - *lon = atof(longp); - - return true; -} - -/********* TCP FUNCTIONS ************************************/ - - -bool Adafruit_FONA::TCPconnect(char *server, uint16_t port) { - flushInput(); - - // close all old connections - if (! sendCheckReply("AT+CIPSHUT", "SHUT OK", CIPSHUT_DELAY) ) return false; - - // single connection at a time - if (! sendCheckReply("AT+CIPMUX=0", "OK",CPIMUX_DELAY) ) return false; - - // manually read data - if (! sendCheckReply("AT+CIPRXGET=1", "OK") ) return false; - -#ifdef ADAFRUIT_FONA_DEBUG - printf("AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", server, port); -#endif - - mySerial.printf("AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", server, port); - - if (! expectReply("OK")) return false; - if (! expectReply("CONNECT OK")) return false; - return true; -} - -bool Adafruit_FONA::TCPclose(void) { - return sendCheckReply("AT+CIPCLOSE", "OK"); -} - -bool Adafruit_FONA::TCPconnected(void) { - if (! sendCheckReply("AT+CIPSTATUS", "OK") ) return false; - readline(100); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return (strcmp(replybuffer, "STATE: CONNECT OK") == 0); -} - -bool Adafruit_FONA::TCPsend(char *packet, uint8_t len) { -#ifdef ADAFRUIT_FONA_DEBUG - printf("AT+CIPSEND=%d\r\n", len); - - for (uint16_t i=0; i<len; i++) { - printf(" 0x%#02x", packet[i]); - } - printf("\r\n"); -#endif - - - mySerial.printf("AT+CIPSEND=%d\r\n", len); - readline(); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - if (replybuffer[0] != '>') return false; - - for (uint16_t i=0; i<len; i++) { - mySerial.putc(packet[i]); - } - readline(3000); // wait up to 3 seconds to send the data -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - - return (strcmp(replybuffer, "SEND OK") == 0); -} - -uint16_t Adafruit_FONA::TCPavailable(void) { - uint16_t avail; - - if (! sendParseReply("AT+CIPRXGET=4", "+CIPRXGET: 4,", &avail, ',', 0) ) return false; - -#ifdef ADAFRUIT_FONA_DEBUG - printf("%d bytes available\r\n", avail); -#endif - - return avail; -} - - -uint16_t Adafruit_FONA::TCPread(uint8_t *buff, uint8_t len) { - uint16_t avail; - - mySerial.printf("AT+CIPRXGET=2,%d\r\n", len); - readline(); - if (! parseReply("+CIPRXGET: 2,", &avail, ',', 0)) return false; - - readRaw(avail); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("%d bytes read\r\n", avail); - for (uint8_t i=0;i<avail;i++) { - printf(" 0x%#02x", replybuffer[i]); - } - printf("\r\n"); -#endif - - memcpy(buff, replybuffer, avail); - - return avail; -} - -/********* HTTP LOW LEVEL FUNCTIONS ************************************/ - -bool Adafruit_FONA::HTTP_init() { - return sendCheckReply("AT+HTTPINIT", "OK"); -} - -bool Adafruit_FONA::HTTP_term() { - return sendCheckReply("AT+HTTPTERM", "OK"); -} - -void Adafruit_FONA::HTTP_para_start(const char* parameter, bool quoted) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> AT+HTTPPARA=\"%s\"\r\n", parameter); -#endif - - mySerial.printf("AT+HTTPPARA=\"%s", parameter); - if (quoted) - mySerial.printf("\",\""); - else - mySerial.printf("\","); -} - -bool Adafruit_FONA::HTTP_para_end(bool quoted) { - if (quoted) - mySerial.printf("\"\r\n"); - else - mySerial.printf("\r\n"); - - return expectReply("OK"); -} - -bool Adafruit_FONA::HTTP_para(const char* parameter, const char* value) { - HTTP_para_start(parameter, true); - mySerial.printf(value); - return HTTP_para_end(true); -} - -bool Adafruit_FONA::HTTP_para(const char* parameter, int32_t value) { - HTTP_para_start(parameter, false); - mySerial.printf("%d", value); - return HTTP_para_end(false); -} - -bool Adafruit_FONA::HTTP_data(uint32_t size, uint32_t maxTime) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> AT+HTTPDATA=%d,%d\r\n", size, maxTime); -#endif - - mySerial.printf("AT+HTTPDATA=%d,%d\r\n", size, maxTime); - - return expectReply("DOWNLOAD"); -} - -bool Adafruit_FONA::HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout) { - // Send request. - if (! sendCheckReply("AT+HTTPACTION=", method, "OK")) - return false; - - // Parse response status and size. - readline(timeout); - if (! parseReply("+HTTPACTION:", status, ',', 1)) - return false; - if (! parseReply("+HTTPACTION:", datalen, ',', 2)) - return false; - - return true; -} - -bool Adafruit_FONA::HTTP_readall(uint16_t *datalen) { - getReply("AT+HTTPREAD"); - if (! parseReply("+HTTPREAD:", datalen, ',', 0)) - return false; - - return true; -} - -bool Adafruit_FONA::HTTP_ssl(bool onoff) { - return sendCheckReply("AT+HTTPSSL=", onoff ? 1 : 0, "OK"); -} - -/********* HTTP HIGH LEVEL FUNCTIONS ***************************/ - -bool Adafruit_FONA::HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen){ - if (! HTTP_setup(url)) - return false; - - // HTTP GET - if (! HTTP_action(FONA_HTTP_GET, status, datalen)) - return false; - -#ifdef ADAFRUIT_FONA_DEBUG - printf("Status: %d\r\n", *status); - printf("Len: %d\r\n", *datalen); -#endif - - // HTTP response data - if (! HTTP_readall(datalen)) - return false; - - return true; -} - -void Adafruit_FONA::HTTP_GET_end(void) { - HTTP_term(); -} - -bool Adafruit_FONA::HTTP_POST_start(char *url, const char* contenttype, const uint8_t *postdata, uint16_t postdatalen, uint16_t *status, uint16_t *datalen) { - if (! HTTP_setup(url)) - return false; - - if (! HTTP_para("CONTENT", contenttype)) { - return false; - } - - // HTTP POST data - if (! HTTP_data(postdatalen, 10000)) - return false; - for (uint16_t i = 0; i < postdatalen; i++) { - mySerial.putc(postdata[i]); - } - if (! expectReply("OK")) - return false; - - // HTTP POST - if (! HTTP_action(FONA_HTTP_POST, status, datalen)) - return false; - -#ifdef ADAFRUIT_FONA_DEBUG - printf("Status: %d\r\n", *status); - printf("Len: %d\r\n", *datalen); -#endif - - // HTTP response data - if (! HTTP_readall(datalen)) - return false; - - return true; -} - -void Adafruit_FONA::HTTP_POST_end(void) { - HTTP_term(); -} - -void Adafruit_FONA::setUserAgent(const char* useragent) { - this->useragent = (char*) useragent; -} - -void Adafruit_FONA::setHTTPSRedirect(bool onoff) { - httpsredirect = onoff; -} - -/********* HTTP HELPERS ****************************************/ - -bool Adafruit_FONA::HTTP_setup(char *url) { - // Handle any pending - HTTP_term(); - - // Initialize and set parameters - if (! HTTP_init()) - return false; - if (! HTTP_para("CID", 1)) - return false; - if (! HTTP_para("UA", useragent)) - return false; - if (! HTTP_para("URL", url)) - return false; - - // HTTPS redirect - if (httpsredirect) { - if (! HTTP_para("REDIR",1)) - return false; - - if (! HTTP_ssl(true)) - return false; - } - - return true; -} - - -/********* HELPERS *********************************************/ - -bool Adafruit_FONA::expectReply(const char* reply, uint16_t timeout) { - readline(timeout); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return (strcmp(replybuffer, reply) == 0); -} - -/********* LOW LEVEL *******************************************/ - -void Adafruit_FONA::flushInput() { - // Read all available serial input to flush pending data. - uint16_t timeoutloop = 0; - while (timeoutloop++ < 40) { - while(readable()) { - getc(); - timeoutloop = 0; // If char was received reset the timer - } - wait_ms(1); - } -} - -uint16_t Adafruit_FONA::readRaw(uint16_t b) { - uint16_t idx = 0; - - while (b && (idx < sizeof(replybuffer)-1)) { - if (readable()) { - replybuffer[idx] = getc(); - idx++; - b--; - } - } - replybuffer[idx] = 0; - - return idx; -} - -// This function just reads the output from FONA after an AT command is sent to it -uint8_t Adafruit_FONA::readline(uint16_t timeout, bool multiline) { - uint16_t replyidx = 0; - - while (timeout--) { - if (replyidx >= 254) { - break; - } - - while(readable()) { - char c = getc(); - if (c == '\r') continue; - if (c == 0xA) { - if (replyidx == 0) // the first 0x0A is ignored - continue; - - if (!multiline) { - timeout = 0; // the second 0x0A is the end of the line - break; - } - } - replybuffer[replyidx] = c; - replyidx++; - } - - if (timeout == 0) { - break; - } - wait_ms(1); - } - replybuffer[replyidx] = 0; // null term - return replyidx; -} - -uint8_t Adafruit_FONA::getReply(const char* send, uint16_t timeout) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> %s\r\n", send); -#endif - - mySerial.printf("%s\r\n",send); - - uint8_t l = readline(timeout); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return l; -} - -// Send prefix, suffix, and newline. Return response (and also set replybuffer with response). -uint8_t Adafruit_FONA::getReply(const char* prefix, char* suffix, uint16_t timeout) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> %s%s\r\n", prefix, suffix); -#endif - - mySerial.printf("%s%s\r\n", prefix, suffix); - - uint8_t l = readline(timeout); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return l; -} - -// Send prefix, suffix, and newline. Return response (and also set replybuffer with response). -uint8_t Adafruit_FONA::getReply(const char* prefix, int32_t suffix, uint16_t timeout) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> %s%d\r\n", prefix, suffix); -#endif - - mySerial.printf("%s%d\r\n", prefix, suffix); - - uint8_t l = readline(timeout); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return l; -} - -// Send prefix, suffix, suffix2, and newline. Return response (and also set replybuffer with response). -uint8_t Adafruit_FONA::getReply(const char* prefix, int32_t suffix1, int32_t suffix2, uint16_t timeout) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> %s%d,%d\r\n", prefix, suffix1, suffix2); -#endif - - mySerial.printf("%s%d,%d\r\n", prefix, suffix1, suffix2); - - uint8_t l = readline(timeout); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return l; -} - -// Send prefix, ", suffix, ", and newline. Return response (and also set replybuffer with response). -uint8_t Adafruit_FONA::getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout) { - flushInput(); - -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t---> %s\"%s\"\r\n", prefix, suffix); -#endif - - mySerial.printf("%s\"%s\"\r\n", prefix, suffix); - - uint8_t l = readline(timeout); -#ifdef ADAFRUIT_FONA_DEBUG - printf("\t<--- %s\r\n", replybuffer); -#endif - return l; -} - - -bool Adafruit_FONA::sendCheckReply(const char *send, const char *reply, uint16_t timeout) { - if (! getReply(send, timeout) ) - return false; - - return (strcmp(replybuffer, reply) == 0); -} - -// Send prefix, suffix, and newline. Verify FONA response matches reply parameter. -bool Adafruit_FONA::sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout) { - getReply(prefix, suffix, timeout); - return (strcmp(replybuffer, reply) == 0); -} - -// Send prefix, suffix, and newline. Verify FONA response matches reply parameter. -bool Adafruit_FONA::sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout) { - getReply(prefix, suffix, timeout); - return (strcmp(replybuffer, reply) == 0); -} - -// Send prefix, suffix, suffix2, and newline. Verify FONA response matches reply parameter. -bool Adafruit_FONA::sendCheckReply(const char* prefix, int32_t suffix1, int32_t suffix2, const char* reply, uint16_t timeout) { - getReply(prefix, suffix1, suffix2, timeout); - return (strcmp(replybuffer, reply) == 0); -} - -// Send prefix, ", suffix, ", and newline. Verify FONA response matches reply parameter. -bool Adafruit_FONA::sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout) { - getReplyQuoted(prefix, suffix, timeout); - return (strcmp(replybuffer, reply) == 0); -} - -bool Adafruit_FONA::parseReply(const char* toreply, uint16_t *v, char divider, uint8_t index) { - char *p = strstr(replybuffer, toreply); // get the pointer to the voltage - if (p == 0) return false; - p += strlen(toreply); - - for (uint8_t i=0; i<index;i++) { - // increment dividers - p = strchr(p, divider); - if (!p) return false; - p++; - } - - *v = atoi(p); - - return true; -} - -bool Adafruit_FONA::parseReply(const char* toreply, char *v, char divider, uint8_t index) { - uint8_t i=0; - char *p = strstr(replybuffer, toreply); - if (p == 0) return false; - p+=strlen(toreply); - - for (i=0; i<index;i++) { - // increment dividers - p = strchr(p, divider); - if (!p) return false; - p++; - } - - for(i=0; i<strlen(p);i++) { - if(p[i] == divider) - break; - v[i] = p[i]; - } - - v[i] = '\0'; - - return true; -} - -// Parse a quoted string in the response fields and copy its value (without quotes) -// to the specified character array (v). Only up to maxlen characters are copied -// into the result buffer, so make sure to pass a large enough buffer to handle the -// response. -bool Adafruit_FONA::parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index) { - uint8_t i=0, j; - // Verify response starts with toreply. - char *p = strstr(replybuffer, toreply); - if (p == 0) return false; - p+=strlen(toreply); - - // Find location of desired response field. - for (i=0; i<index;i++) { - // increment dividers - p = strchr(p, divider); - if (!p) return false; - p++; - } - - // Copy characters from response field into result string. - for(i=0, j=0; j<maxlen && i<strlen(p); ++i) { - // Stop if a divier is found. - if(p[i] == divider) - break; - // Skip any quotation marks. - else if(p[i] == '"') - continue; - v[j++] = p[i]; - } - - // Add a null terminator if result string buffer was not filled. - if (j < maxlen) - v[j] = '\0'; - - return true; -} - -bool Adafruit_FONA::sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider, uint8_t index) { - getReply(tosend); - - if (! parseReply(toreply, v, divider, index)) return false; - - readline(); // eat 'OK' - - return true; -}
diff -r 06dd156420a5 -r 7af85396d035 Adafruit_FONA.h --- a/Adafruit_FONA.h Fri Jan 27 00:22:31 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,412 +0,0 @@ -/*************************************************** - This is a library 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 - - These displays 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 George Tzintzarov & Jesse Baker 03/14/2016 for use in mbed LPC1768 - */ - -#ifndef ADAFRUIT_FONA_H -#define ADAFRUIT_FONA_H - -#include "mbed.h" - -//#define ADAFRUIT_FONA_DEBUG - -#define FONA_HEADSETAUDIO 0 -#define FONA_EXTAUDIO 1 - -#define FONA_STTONE_DIALTONE 1 -#define FONA_STTONE_BUSY 2 -#define FONA_STTONE_CONGESTION 3 -#define FONA_STTONE_PATHACK 4 -#define FONA_STTONE_DROPPED 5 -#define FONA_STTONE_ERROR 6 -#define FONA_STTONE_CALLWAIT 7 -#define FONA_STTONE_RINGING 8 -#define FONA_STTONE_BEEP 16 -#define FONA_STTONE_POSTONE 17 -#define FONA_STTONE_ERRTONE 18 -#define FONA_STTONE_INDIANDIALTONE 19 -#define FONA_STTONE_USADIALTONE 20 - -#define FONA_DEFAULT_TIMEOUT_MS 500 //timeout between send AT and reply from FONA - -#define FONA_HTTP_GET 0 -#define FONA_HTTP_POST 1 -#define FONA_HTTP_HEAD 2 - -#define FONA_CALL_READY 0 -#define FONA_CALL_FAILED 1 -#define FONA_CALL_UNKNOWN 2 -#define FONA_CALL_RINGING 3 -#define FONA_CALL_INPROGRESS 4 - -#define RX_BUFFER_SIZE 255 - -#define CIPSHUT_DELAY (uint16_t)65000 -#define CGATT_DELAY (uint16_t)10000 -#define SAPBR_DELAY (uint16_t)85000 -#define CIICR_DELAY (uint16_t)85000 -#define CGATT_DELAY (uint16_t)10000 -#define CIPGSMLOC_DELAY (uint16_t)60000 -#define CPIMUX_DELAY (uint16_t)160000 - -/** Adafruit FONA 800H Class -* Modified by George Tzintzarov & Jesse Baker 03/14/2016 for use in mbed LPC1768 -*/ - -class Adafruit_FONA : public Stream { - public: - - class EventListener { - public: - /** - * Method called when somebody call the FONA. - */ - virtual void onRing() = 0; - - /** - * Method called when the calling person stop his call. - */ - virtual void onNoCarrier() = 0; - }; - - public: - Serial mySerial; - - Adafruit_FONA(PinName tx, PinName rx, PinName rst, PinName ringIndicator) : - mySerial(tx, rx),_rstpin(rst, false), _ringIndicatorInterruptIn(ringIndicator), - apn("FONAnet"), apnusername(NULL), apnpassword(NULL), httpsredirect(false), useragent("FONA"), - _incomingCall(false), eventListener(NULL), rxBufferInIndex(0), rxBufferOutIndex(0), - currentReceivedLineSize(0) {} - - - bool begin(int baudrate); - - /** Set the event listener for incoming calls - @param eventListener A pointer to the event listener - @see Adafruit_FONA::EventListener for specific example - */ - - void setEventListener(EventListener *eventListener); - bool setBaudrate(uint16_t baud); - bool readRTC(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *hr, uint8_t *min, uint8_t *sec); - // Stream---------------------------------------------------------------------- - virtual int _putc(int value); - virtual int _getc(); - - /** Check if FONA has anything in its output buffer - @return 0 if nothing - */ - int readable(void); - - // RTC---------------------------------------------------------------------- - bool enableRTC(uint8_t i); // i = 0 <=> disable, i = 1 <=> enable - - - bool getADCVoltage(uint16_t *v); - - - bool getBattPercent(uint16_t *p); - - bool getBattVoltage(uint16_t *v); - - - bool unlockSIM(char *pin); - - uint8_t getSIMCCID(char *ccid); - - uint8_t getNetworkStatus(void); - - uint8_t getRSSI(void); - - uint8_t getIMEI(char *imei); - - // set Audio output---------------------------------------------------------------------- - /** Set the Audio Output interface - @param a 0 is headset, 1 is external audio - @return TRUE if successful - */ - bool setAudio(uint8_t a); - - /** Set the Audio Volume - @param i a unit8_t volume number - @return TRUE if successful - */ - bool setVolume(uint8_t i); - - /** Get the Audio Volume - @return the current volume - */ - uint8_t getVolume(void); - bool playToolkitTone(uint8_t t, uint16_t len); - bool setMicVolume(uint8_t a, uint8_t level); - bool playDTMF(char tone); - - // FM radio functions---------------------------------------------------------------------- - /** Tune the FM radio - @param station frequency, for example 107.9 MHz -> 1079 - @return TRUE if successful - */ - bool tuneFMradio(uint16_t station); - - /** FM radio set output - @param onoff bool to turn on if TRUE - @param a 0 (default) is headset, 1 is external audio - @return TRUE if successful - */ - bool FMradio(bool onoff, uint8_t a = FONA_HEADSETAUDIO); - - /** Set the FM Radio Volume - @param i a unit8_t volume number - @return TRUE if successful - */ - bool setFMVolume(uint8_t i); - - /** Get the FM Volume - @return the current FM volume - */ - int8_t getFMVolume(); - - /** Get the FM signal strength - @param station a unit8_t volume number - @return TRUE if successful - */ - int8_t getFMSignalLevel(uint16_t station); - - // SMS handling---------------------------------------------------------------------- - /** Set the SMS Interrupt - @param i 0 = OFF, 1 = ON with TCPIP, FTP, and URC control Ring Indicator Pin, 2 = ON with only TCPIP control - @return TRUE if successful - @see https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf page 152 - */ - bool setSMSInterrupt(uint8_t i); - - /** Get SMS Interrupt Setting - @return setting - @see https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf page 152 - */ - uint8_t getSMSInterrupt(void); - - /** Set the SMS Interrupt - @return number of SMS messages in inbox - */ - int8_t getNumSMS(void); - - /** Read SMS - @param i sms number in memory - @param smsbuff char pointer to char array - @param max Maximum length of smsbuff - @param readsize the size in bytes of the SMS - @return TRUE if successful - */ - bool readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize); - - /** Send SMS - @param smsaddr Phone number to send out - @param smsmsg Char array containing message - @return TRUE if successful - */ - bool sendSMS(char *smsaddr, char *smsmsg); - - /** Delete SMS - @param i number of SMS in memory - @return TRUE if successful - */ - bool deleteSMS(uint8_t i); - - /** Send SMS - @param i Number of SMS in memory - @param sender Char array to store the sender number - @param senderlen length of sender - @return TRUE if successful - */ - bool getSMSSender(uint8_t i, char *sender, int senderlen); - - // Time---------------------------------------------------------------------- - /** Enable FONA to sync time with the cellular network - @param onoff on = true, off = false - @return TRUE if successful - */ - bool enableNetworkTimeSync(bool onoff); - - /** Enable FONA to sync time with the time server - @param onoff true = on, false = off - @return TRUE if successful - */ - bool enableNTPTimeSync(bool onoff, const char* ntpserver=0); - - /** Retrieve the current time from the enabled server - @param buff char array to store time value. Given as "yy/MM/dd,hh:mm:ss+zz" - @param maxlen Maximum length of the char array - @return TRUE if successful - */ - bool getTime(char* buff, uint16_t maxlen); - - // GPRS handling---------------------------------------------------------------------- - bool enableGPRS(bool onoff); - uint8_t GPRSstate(void); - bool getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen); - bool getGSMLoc(float *lat, float *lon); - void setGPRSNetworkSettings(const char* apn, const char* username=0, const char* password=0); - - // GPS handling---------------------------------------------------------------------- - bool enableGPS(bool onoff); - int8_t GPSstatus(void); - uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff); - bool getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0); - bool enableGPSNMEA(uint8_t nmea); - - // TCP raw connections---------------------------------------------------------------------- - bool TCPconnect(char *server, uint16_t port); - bool TCPclose(void); - bool TCPconnected(void); - bool TCPsend(char *packet, uint8_t len); - uint16_t TCPavailable(void); - uint16_t TCPread(uint8_t *buff, uint8_t len); - - // HTTP low level interface (maps directly to SIM800 commands).---------------------------------------------------------------------- - bool HTTP_init(); - bool HTTP_term(); - void HTTP_para_start(const char* parameter, bool quoted = true); - bool HTTP_para_end(bool quoted = true); - bool HTTP_para(const char* parameter, const char *value); - bool HTTP_para(const char* parameter, int32_t value); - bool HTTP_data(uint32_t size, uint32_t maxTime=10000); - bool HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 30000); - bool HTTP_readall(uint16_t *datalen); - bool HTTP_ssl(bool onoff); - - // HTTP high level interface (easier to use, less flexible).---------------------------------------------------------------------- - bool HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen); - void HTTP_GET_end(void); - bool HTTP_POST_start(char *url, const char* contenttype, const uint8_t *postdata, uint16_t postdatalen, uint16_t *status, uint16_t *datalen); - void HTTP_POST_end(void); - void setUserAgent(const char* useragent); - - // HTTPS---------------------------------------------------------------------- - void setHTTPSRedirect(bool onoff); - - // PWM (buzzer)---------------------------------------------------------------------- - /** Control the buzzer capability of the PWM out on the FONA - @param period of the buzzing cycle (max 2000) - @param duty the duty cycle of the buzzer (0 to 100) - @return TRUE if successful - */ - bool setPWM(uint16_t period, uint8_t duty = 50); - - // Phone calls---------------------------------------------------------------------- - /** Call a phone - @param phonenum a character array of the phone number - @return TRUE if successful - */ - bool callPhone(char *phonenum); - uint8_t getCallStatus(void); - /** Hang up a phone call - */ - bool hangUp(void); - - /** Answer a phone call - */ - bool pickUp(void); - - /** Enable/disable caller ID - @param enable true to enable, false to disable - @return TRUE if successful - */ - bool callerIdNotification(bool enable); - - /** Retrieve the incoming call number - @param phonenum a character array of the phone number calling - @return TRUE if successful - */ - bool incomingCallNumber(char* phonenum); - - // Helper functions to verify responses. - bool expectReply(const char* reply, uint16_t timeout = 10000); - bool sendUSSD(char *ussdmsg, char *ussdbuff, uint16_t maxlen, uint16_t *readlen); - private: - DigitalOut _rstpin; - InterruptIn _ringIndicatorInterruptIn; - - char replybuffer[255]; // the output of getreply(), readline() is the function that changes the replybuffer - char* apn; - char* apnusername; - char* apnpassword; - bool httpsredirect; - char* useragent; - - volatile bool _incomingCall; - EventListener *eventListener; - - - // Circular buffer used to receive serial data from an interruption - int rxBuffer[RX_BUFFER_SIZE + 1]; - volatile int rxBufferInIndex; // Index where new data is added to the buffer - volatile int rxBufferOutIndex; // Index where data is removed from the buffer - char currentReceivedLine[RX_BUFFER_SIZE]; // Array containing the current received line - int currentReceivedLineSize; - - inline bool isRxBufferFull() { - return ((rxBufferInIndex + 1) % RX_BUFFER_SIZE) == rxBufferOutIndex; - } - - inline bool isRxBufferEmpty() { - return rxBufferInIndex == rxBufferOutIndex; - } - - inline void incrementRxBufferInIndex() { - rxBufferInIndex = (rxBufferInIndex + 1) % RX_BUFFER_SIZE; - } - - inline void incrementRxBufferOutIndex() { - rxBufferOutIndex = (rxBufferOutIndex + 1) % RX_BUFFER_SIZE; - } - - /** - * Method called when Serial data is received (interrupt routine). - */ - void onSerialDataReceived(); - - // HTTP helpers - bool HTTP_setup(char *url); - - void flushInput(); - uint16_t readRaw(uint16_t b); - uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, bool multiline = false); - uint8_t getReply(const char* send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - uint8_t getReply(const char* prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - uint8_t getReply(const char* prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - uint8_t getReply(const char* prefix, int32_t suffix1, int32_t suffix2, uint16_t timeout); // Don't set default value or else function call is ambiguous. - uint8_t getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - - bool sendCheckReply(const char* send, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - bool sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - bool sendCheckReply(const char* prefix, int32_t suffix, int32_t suffix2, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - bool sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); - - bool parseReply(const char* toreply, uint16_t *v, char divider = ',', uint8_t index=0); - bool parseReply(const char* toreply, char *v, char divider = ',', uint8_t index=0); - bool parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index); - - bool sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider = ',', uint8_t index = 0); - - void onIncomingCall(); -}; - -#endif
diff -r 06dd156420a5 -r 7af85396d035 main.cpp --- a/main.cpp Fri Jan 27 00:22:31 2017 +0000 +++ b/main.cpp Wed Feb 28 00:11:36 2018 +0000 @@ -1,784 +1,39 @@ #include "mbed.h" -#include <ctype.h> -#include "Adafruit_FONA.h" -#define BAUD_RATE 115200 -#define FONA_RST D4 -#define FONA_TX PC_10 -#define FONA_RX PC_11 -#define FONA_RI D6 - -#define APN "epc.tmobile.com" -#define USERNAME "" -#define PASSWORD "" - +#if !DEVICE_ANALOGOUT +#error You cannot use this example as the AnalogOut is not supported on this device. +#else +AnalogOut my_output(PA_4); +#endif - -// 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); - -// 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 flushSerial(); -char readBlocking(); -uint16_t readnumber(); -long map(long x, long in_min, long in_max, long out_min, long out_max); - +#define PI (3.141592653589793238462) +#define AMPLITUDE (1.0) // x * 3.3V +#define PHASE (PI * 1) // 2*pi is one period +#define RANGE (0x7FFF) +#define OFFSET (0x7FFF) + +// Configuration for sinewave output +#define BUFFER_SIZE (360) +uint16_t buffer[BUFFER_SIZE]; + +void calculate_sinewave(void); + int main() { - pcSerial.baud(BAUD_RATE); - wait(1); - pcSerial.printf("\r\n"); - - pcSerial.printf("FONA basic test\r\n"); - pcSerial.printf("Initializing....(May take 3 seconds)\r\n"); - - // See if the FONA is responding - if (! fona.begin(BAUD_RATE)) { - pcSerial.printf("Couldn't find FONA\r\n"); - while (1); - } - fona.setEventListener(&fonaEventListener); - pcSerial.printf("FONA is OK\r\n"); - - // 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) { - pcSerial.printf("SIM card IMEI: %s\r\n", imei); - } - fona.setGPRSNetworkSettings(APN, USERNAME, PASSWORD); - // 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(); - - while (true) { - pcSerial.printf("FONA> "); - // if nothing is available on the pcSearial port but there is something on the fona serial, then print it (flush it) to the pc Serial - while (! pcSerial.readable() ) { - if (fona.readable()) { - pcSerial.putc(fona.getc()); - } - } - - // get the input command from the terminal - char command = pcSerial.getc(); - pcSerial.printf("%c\r\n", command); //loops back to COM port - - - switch (command) { - case '?': { - printMenu(); - break; - } - - case 'a': { - // read the ADC - uint16_t adc; - if (! fona.getADCVoltage(&adc)) { - pcSerial.printf("Failed to read ADC\r\n"); - } else { - pcSerial.printf("ADC = %d mV\r\n", adc); - } - break; - } - - case 'b': { - // read the battery voltage and percentage - uint16_t vbat; - if (! fona.getBattVoltage(&vbat)) { - pcSerial.printf("Failed to read Batt\r\n"); - } else { - pcSerial.printf("VBat = %d mV\r\n", vbat); - } - - if (! fona.getBattPercent(&vbat)) { - pcSerial.printf("Failed to read Batt\r\n"); - } else { - pcSerial.printf("VPct = %d%%\r\n", vbat); - } - - break; - } - - case 'U': { - // Unlock the SIM with a PIN code - char PIN[5]; - flushSerial(); - pcSerial.printf("Enter 4-digit PIN\r\n"); - readline(PIN, 3); - pcSerial.printf("%s\r\n", PIN); - pcSerial.printf("Unlocking SIM card: "); - if (! fona.unlockSIM(PIN)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - - case 'C': { - // read the CCID - fona.getSIMCCID(replybuffer); // make sure replybuffer is at least 21 bytes! - pcSerial.printf("SIM CCID = %s\r\n", replybuffer); - break; - } - - case 'i': { - // read the RSSI - uint8_t n = fona.getRSSI(); - int8_t r = 0; - - pcSerial.printf("RSSI = %d: ", n); - if (n == 0) r = -115; - if (n == 1) r = -111; - if (n == 31) r = -52; - if ((n >= 2) && (n <= 30)) { - r = map(n, 2, 30, -110, -54); - } - pcSerial.printf("%d dBm\r\n", r); - - break; - } - - case 'n': { - // read the network/cellular status - uint8_t n = fona.getNetworkStatus(); - pcSerial.printf("Network status %d: ", n); - if (n == 0) pcSerial.printf("Not registered\r\n"); - if (n == 1) pcSerial.printf("Registered (home)\r\n"); - if (n == 2) pcSerial.printf("Not registered (searching)\r\n"); - if (n == 3) pcSerial.printf("Denied\r\n"); - if (n == 4) pcSerial.printf("Unknown\r\n"); - if (n == 5) pcSerial.printf("Registered roaming\r\n"); - break; - } - - /*** Audio ***/ - case 'v': { - // set volume - flushSerial(); - pcSerial.printf("Set Vol %%"); - uint8_t vol = readnumber(); - pcSerial.printf("\r\n"); - if (! fona.setVolume(vol)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - - case 'V': { - uint8_t v = fona.getVolume(); - pcSerial.printf("%d%%\r\n", v); - - break; - } - - case 'H': { - // Set Headphone output - if (! fona.setAudio(FONA_HEADSETAUDIO)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - fona.setMicVolume(FONA_HEADSETAUDIO, 15); - break; - } - case 'e': { - // Set External output - if (! fona.setAudio(FONA_EXTAUDIO)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - - fona.setMicVolume(FONA_EXTAUDIO, 10); - break; - } - - case 'T': { - // play tone - flushSerial(); - pcSerial.printf("Play tone #"); - uint8_t kittone = readnumber(); - pcSerial.printf("\r\n"); - // play for 1 second (1000 ms) - if (! fona.playToolkitTone(kittone, 1000)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - - /*** FM Radio ***/ - - case 'f': { - // get freq - flushSerial(); - 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"); - } - break; - } - case 'F': { - // FM radio off - if (! fona.FMradio(false)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - case 'm': { - // Set FM volume. - flushSerial(); - pcSerial.printf("Set FM Vol [0-6]:"); - uint8_t vol = readnumber(); - pcSerial.printf("\r\n"); - if (!fona.setFMVolume(vol)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - case 'M': { - // Get FM volume. - int8_t fmvol = fona.getFMVolume(); - if (fmvol < 0) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("FM volume: %d\r\n", fmvol); - } - break; - } - case 'q': { - // Get FM station signal level (in decibels). - flushSerial(); - pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): "); - uint16_t station = readnumber(); - pcSerial.printf("\r\n"); - int8_t level = fona.getFMSignalLevel(station); - if (level < 0) { - pcSerial.printf("Failed! Make sure FM radio is on (tuned to station).\r\n"); - } else { - pcSerial.printf("Signal level (dB): %d\r\n", level); - } - break; - } - - /*** PWM ***/ - - case 'P': { - // PWM Buzzer output @ 2KHz max - flushSerial(); - pcSerial.printf("PWM Freq, 0 = Off, (1-2000): "); - uint16_t freq = readnumber(); - pcSerial.printf("\r\n"); - if (! fona.setPWM(freq)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - - /*** Call ***/ - case 'c': { - // call a phone! - char number[30]; - flushSerial(); - pcSerial.printf("Call #"); - readline(number, 30); - pcSerial.printf("\r\n"); - pcSerial.printf("Calling %s\r\n", number); - if (!fona.callPhone(number)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("Sent!\r\n"); - } - - break; - } - case 'h': { - // hang up! - if (! fona.hangUp()) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - - case 'p': { - // pick up! - if (! fona.pickUp()) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("OK!\r\n"); - } - break; - } - - /*** SMS ***/ - - case 'N': { - // read the number of SMS's! - int8_t smsnum = fona.getNumSMS(); - if (smsnum < 0) { - pcSerial.printf("Could not read # SMS\r\n"); - } else { - pcSerial.printf("%d SMS's on SIM card!\r\n", smsnum); - } - break; - } - case 'r': { - // read an SMS - flushSerial(); - pcSerial.printf("Read #"); - uint8_t smsn = readnumber(); - pcSerial.printf("\r\nReading SMS #%d\r\n", smsn); - - // Retrieve SMS sender address/phone number. - if (! fona.getSMSSender(smsn, replybuffer, 250)) { - pcSerial.printf("Failed!\r\n"); - break; - } - pcSerial.printf("FROM: %s\r\n", replybuffer); - - // Retrieve SMS value. - uint16_t smslen; - if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len! - pcSerial.printf("Failed!\r\n"); - break; - } - pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen); - pcSerial.printf("%s\r\n", replybuffer); - pcSerial.printf("*****\r\n"); - - break; - } - case 'R': { - // read all SMS - int8_t smsnum = fona.getNumSMS(); - uint16_t smslen; - for (int8_t smsn=1; smsn<=smsnum; smsn++) { - pcSerial.printf("\r\nReading SMS #%d\r\n", smsn); - if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len! - pcSerial.printf("Failed!\r\n"); - break; - } - // if the length is zero, its a special case where the index number is higher - // so increase the max we'll look at! - if (smslen == 0) { - pcSerial.printf("[empty slot]\r\n"); - smsnum++; - continue; - } - - pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen); - pcSerial.printf("%s\r\n", replybuffer); - pcSerial.printf("*****\r\n"); - } - break; - } - - case 'd': { - // delete an SMS - flushSerial(); - pcSerial.printf("Delete #"); - uint8_t smsn = readnumber(); - - pcSerial.printf("\r\nDeleting SMS #%d\r\n", smsn); - if (fona.deleteSMS(smsn)) { - pcSerial.printf("OK!\r\n"); - } else { - pcSerial.printf("Couldn't delete\r\n"); - } - break; - } - - case 's': { - // send an SMS! - char sendto[21], message[141]; - flushSerial(); - pcSerial.printf("Send to #"); - readline(sendto, 20); - pcSerial.printf("%s\r\n", sendto); - pcSerial.printf("Type out one-line message (140 char): "); - readline(message, 140); - pcSerial.printf("%s\r\n", message); - if (!fona.sendSMS(sendto, message)) { - pcSerial.printf("Failed\r\n"); - } else { - pcSerial.printf("Sent!\r\n"); - } - - break; - } - - /*** Time ***/ - - case 'y': { - // enable network time sync - if (!fona.enableNetworkTimeSync(true)) - pcSerial.printf("Failed to enable\r\n"); - break; - } - - case 'Y': { - // enable NTP time sync - if (!fona.enableNTPTimeSync(true, "pool.ntp.org")) - pcSerial.printf("Failed to enable\r\n"); - break; - } - - case 't': { - // read the time - char buffer[23]; - - fona.getTime(buffer, 23); // make sure replybuffer is at least 23 bytes! - pcSerial.printf("Time = %s\r\n", buffer); - break; - } - - /*********************************** GPS (SIM808 only) */ - - case 'o': { - // turn GPS off - if (!fona.enableGPS(false)) - pcSerial.printf("Failed to turn off\r\n"); - break; - } - case 'O': { - // turn GPS on - if (!fona.enableGPS(true)) - pcSerial.printf("Failed to turn on\r\n"); - break; - } - case 'x': { - int8_t stat; - // check GPS fix - stat = fona.GPSstatus(); - if (stat < 0) - pcSerial.printf("Failed to query\r\n"); - if (stat == 0) pcSerial.printf("GPS off\r\n"); - if (stat == 1) pcSerial.printf("No fix\r\n"); - if (stat == 2) pcSerial.printf("2D fix\r\n"); - if (stat == 3) pcSerial.printf("3D fix\r\n"); - break; - } - - case 'L': { - // check for GPS location - char gpsdata[80]; - fona.getGPS(0, gpsdata, 80); - pcSerial.printf("Reply in format: mode,longitude,latitude,altitude,utctime(yyyymmddHHMMSS),ttff,satellites,speed,course\r\n"); - pcSerial.printf("%s\r\n", gpsdata); - - break; - } - - case 'E': { - flushSerial(); - pcSerial.printf("GPS NMEA output sentences (0 = off, 34 = RMC+GGA, 255 = all)\r\n"); - uint8_t nmeaout = readnumber(); - - // turn on NMEA output - fona.enableGPSNMEA(nmeaout); - - break; - } - - /*********************************** GPRS */ - - case 'g': { - // turn GPRS off - if (!fona.enableGPRS(false)) - pcSerial.printf("Failed to turn off\r\n"); - break; - } - case 'G': { - // turn GPRS on - if (!fona.enableGPRS(true)) - pcSerial.printf("Failed to turn on\r\n"); - break; - } - case 'l': { - // check for GSMLOC (requires GPRS) - uint16_t returncode; - - if (!fona.getGSMLoc(&returncode, replybuffer, 250)) - pcSerial.printf("Failed!\r\n"); - if (returncode == 0) { - pcSerial.printf("%s\r\n", replybuffer); - } else { - pcSerial.printf("Fail code #%d\r\n", returncode); - } - - break; - } - case 'w': { - // read website URL - uint16_t statuscode; - int16_t length; - char url[80]; - - flushSerial(); - pcSerial.printf("NOTE: in beta! Use small webpages to read!\r\n"); - pcSerial.printf("URL to read (e.g. www.adafruit.com/testwifi/index.html):\r\n"); - pcSerial.printf("http://"); readline(url, 79); - pcSerial.printf("%s\r\n", url); - - pcSerial.printf("****\r\n"); - if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) { - pcSerial.printf("Failed!\r\n"); - break; - } - while (length > 0) { - while (fona.readable()) { - char c = fona.getc(); - pcSerial.putc(c); - length--; - if (! length) break; - } - } - pcSerial.printf("\r\n****\r\n"); - fona.HTTP_GET_end(); - break; - } - - case 'W': { - // Post data to website - uint16_t statuscode; - int16_t length; - char url[80]; - char data[80]; - - flushSerial(); - pcSerial.printf("NOTE: in beta! Use simple websites to post!\r\n"); - pcSerial.printf("URL to post (e.g. httpbin.org/post):\r\n"); - pcSerial.printf("http://"); readline(url, 79); - pcSerial.printf("%s\r\n", url); - pcSerial.printf("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):\r\n"); - readline(data, 79); - pcSerial.printf("%s\r\n", data); - - pcSerial.printf("****\r\n"); - if (!fona.HTTP_POST_start(url, "text/plain", (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) { - pcSerial.printf("Failed!\r\n"); - break; - } - while (length > 0) { - while (fona.readable()) { - char c = fona.getc(); - pcSerial.putc(c); - length--; - if (! length) break; - } - } - pcSerial.printf("\r\n****\r\n"); - fona.HTTP_POST_end(); - break; - } - /*****************************************/ - - case 'S': { - pcSerial.printf("Creating SERIAL TUBE\r\n"); - while (1) { - while(pcSerial.readable() ){ - fona._putc(pcSerial.getc() ); - } - while(fona.readable() ){ - pcSerial.putc(fona._getc() ); - } - - } - } - - default: { - pcSerial.printf("Unknown command\r\n"); - printMenu(); - break; - } - } - // flush input - flushSerial(); - while (fona.readable()) { - pcSerial.putc(fona.getc()); + printf("Sinewave example\n"); + calculate_sinewave(); + while(1) { + // sinewave output + for (int i = 0; i < BUFFER_SIZE; i++) { + my_output.write_u16(buffer[i]); + wait_us(10); } } } - -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(); + +// Create the sinewave buffer +void calculate_sinewave(void){ + for (int i = 0; i < BUFFER_SIZE; i++) { + double rads = (PI * i)/180.0; // Convert degree in radian + buffer[i] = (uint16_t)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET); + } } - -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; -} - - -
diff -r 06dd156420a5 -r 7af85396d035 mbed-dev.lib --- a/mbed-dev.lib Fri Jan 27 00:22:31 2017 +0000 +++ b/mbed-dev.lib Wed Feb 28 00:11:36 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-dev/#95d6b41a828b +http://mbed.org/users/mbed_official/code/mbed-dev/#57724642e740