Skip to content

Commit

Permalink
feat: add function to read BL mac address
Browse files Browse the repository at this point in the history
  • Loading branch information
felixerdy committed Dec 7, 2023
1 parent c255ce1 commit 145f952
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
48 changes: 43 additions & 5 deletions src/NINAB31serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool NINAB31Serial::begin(){
}

bool NINAB31Serial::setLocalName(String name){
if(name.length()>29){
if(name.length()>32){
return false;
}
return checkResponse(String("AT+UBTLN=\"")+name+"\"", 1000);
Expand Down Expand Up @@ -126,8 +126,46 @@ int NINAB31Serial::parseResponse(String cmd, uint32_t timeout){
return -1;
}

String NINAB31Serial::parseStringResponse(String cmd, uint32_t timeout){
while(SerialBLE.available()){
(SerialBLE.read());
}
SerialBLE.print(cmd);
SerialBLE.write('\r');
SerialBLE.flush();
String input="";
auto starttime=millis();
while(millis()-starttime<timeout || timeout==0){
if(SerialBLE.available()){
input+=(char)(SerialBLE.read());
if(input.endsWith("ERROR\r")){
//Serial.println(String("error with command ")+cmd);
return "-1";
}
if(input.endsWith("OK\r")){
int colonpos=input.indexOf(':');
int commapos=input.indexOf(',');
if(colonpos!=-1){
if(commapos!=-1){
return input.substring(colonpos+1,commapos);
}else{
String result = input.substring(colonpos+1);
int returnpos = result.indexOf('\r');
if (returnpos != -1) {
return result.substring(0, returnpos);
}
return result;
}
}
return "-1";
}
}
}
return "-1";
}

bool NINAB31Serial::checkResponse(String msg, uint32_t timeout){
while(SerialBLE.available())SerialBLE.read(); //flush input buffer
while(SerialBLE.available())SerialBLE.read(); //flush input buffer
SerialBLE.print(msg);
SerialBLE.write('\r');
SerialBLE.flush();
Expand All @@ -145,7 +183,7 @@ bool NINAB31Serial::checkResponse(String msg, uint32_t timeout){
}
}
return false;

}

bool NINAB31Serial::checkUnsolicited(){
Expand Down Expand Up @@ -186,12 +224,12 @@ String NINAB31Serial::checkCharWritten(int handle){
return m_input.substring(seccommapos+1,thirdcommapos);
}
}

}else{
//Serial.println(m_input);
}
return "";

}

void NINAB31Serial::flushInput(){
Expand Down
5 changes: 3 additions & 2 deletions src/NINAB31serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class NINAB31Serial
{

private:

static String m_input;
Expand All @@ -22,13 +22,14 @@ class NINAB31Serial
static bool setConnectionInterval(int minInterval, int maxInterval);

static int parseResponse(String msg, uint32_t timeout);
static String parseStringResponse(String msg, uint32_t timeout);
static bool checkResponse(String msg, uint32_t timeout);
static bool poll();
static String checkCharWritten(int handle);
static void flushInput();
static bool advertise();
static bool stopAdvertise();

};

#endif
8 changes: 8 additions & 0 deletions src/SenseBoxBLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ int SenseBoxBLE::addService(const char* serviceUUID){
return port.parseResponse(String("AT+UBTGSER=")+serviceUUID,1000);
}

void SenseBoxBLE::setName(String name){
port.setLocalName(name);
}

String SenseBoxBLE::getMCUId(){
return port.parseStringResponse("AT+UMLA=1",1000);
}

/**
* @brief Adds a characteristic to the BLE module.
*
Expand Down
2 changes: 2 additions & 0 deletions src/SenseBoxBLE.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class SenseBoxBLE
static int addService(const char* serviceUUID);
static int addCharacteristic(const char* characteristicUUID);
static int setConfigCharacteristic(const char* serviceUUID, const char* characteristicUUID);
static void setName(String name);
static String getMCUId();

static bool write(int, float&);
static bool write(int, float&, float&);
Expand Down

0 comments on commit 145f952

Please sign in to comment.