Skip to content

Commit 05c13c0

Browse files
committed
refactor(BLE): simplify setValue() for primitive types
ESP32 is little-endian architecture, so the code that filled temporary arrays created from integers basically did nothing. Pass primitives as 'buffers' to generic `setValue()` instead. Also use sizeof() instead of hardcoded sizes as the argument.
1 parent 8e38105 commit 05c13c0

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

libraries/BLE/src/BLECharacteristic.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,38 +376,23 @@ void BLECharacteristic::setValue(String value) {
376376
} // setValue
377377

378378
void BLECharacteristic::setValue(uint16_t data16) {
379-
uint8_t temp[2];
380-
temp[0] = data16;
381-
temp[1] = data16 >> 8;
382-
setValue(temp, 2);
379+
setValue((uint8_t*)(&data16), sizeof(data16));
383380
} // setValue
384381

385382
void BLECharacteristic::setValue(uint32_t data32) {
386-
uint8_t temp[4];
387-
temp[0] = data32;
388-
temp[1] = data32 >> 8;
389-
temp[2] = data32 >> 16;
390-
temp[3] = data32 >> 24;
391-
setValue(temp, 4);
383+
setValue((uint8_t*)(&data32), sizeof(data32));
392384
} // setValue
393385

394386
void BLECharacteristic::setValue(int data32) {
395-
uint8_t temp[4];
396-
temp[0] = data32;
397-
temp[1] = data32 >> 8;
398-
temp[2] = data32 >> 16;
399-
temp[3] = data32 >> 24;
400-
setValue(temp, 4);
387+
setValue((uint8_t*)(&data32), sizeof(data32));
401388
} // setValue
402389

403390
void BLECharacteristic::setValue(float data32) {
404-
float temp = data32;
405-
setValue((uint8_t *)&temp, 4);
391+
setValue((uint8_t*)(&data32), sizeof(data32));
406392
} // setValue
407393

408394
void BLECharacteristic::setValue(double data64) {
409-
double temp = data64;
410-
setValue((uint8_t *)&temp, 8);
395+
setValue((uint8_t*)(&data64), sizeof(data64));
411396
} // setValue
412397

413398
/**

0 commit comments

Comments
 (0)