|
| 1 | +#include "LowPower.h" |
| 2 | +#include <arm_math.h> |
| 3 | + |
| 4 | +#include "config.h" |
| 5 | +#include <PDM.h> |
| 6 | + |
| 7 | +#include <ArduinoBLE.h> |
| 8 | + |
| 9 | +const int VERSION = 0x00000001; |
| 10 | + |
| 11 | +#define SCIENCE_KIT_UUID(val) ("555a0002-" val "-467a-9538-01f0652c74e8") |
| 12 | + |
| 13 | +//#define DEBUG 0 |
| 14 | + |
| 15 | +BLEService service (SCIENCE_KIT_UUID("0000")); |
| 16 | +BLEUnsignedIntCharacteristic versionCharacteristic (SCIENCE_KIT_UUID("0001"), BLERead); |
| 17 | +BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID("0011"), BLENotify, 3 * sizeof(float)); |
| 18 | +BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID("0012"), BLENotify, 3 * sizeof(float)); |
| 19 | +BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("0013"), BLENotify, 3 * sizeof(float)); |
| 20 | +BLEFloatCharacteristic temperatureCharacteristic (SCIENCE_KIT_UUID("0014"), BLENotify); |
| 21 | +BLEFloatCharacteristic pressureCharacteristic (SCIENCE_KIT_UUID("0015"), BLENotify); |
| 22 | +BLEFloatCharacteristic humidityCharacteristic (SCIENCE_KIT_UUID("0016"), BLENotify); |
| 23 | +BLEUnsignedIntCharacteristic proximityCharacteristic (SCIENCE_KIT_UUID("0017"), BLENotify); |
| 24 | +BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int)); |
| 25 | +BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify); |
| 26 | +BLEFloatCharacteristic resistanceCharacteristic (SCIENCE_KIT_UUID("0020"), BLENotify); |
| 27 | + |
| 28 | +byte voltageBufferIndex = 0; |
| 29 | +bool voltageBufferFilled = false; |
| 30 | +short soundSampleBuffer[256]; |
| 31 | + |
| 32 | +void updateSubscribedCharacteristics(); |
| 33 | + |
| 34 | +void onPDMdata() { |
| 35 | + // query the number of bytes available |
| 36 | + int bytesAvailable = PDM.available(); |
| 37 | + |
| 38 | + // read into the sample buffer |
| 39 | + PDM.read(soundSampleBuffer, bytesAvailable); |
| 40 | +} |
| 41 | + |
| 42 | +uint16_t getSoundAverage() { |
| 43 | + uint32_t avg = 0; |
| 44 | + for (int i = 0; i < sizeof(soundSampleBuffer)/sizeof(soundSampleBuffer[0]); i++) { |
| 45 | + avg += soundSampleBuffer[i]*soundSampleBuffer[i]; |
| 46 | + } |
| 47 | + return sqrt(avg); |
| 48 | +} |
| 49 | + |
| 50 | +// String to calculate the local and device name |
| 51 | +String name; |
| 52 | +unsigned long lastNotify = 0; |
| 53 | + |
| 54 | +void printSerialMsg(const char * msg) { |
| 55 | + #ifdef DEBUG |
| 56 | + if (Serial) { |
| 57 | + Serial.println(msg); |
| 58 | + } |
| 59 | + #endif |
| 60 | +} |
| 61 | + |
| 62 | +void setup() { |
| 63 | + #ifdef DEBUG |
| 64 | + Serial.begin(9600); |
| 65 | + while (!Serial); |
| 66 | + Serial.println("Started"); |
| 67 | + #endif |
| 68 | + |
| 69 | + delay(2000); |
| 70 | + sensorsInit(); |
| 71 | + |
| 72 | + PDM.onReceive(onPDMdata); |
| 73 | + if (!PDM.begin(1, 16000)) { |
| 74 | + printSerialMsg("Failed to start PDM!"); |
| 75 | + blinkLoop(); |
| 76 | + } |
| 77 | + |
| 78 | + if (!BLE.begin()) { |
| 79 | + printSerialMsg("Failed to initialized BLE!"); |
| 80 | + blinkLoop(); |
| 81 | + } |
| 82 | + |
| 83 | + String address = BLE.address(); |
| 84 | + #ifdef DEBUG |
| 85 | + if (Serial) { |
| 86 | + Serial.print("address = "); |
| 87 | + Serial.println(address); |
| 88 | + } |
| 89 | + #endif |
| 90 | + address.toUpperCase(); |
| 91 | + |
| 92 | + name = "BLE Sense - "; |
| 93 | + name += address[address.length() - 5]; |
| 94 | + name += address[address.length() - 4]; |
| 95 | + name += address[address.length() - 2]; |
| 96 | + name += address[address.length() - 1]; |
| 97 | + |
| 98 | + #ifdef DEBUG |
| 99 | + if (Serial) { |
| 100 | + Serial.print("name = "); |
| 101 | + Serial.println(name); |
| 102 | + } |
| 103 | + #endif |
| 104 | + |
| 105 | + BLE.setLocalName(name.c_str()); |
| 106 | + BLE.setDeviceName(name.c_str()); |
| 107 | + BLE.setAdvertisedService(service); |
| 108 | + |
| 109 | + service.addCharacteristic(versionCharacteristic); |
| 110 | + service.addCharacteristic(accelerationCharacteristic); |
| 111 | + service.addCharacteristic(gyroscopeCharacteristic); |
| 112 | + service.addCharacteristic(magneticFieldCharacteristic); |
| 113 | + service.addCharacteristic(temperatureCharacteristic); |
| 114 | + service.addCharacteristic(pressureCharacteristic); |
| 115 | + service.addCharacteristic(humidityCharacteristic); |
| 116 | + service.addCharacteristic(proximityCharacteristic); |
| 117 | + service.addCharacteristic(colorCharacteristic); |
| 118 | + service.addCharacteristic(soundPressureCharacteristic); |
| 119 | + service.addCharacteristic(resistanceCharacteristic); |
| 120 | + |
| 121 | + versionCharacteristic.setValue(VERSION); |
| 122 | + |
| 123 | + BLE.addService(service); |
| 124 | + BLE.advertise(); |
| 125 | + |
| 126 | + lowPower(); |
| 127 | +} |
| 128 | + |
| 129 | +void loop() { |
| 130 | + BLE.poll(1000); |
| 131 | + while (BLE.connected()) { |
| 132 | + lowPowerBleWait(100); |
| 133 | + updateSubscribedCharacteristics(); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +void updateSubscribedCharacteristics() { |
| 138 | + if (accelerationCharacteristic.subscribed()) { |
| 139 | + if (IMU_SK.accelerationAvailable()) { |
| 140 | + float x, y, z; |
| 141 | + IMU_SK.readAcceleration(x, y, z); |
| 142 | + float acceleration[3]; |
| 143 | + |
| 144 | + acceleration[0] = x; |
| 145 | + acceleration[1] = y; |
| 146 | + acceleration[2] = z; |
| 147 | + accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration)); |
| 148 | + } |
| 149 | + } |
| 150 | + if (gyroscopeCharacteristic.subscribed()) { |
| 151 | + if (IMU_SK.gyroscopeAvailable()) { |
| 152 | + float x, y, z; |
| 153 | + IMU_SK.readGyroscope(x, y, z); |
| 154 | + float gyroscope[3]; |
| 155 | + |
| 156 | + gyroscope[0] = x; |
| 157 | + gyroscope[1] =y; |
| 158 | + gyroscope[2] = z; |
| 159 | + gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if (magneticFieldCharacteristic.subscribed()) { |
| 164 | + float x, y, z; |
| 165 | + if (BME.magneticFieldAvailable()) { |
| 166 | + BME.readMagneticField(x, y, z); |
| 167 | + float magneticField[3]; |
| 168 | + magneticField[0] = x; |
| 169 | + magneticField[1] = y; |
| 170 | + magneticField[2] = z; |
| 171 | + magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField)); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (temperatureCharacteristic.subscribed()) { |
| 176 | + float temperature = TempSens.getTemperature(); |
| 177 | + temperatureCharacteristic.writeValue(temperature); |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + if(resistanceCharacteristic.subscribed()){ |
| 182 | + float resistanceAvg = INFINITY; |
| 183 | + resistanceAvg = ResSens.getResistorValue(); |
| 184 | + resistanceCharacteristic.writeValue(resistanceAvg); |
| 185 | + } |
| 186 | + |
| 187 | + if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) { |
| 188 | + uint32_t proximity = APDS.readProximity(); |
| 189 | + proximityCharacteristic.writeValue(proximity); |
| 190 | + } |
| 191 | + if (colorCharacteristic.subscribed() && APDS.colorAvailable()) { |
| 192 | + int color[4]; |
| 193 | + APDS.readColor(color[0], color[1], color[2], color[3]); |
| 194 | + colorCharacteristic.writeValue((byte*)color, sizeof(color)); |
| 195 | + } |
| 196 | + |
| 197 | + if (pressureCharacteristic.subscribed()) { |
| 198 | + float pressure = BARO.readPressure(); |
| 199 | + pressureCharacteristic.writeValue(pressure); |
| 200 | + } |
| 201 | +} |
0 commit comments