Extracted BLE advertisement packet building logic to BLEAdvertisementData
This commit is contained in:
parent
30aa1168fa
commit
8d01cd3b7e
3 changed files with 61 additions and 25 deletions
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include "ble_advertisement_data.h"
|
||||||
|
|
||||||
|
namespace parasite {
|
||||||
|
|
||||||
|
void BLEAdvertisementData::SetRawSoilMoisture(int raw_soil_moisture) {
|
||||||
|
uint16_t packed_value = raw_soil_moisture;
|
||||||
|
data_[kRawSoilMoistureOffset] = packed_value >> 8;
|
||||||
|
data_[kRawSoilMoistureOffset + 1] = packed_value & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
46
code/parasite/lib/parasite/parasite/ble_advertisement_data.h
Normal file
46
code/parasite/lib/parasite/parasite/ble_advertisement_data.h
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
#ifndef _PARASITE_BLE_ADVERTISEMENT_DATA_H_
|
||||||
|
#define _PARASITE_BLE_ADVERTISEMENT_DATA_H_
|
||||||
|
|
||||||
|
// #include <string>
|
||||||
|
#include <ble_gap.h>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace parasite {
|
||||||
|
|
||||||
|
constexpr size_t kAdvertisementDataLen = 18;
|
||||||
|
constexpr size_t kRawSoilMoistureOffset = 14;
|
||||||
|
constexpr size_t kPercentSoilMoistureOffset = kRawSoilMoistureOffset + 2;
|
||||||
|
constexpr size_t kTemperatureOfsset = kRawSoilMoistureOffset + 4;
|
||||||
|
|
||||||
|
class BLEAdvertisementData {
|
||||||
|
public:
|
||||||
|
void SetRawSoilMoisture(int raw_soil_moisture);
|
||||||
|
const uint8_t* GetRawData() const { return data_; }
|
||||||
|
size_t GetDataLen() const { return kAdvertisementDataLen; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t data_[kAdvertisementDataLen] = {
|
||||||
|
9, // Length of name.
|
||||||
|
BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
|
||||||
|
'P',
|
||||||
|
'a',
|
||||||
|
'r',
|
||||||
|
'a',
|
||||||
|
's',
|
||||||
|
'i',
|
||||||
|
't',
|
||||||
|
'e',
|
||||||
|
7, // Length of the service data.
|
||||||
|
BLE_GAP_AD_TYPE_SERVICE_DATA,
|
||||||
|
0x1a,
|
||||||
|
0x18, // Environment sensor service UUID.
|
||||||
|
0x00,
|
||||||
|
0x00, // Raw soil humidity.
|
||||||
|
0x00,
|
||||||
|
0x00, // Percentage soil humidity.
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace parasite
|
||||||
|
#endif // _PARASITE_BLE_ADVERTISEMENT_DATA_H_
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "parasite/ble.h"
|
#include "parasite/ble.h"
|
||||||
|
#include "parasite/ble_advertisement_data.h"
|
||||||
#include "parasite/pwm.h"
|
#include "parasite/pwm.h"
|
||||||
|
|
||||||
constexpr int kLED1Pin = 17;
|
constexpr int kLED1Pin = 17;
|
||||||
|
|
@ -13,23 +14,6 @@ constexpr int kSensAnalogPin = 4; // AIN2
|
||||||
constexpr int kDischargeEnablePin = 16;
|
constexpr int kDischargeEnablePin = 16;
|
||||||
constexpr double kPWMFrequency = 500000;
|
constexpr double kPWMFrequency = 500000;
|
||||||
|
|
||||||
constexpr int kManufacturerDataLen = 3;
|
|
||||||
|
|
||||||
// We can have at most 31 bytes here.
|
|
||||||
uint8_t advertisement_data[] = {
|
|
||||||
9, // Length of name.
|
|
||||||
BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
|
|
||||||
'P', 'a', 'r', 'a', 's', 'i', 't', 'e',
|
|
||||||
7, // Length of the service data.
|
|
||||||
BLE_GAP_AD_TYPE_SERVICE_DATA,
|
|
||||||
0x1a, 0x18, // Environment sensor service UUID.
|
|
||||||
0x00, 0x00, // Raw soil humidity.
|
|
||||||
0x00, 0x00, // Percentage soil humidity.
|
|
||||||
};
|
|
||||||
constexpr size_t kRawSoilMoistureOffset = 14;
|
|
||||||
constexpr size_t kPercentSoilMoistureOffset = kRawSoilMoistureOffset + 2;
|
|
||||||
constexpr size_t kTemperatureOfsset = kRawSoilMoistureOffset + 4;
|
|
||||||
|
|
||||||
ble_gap_addr_t kGAPAddr{
|
ble_gap_addr_t kGAPAddr{
|
||||||
1,
|
1,
|
||||||
BLE_GAP_ADDR_TYPE_PUBLIC,
|
BLE_GAP_ADDR_TYPE_PUBLIC,
|
||||||
|
|
@ -45,16 +29,11 @@ void setupAdvertising() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateAdvertisingData(int moisture_level) {
|
void updateAdvertisingData(int moisture_level) {
|
||||||
uint16_t packed_raw_moisture = moisture_level;
|
parasite::BLEAdvertisementData data;
|
||||||
advertisement_data[kRawSoilMoistureOffset] = packed_raw_moisture >> 8;
|
data.SetRawSoilMoisture(moisture_level);
|
||||||
advertisement_data[kRawSoilMoistureOffset + 1] = packed_raw_moisture & 0xff;
|
|
||||||
// manufacturer_data[1] = 0xff;
|
|
||||||
Bluefruit.Advertising.stop();
|
Bluefruit.Advertising.stop();
|
||||||
Bluefruit.Advertising.clearData();
|
Bluefruit.Advertising.clearData();
|
||||||
// Bluefruit.Advertising.addName();
|
Bluefruit.Advertising.setData(data.GetRawData(), data.GetDataLen());
|
||||||
// Bluefruit.Advertising.addManufacturerData(manufacturer_data,
|
|
||||||
// kManufacturerDataLen);
|
|
||||||
Bluefruit.Advertising.setData(advertisement_data, sizeof(advertisement_data));
|
|
||||||
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||||
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||||
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
|
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue