// File: JDY08.c //------------------------------------------------------------------------------ // Author: Giovanni de Sanctis // Email: info@lateral-technologies.com //------------------------------------------------------------------------------ // Date: Dec 2018 // HAL for the JDY-08 Bluetooth module (based on the TI chip CC2541) //------------------------------------------------------------------------------ #include "JDY08.h" #include "Definitions.h" #include "./mcc_generated_files/mcc.h" #include #include //AT commands #define HOSTEN "AT+HOSTEN0" //Sets the right mode for BT serial slave #define RST "AT+RST" //Resets the BT module #define NAME "AT+NAME(!)Tail1" //Sets the BT name #define REVERSE "AT+REVERSE1" // #define SPLASH "Tail sw.ver."STR_VER //Sent after initialisation char rxBuff[RX_BUFF_LEN]; //rx buffer uint8_t rxRdPtr; //Reading Pointer to rxBuff //Return the pointer to the buffer read, or a NULL pointer if no chars were received char* btReadStr() { char c; //temp char uint8_t i; //iterator uint8_t rxPtr; //index to the rx buffer //Flush buffer memset(rxBuff,0,sizeof(rxBuff)); rxPtr=0; //If no data received then return if (!EUSART_is_rx_ready()) return 0; while (EUSART_is_rx_ready()) //Keeps reading until there is data { c=EUSART_Read(); rxBuff[rxPtr++]=c; if (c=='\n') break; if (rxPtr>sizeof(rxBuff)-1) break; } rxBuff[rxPtr]=0; //Terminates the string rxRdPtr = 0; //Resets the buffer read pointer //Convert to uppercase for (i=0; (rxBuff[i]!=0 && i='a' && c<='z') rxBuff[i]=c-('a'-'A'); } if (rxPtr) return rxBuff; //Return pointer to string if not empty else return 0; //A null pointer means nothing was received } void btSendStr(const char* str,uint8_t len) { uint8_t i; if (len==0) len=strlen(str);//+1; //if len=0 set len to strlen (+terminator) for (i=0; i0 if rx string contains the string passed //Must be called after btReadStr() //This is used to seek for a command string uint8_t btRxStrContains(const char* str) { char* currPtr = strstr(&rxBuff[rxRdPtr],str); if (currPtr) { rxRdPtr+=strlen(str); //Move pointer past the recognised substring if (rxRdPtr>=sizeof(rxBuff)) rxRdPtr=sizeof(rxBuff); return 1; } else currPtr=rxBuff; return 0; } //Reads the next numerical value (must be preceded by the specified char) //Must be called after btReadStr() uint8_t btRxValue(uint16_t * const val,char * const type,const uint16_t maxVal) { char c; uint8_t valOK = 0; *type = 0; *val = 0; while (rxRdPtr='A' && c<='Z') { *type = c; *val = 0; valOK = 0; } else if (*type && c>='0' && c<='9') { if (*val