64 valueBytes(initialTemp),
71 ble.gattServer().addService(hrmService);
82 valueBytes.updateTemperature(temperature);
83 ble.gattServer().write(tempMeasurement.
getValueHandle(), valueBytes.getPointer(),
sizeof(TemperatureValueBytes));
92 ble.gattServer().write(tempLocation.
getValueHandle(),
reinterpret_cast<uint8_t *
>(&loc),
sizeof(uint8_t));
97 struct TemperatureValueBytes {
98 static const unsigned OFFSET_OF_FLAGS = 0;
99 static const unsigned OFFSET_OF_VALUE = OFFSET_OF_FLAGS +
sizeof(uint8_t);
100 static const unsigned SIZEOF_VALUE_BYTES =
sizeof(uint8_t) +
sizeof(
float);
102 static const unsigned TEMPERATURE_UNITS_FLAG_POS = 0;
103 static const unsigned TIMESTAMP_FLAG_POS = 1;
104 static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2;
106 static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
107 static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
109 TemperatureValueBytes(
float initialTemperature) : bytes() {
111 bytes[OFFSET_OF_FLAGS] = (TEMPERATURE_UNITS_CELSIUS << TEMPERATURE_UNITS_FLAG_POS) |
112 (
false << TIMESTAMP_FLAG_POS) |
113 (
false << TEMPERATURE_TYPE_FLAG_POS);
114 updateTemperature(initialTemperature);
117 void updateTemperature(
float temp) {
118 uint32_t temp_ieee11073 = quick_ieee11073_from_float(temp);
119 memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073,
sizeof(
float));
122 uint8_t *getPointer() {
126 const uint8_t *getPointer()
const {
136 static uint32_t quick_ieee11073_from_float(
float temperature) {
137 uint8_t exponent = 0xFE;
138 uint32_t mantissa = (uint32_t)(temperature * 100);
140 return (((uint32_t)exponent) << 24) | mantissa;
146 uint8_t bytes[SIZEOF_VALUE_BYTES];
151 TemperatureValueBytes valueBytes;