8000 Common code style by toxuin · Pull Request #691 · nkolban/esp32-snippets · GitHub
[go: up one dir, main page]

Skip to content

Common code style #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Common code style
  • Loading branch information
toxuin committed Oct 14, 2018
commit b17e34656bc5e712096ec1e2a3bd2198f23f9641
2 changes: 1 addition & 1 deletion cpp_utils/Apa102.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Apa102::show() {

double brigthnessScale = getBrightness() / 100.0;
// Loop over all the pixels in the pixels array to set the colors.
for (int i=0; i<m_pixelCount; i++) {
for (uint16_t i = 0; i < m_pixelCount; i++) {
mySPI.transferByte(0xff); // Maximum brightness
mySPI.transferByte(m_pixels[i].blue * brigthnessScale);
mySPI.transferByte(m_pixels[i].green * brigthnessScale);
Expand Down
17 changes: 5 additions & 12 deletions cpp_utils/BLE2902.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "BLE2902.h"

BLE2902::BLE2902() : BLEDescriptor(BLEUUID((uint16_t) 0x2902)) {
uint8_t data[2] = {0,0};
uint8_t data[2] = { 0, 0 };
setValue(data, 2);
} // BLE2902

Expand Down Expand Up @@ -44,11 +44,8 @@ bool BLE2902::getIndications() {
*/
void BLE2902::setIndications(bool flag) {
uint8_t *pValue = getValue();
if (flag) {
pValue[0] |= 1<<1;
} else {
pValue[0] &= ~(1<<1);
}
if (flag) pValue[0] |= 1 << 1;
else pValue[0] &= ~(1 << 1);
} // setIndications


Expand All @@ -58,12 +55,8 @@ void BLE2902::setIndications(bool flag) {
*/
void BLE2902::setNotifications(bool flag) {
uint8_t *pValue = getValue();
if (flag) {
pValue[0] |= 1<<0;
} else {
pValue[0] &= ~(1<<0);
}
if (flag) pValue[0] |= 1 << 0;
else pValue[0] &= ~(1 << 0);
} // setNotifications


#endif
14 changes: 7 additions & 7 deletions cpp_utils/BLE2904.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ BLE2904::BLE2904() : BLEDescriptor(BLEUUID((uint16_t) 0x2904)) {
m_data.m_namespace = 1; // 1 = Bluetooth SIG Assigned Numbers
m_data.m_unit = 0;
m_data.m_description = 0;
setValue((uint8_t*)&m_data, sizeof(m_data));
setValue((uint8_t*) &m_data, sizeof(m_data));
} // BLE2902


Expand All @@ -30,7 +30,7 @@ BLE2904::BLE2904() : BLEDescriptor(BLEUUID((uint16_t) 0x2904)) {
*/
void BLE2904::setDescription(uint16_t description) {
m_data.m_description = description;
setValue((uint8_t*)&m_data, sizeof(m_data));
setValue((uint8_t*) &m_data, sizeof(m_data));
}


Expand All @@ -39,7 +39,7 @@ void BLE2904::setDescription(uint16_t description) {
*/
void BLE2904::setExponent(int8_t exponent) {
m_data.m_exponent = exponent;
setValue((uint8_t*)&m_data, sizeof(m_data));
setValue((uint8_t*) &m_data, sizeof(m_data));
} // setExponent


Expand All @@ -48,7 +48,7 @@ void BLE2904::setExponent(int8_t exponent) {
*/
void BLE2904::setFormat(uint8_t format) {
m_data.m_format = format;
setValue((uint8_t*)&m_data, sizeof(m_data));
setValue((uint8_t*) &m_data, sizeof(m_data));
} // setFormat


Expand All @@ -57,18 +57,18 @@ void BLE2904::setFormat(uint8_t format) {
*/
void BLE2904::setNamespace(uint8_t namespace_value) {
m_data.m_namespace = namespace_value;
setValue((uint8_t*)&m_data, sizeof(m_data));
setValue((uint8_t*) &m_data, sizeof(m_data));
} // setNamespace


/**
* @brief Set the units for this value. It should be one of the encoded values defined here:
* https://www.bluetooth.com/specifications/assigned-numbers/units
* @param [in] uint The type of units of this characteristic as defined by assigned numbers.
* @param [in] unit The type of units of this characteristic as defined by assigned numbers.
*/
void BLE2904::setUnit(uint16_t unit) {
m_data.m_unit = unit;
setValue((uint8_t*)&m_data, sizeof(m_data));
setValue((uint8_t*) &m_data, sizeof(m_data));
} // setUnit

#endif
29 changes: 14 additions & 15 deletions cpp_utils/BLEAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ BLEAddress::BLEAddress(esp_bd_addr_t address) {
* @param [in] stringAddress The hex representation of the address.
*/
BLEAddress::BLEAddress(std::string stringAddress) {
if (stringAddress.length() != 17) {
return;
}
if (stringAddress.length() != 17) return;

int data[6];
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
m_address[0] = (uint8_t)data[0];
m_address[1] = (uint8_t)data[1];
m_address[2] = (uint8_t)data[2];
m_address[3] = (uint8_t)data[3];
m_address[4] = (uint8_t)data[4];
m_address[5] = (uint8_t)data[5];
m_address[0] = (uint8_t) data[0];
m_address[1] = (uint8_t) data[1];
m_address[2] = (uint8_t) data[2];
m_address[3] = (uint8_t) data[3];
m_address[4] = (uint8_t) data[4];
m_address[5] = (uint8_t) data[5];
} // BLEAddress


Expand Down Expand Up @@ -85,12 +84,12 @@ esp_bd_addr_t *BLEAddress::getNative() {
*/
std::string BLEAddress::toString() {
std::stringstream stream;
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[0] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[1] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[2] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[3] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[4] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[5];
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[0] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[1] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[2] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[3] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[4] << ':';
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[5];
return stream.str();
} // toString
#endif
32 changes: 15 additions & 17 deletions cpp_utils/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ BLEUUID BLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventually, is
*/
bool BLEAdvertisedDevice::isAdvertisingService(BLEUUID uuid){
for (int i = 0; i < m_serviceUUIDs.size(); i++) {
if(m_serviceUUIDs[i].equals(uuid))
return true;
if (m_serviceUUIDs[i].equals(uuid)) return true;
}
return false;
}
Expand Down Expand Up @@ -236,9 +235,9 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
bool finished = false;
setPayload(payload);

while(!finished) {
length = *payload; // Retrieve the length of the record.
payload++; // Skip to type
while (!finished) {
sizeConsumed += 1 + length; // increase the size consumed.

if (length != 0) { // A length of 0 indicates that we have reached the end.
Expand All @@ -251,7 +250,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
ad_type, BLEUtils::advTypeToString(ad_type), length, pHex);
free(pHex);

switch(ad_type) {
switch (ad_type) {
case ESP_BLE_AD_TYPE_NAME_CMPL: { // Adv Data Type: 0x09
setName(std::string(reinterpret_cast<char*>(payload), length));
break;
Expand All @@ -274,16 +273,16 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {

case ESP_BLE_AD_TYPE_16SRV_CMPL:
case ESP_BLE_AD_TYPE_16SRV_PART: { // Adv Data Type: 0x02
for (int var = 0; var < length/2; ++var) {
setServiceUUID(BLEUUID(*reinterpret_cast<uint16_t*>(payload+var*2)));
for (int var = 0; var < length / 2; ++var) {
setServiceUUID(BLEUUID(*reinterpret_cast<uint16_t*>(payload + var * 2)));
}
break;
} // ESP_BLE_AD_TYPE_16SRV_PART

case ESP_BLE_AD_TYPE_32SRV_CMPL:
case ESP_BLE_AD_TYPE_32SRV_PART: { // Adv Data Type: 0x04
for (int var = 0; var < length/4; ++var) {
setServiceUUID(BLEUUID(*reinterpret_cast<uint32_t*>(payload+var*4)));
for (int var = 0; var < length / 4; ++var) {
setServiceUUID(BLEUUID(*reinterpret_cast<uint32_t*>(payload + var * 4)));
}
break;
} // ESP_BLE_AD_TYPE_32SRV_PART
Expand All @@ -309,10 +308,10 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_SERVICE_DATA");
break;
}
uint16_t uuid = *(uint16_t *)payload;
uint16_t uuid = *(uint16_t*) payload;
setServiceDataUUID(BLEUUID(uuid));
if (length > 2) {
setServiceData(std::string(reinterpret_cast<char*>(payload+2), length-2));
setServiceData(std::string(reinterpret_cast<char*>(payload + 2), length - 2));
}
break;
} //ESP_BLE_AD_TYPE_SERVICE_DATA
Expand All @@ -322,10 +321,10 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
ESP_LOGE(LOG_TAG, "Length too small for ESP_BLE_AD_TYPE_32SERVICE_DATA");
break;
}
uint32_t uuid = *(uint32_t *)payload;
uint32_t uuid = *(uint32_t*) payload;
setServiceDataUUID(BLEUUID(uuid));
if (length > 4) {
setServiceData(std::string(reinterpret_cast<char*>(payload+4), length-4));
setServiceData(std::string(reinterpret_cast<char*>(payload + 4), length - 4));
}
break;
} //ESP_BLE_AD_TYPE_32SERVICE_DATA
Expand All @@ -336,9 +335,9 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
break;
}

setServiceDataUUID(BLEUUID(payload, (size_t)16, false));
setServiceDataUUID(BLEUUID(payload, (size_t) 16, false));
if (length > 16) {
setServiceData(std::string(reinterpret_cast<char*>(payload+16), length-16));
setServiceData(std::string(reinterpret_cast<char*>(payload + 16), length - 16));
}
break;
} //ESP_BLE_AD_TYPE_32SERVICE_DATA
Expand All @@ -352,7 +351,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
} // Length <> 0


if (sizeConsumed >=31 || length == 0) {
if (sizeConsumed >= 31 || length == 0) {
finished = true;
}
} // !finished
Expand Down Expand Up @@ -395,7 +394,7 @@ void BLEAdvertisedDevice::setAppearance(uint16_t appearance) {
void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
m_manufacturerData = manufacturerData;
m_haveManufacturerData = true;
char* pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)m_manufacturerData.data(), (uint8_t)m_manufacturerData.length());
char* pHex = BLEUtils::buildHexData(nullptr, (uint8_t*) m_manufacturerData.data(), (uint8_t) m_manufacturerData.length());
ESP_LOGD(LOG_TAG, "- manufacturer data: %s", pHex);
free(pHex);
} // setManufacturerData
Expand Down Expand Up @@ -517,4 +516,3 @@ void BLEAdvertisedDevice::setPayload(uint8_t* payload) {


#endif /* CONFIG_BT_ENABLED */

3 changes: 1 addition & 2 deletions cpp_utils/BLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class BLEAdvertisedDevice {
void setTXPower(int8_t txPower);
void setPayload(uint8_t* payload);


bool m_haveAppearance;
bool m_haveManufacturerData;
bool m_haveName;
Expand All @@ -82,7 +81,6 @@ class BLEAdvertisedDevice {
bool m_haveTXPower;


BLEAddress m_address = BLEAddress((uint8_t*)"\0\0\0\0\0\0");
uint8_t m_adFlag;
uint16_t m_appearance;
int m_deviceType;
Expand All @@ -95,6 +93,7 @@ class BLEAdvertisedDevice {
std::string m_serviceData;
BLEUUID m_serviceDataUUID;
uint8_t* m_payload;
BLEAddress m_address = BLEAddress((uint8_t*) "\0\0\0\0\0\0");
};

/**
Expand Down
Loading
0