Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c6517e4

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 5653eb6 commit c6517e4

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(reinterpret_cast<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(reinterpret_cast<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(reinterpret_cast<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(reinterpret_cast<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(reinterpret_cast<uint8_t *>(&data64), sizeof(data64));
411396
} // setValue
412397

413398
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /