Skip to content

Commit 0393917

Browse files
committed
added nano ble sense rev2 firmware
1 parent 093308c commit 0393917

File tree

4 files changed

+363
-0
lines changed

4 files changed

+363
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Arduino.h"
2+
#include "mbed.h"
3+
#include "ArduinoBLE.h"
4+
#include "LowPower.h"
5+
6+
#include "nrf_power.h"
7+
#include "nrf_uarte.h"
8+
#include "nrf_uart.h"
9+
10+
void lowPower()
11+
{
12+
// Disable UARTE0 which is initially enabled by the bootloader
13+
nrf_uarte_task_trigger(NRF_UARTE0, NRF_UARTE_TASK_STOPRX);
14+
while (!nrf_uarte_event_check(NRF_UARTE0, NRF_UARTE_EVENT_RXTO)) ;
15+
NRF_UARTE0->ENABLE = 0;
16+
NRF_UART0->ENABLE = 0;
17+
18+
// Enable DCDC
19+
nrf_power_dcdcen_set(true);
20+
21+
// Turn off LED_BUILTIN
22+
digitalWrite(LED_BUILTIN, LOW);
23+
}
24+
25+
void lowPowerWait(unsigned long time)
26+
{
27+
rtos::ThisThread::sleep_for(time);
28+
}
29+
30+
void lowPowerBleWait(unsigned long time)
31+
{
32+
unsigned long timeRef = millis();
33+
while (millis() - timeRef < time) {
34+
BLE.poll(time - (millis() - timeRef));
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _LOWPOWER_H_
2+
#define _LOWPOWER_H_
3+
4+
void lowPower();
5+
void lowPowerWait(unsigned long time);
6+
void lowPowerBleWait(unsigned long time);
7+
8+
#endif //_LOWPOWER_H_
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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+
#define RESISTANCE_PIN A0
13+
#define VOLTAGE_BUFFER_SIZE 16
14+
#define DEBUG 0
15+
16+
BLEService service(SCIENCE_KIT_UUID("0000"));
17+
BLEUnsignedIntCharacteristic versionCharacteristic(SCIENCE_KIT_UUID("0001"), BLERead);
18+
BLECharacteristic accelerationCharacteristic(SCIENCE_KIT_UUID("0011"), BLENotify, 3 * sizeof(float));
19+
BLECharacteristic gyroscopeCharacteristic(SCIENCE_KIT_UUID("0012"), BLENotify, 3 * sizeof(float));
20+
BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID("0013"), BLENotify, 3 * sizeof(float));
21+
BLEFloatCharacteristic temperatureCharacteristic(SCIENCE_KIT_UUID("0014"), BLENotify);
22+
BLEFloatCharacteristic pressureCharacteristic(SCIENCE_KIT_UUID("0015"), BLENotify);
23+
BLEFloatCharacteristic humidityCharacteristic(SCIENCE_KIT_UUID("0016"), BLENotify);
24+
BLEUnsignedIntCharacteristic proximityCharacteristic(SCIENCE_KIT_UUID("0017"), BLENotify);
25+
BLECharacteristic colorCharacteristic(SCIENCE_KIT_UUID("0018"), BLENotify, 4 * sizeof(int));
26+
BLEUnsignedShortCharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID("0019"), BLENotify);
27+
BLEFloatCharacteristic resistanceCharacteristic(SCIENCE_KIT_UUID("0020"), BLENotify);
28+
29+
byte voltageBufferIndex = 0;
30+
bool voltageBufferFilled = false;
31+
short soundSampleBuffer[256];
32+
short voltageSampleBuffer[VOLTAGE_BUFFER_SIZE];
33+
34+
void updateSubscribedCharacteristics();
35+
36+
void onPDMdata() {
37+
// query the number of bytes available
38+
int bytesAvailable = PDM.available();
39+
40+
// read into the sample buffer
41+
PDM.read(soundSampleBuffer, bytesAvailable);
42+
}
43+
44+
uint16_t getSoundAverage() {
45+
uint32_t avg = 0;
46+
for (int i = 0; i < sizeof(soundSampleBuffer) / sizeof(soundSampleBuffer[0]); i++) {
47+
avg += soundSampleBuffer[i] * soundSampleBuffer[i];
48+
}
49+
return sqrt(avg);
50+
}
51+
52+
void readVoltage() {
53+
voltageSampleBuffer[voltageBufferIndex] = analogRead(RESISTANCE_PIN);
54+
if (!voltageBufferFilled && voltageBufferIndex == VOLTAGE_BUFFER_SIZE - 1) {
55+
voltageBufferFilled = true;
56+
}
57+
voltageBufferIndex = (++voltageBufferIndex) % VOLTAGE_BUFFER_SIZE;
58+
}
59+
60+
uint16_t getVoltageAverage() {
61+
uint16_t avg = 0;
62+
byte upperBound = voltageBufferFilled ? VOLTAGE_BUFFER_SIZE : voltageBufferIndex;
63+
for (int i = 0; i < upperBound; i++) {
64+
avg += voltageSampleBuffer[i];
65+
}
66+
return avg / upperBound;
67+
}
68+
69+
// String to calculate the local and device name
70+
String name;
71+
unsigned long lastNotify = 0;
72+
73+
void printSerialMsg(const char* msg) {
74+
#ifdef DEBUG
75+
if (Serial) {
76+
Serial.println(msg);
77+
}
78+
#endif
79+
}
80+
81+
82+
83+
void setup() {
84+
#ifdef DEBUG
85+
Serial.begin(115200);
86+
while (!Serial)
87+
;
88+
Serial.println("Started");
89+
#endif
90+
91+
delay(2000);
92+
sensorsInit();
93+
94+
pinMode(RESISTANCE_PIN, INPUT); // Used for reading resistance
95+
96+
PDM.onReceive(onPDMdata);
97+
if (!PDM.begin(1, 16000)) {
98+
printSerialMsg("Failed to start PDM!");
99+
blinkLoop();
100+
}
101+
102+
if (!BLE.begin()) {
103+
printSerialMsg("Failed to initialized BLE!");
104+
blinkLoop();
105+
}
106+
107+
String address = BLE.address();
108+
#ifdef DEBUG
109+
if (Serial) {
110+
Serial.print("address = ");
111+
Serial.println(address);
112+
}
113+
#endif
114+
address.toUpperCase();
115+
116+
name = "BLE Sense - ";
117+
name += address[address.length() - 5];
118+
name += address[address.length() - 4];
119+
name += address[address.length() - 2];
120+
name += address[address.length() - 1];
121+
122+
#ifdef DEBUG
123+
if (Serial) {
124+
Serial.print("name = ");
125+
Serial.println(name);
126+
}
127+
#endif
128+
129+
BLE.setLocalName(name.c_str());
130+
BLE.setDeviceName(name.c_str());
131+
BLE.setAdvertisedService(service);
132+
133+
service.addCharacteristic(versionCharacteristic);
134+
service.addCharacteristic(accelerationCharacteristic);
135+
service.addCharacteristic(gyroscopeCharacteristic);
136+
service.addCharacteristic(magneticFieldCharacteristic);
137+
service.addCharacteristic(temperatureCharacteristic);
138+
service.addCharacteristic(pressureCharacteristic);
139+
service.addCharacteristic(humidityCharacteristic);
140+
service.addCharacteristic(proximityCharacteristic);
141+
service.addCharacteristic(colorCharacteristic);
142+
service.addCharacteristic(soundPressureCharacteristic);
143+
service.addCharacteristic(resistanceCharacteristic);
144+
145+
versionCharacteristic.setValue(VERSION);
146+
147+
BLE.addService(service);
148+
BLE.advertise();
149+
150+
lowPower();
151+
Serial.println("Initialization done!");
152+
}
153+
154+
void loop() {
155+
BLE.poll(1000);
156+
while (BLE.connected()) {
157+
lowPowerBleWait(100);
158+
updateSubscribedCharacteristics();
159+
}
160+
}
161+
162+
void updateSubscribedCharacteristics() {
163+
Serial.println("updateSubscribedCharacteristics");
164+
if (accelerationCharacteristic.subscribed()) {
165+
Serial.println("subricption done");
166+
if (IMU.accelerationAvailable()) {
167+
Serial.println("acceleration data available");
168+
float x, y, z;
169+
IMU.readAcceleration(x, y, z);
170+
float acceleration[3];
171+
172+
acceleration[0] = x;
173+
acceleration[1] = y;
174+
acceleration[2] = z;
175+
Serial.print("values: ");
176+
Serial.print(x);
177+
Serial.print(" ");
178+
Serial.print(y);
179+
Serial.print(" ");
180+
Serial.println(z);
181+
accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration));
182+
}
183+
}
184+
if (gyroscopeCharacteristic.subscribed()) {
185+
if (IMU.gyroscopeAvailable()) {
186+
float x, y, z;
187+
IMU.readGyroscope(x, y, z);
188+
float gyroscope[3];
189+
190+
gyroscope[0] = x;
191+
gyroscope[1] = y;
192+
gyroscope[2] = z;
193+
gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope));
194+
}
195+
}
196+
197+
if (magneticFieldCharacteristic.subscribed()) {
198+
float x, y, z;
199+
if (IMU.magneticFieldAvailable()) {
200+
IMU.readMagneticField(x, y, z);
201+
float magneticField[3];
202+
magneticField[0] = x;
203+
magneticField[1] = y;
204+
magneticField[2] = z;
205+
magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField));
206+
}
207+
}
208+
if (soundPressureCharacteristic.subscribed()) {
209+
uint16_t sound = getSoundAverage();
210+
soundPressureCharacteristic.writeValue(sound);
211+
}
212+
213+
bool doTemperature = temperatureCharacteristic.subscribed();
214+
bool doHumidity = humidityCharacteristic.subscribed();
215+
if (doTemperature || doHumidity) {
216+
float temperature = HS300x.readTemperature();
217+
if (doTemperature) {
218+
temperatureCharacteristic.writeValue(temperature);
219+
}
220+
if (doHumidity) {
221+
float humidity = HS300x.readHumidity();
222+
float dp = temperature - ((100.0 - humidity) / 5.0);
223+
float humidityCalibrated = 100.0 - (5.0 * (temperature - dp));
224+
humidityCharacteristic.writeValue(humidityCalibrated);
225+
}
226+
}
227+
228+
if (resistanceCharacteristic.subscribed()) {
229+
readVoltage();
230+
uint16_t measuredValue = getVoltageAverage();
231+
float voltageRatio = 1024.0f / measuredValue;
232+
resistanceCharacteristic.writeValue(voltageRatio);
233+
}
234+
235+
if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) {
236+
uint32_t proximity = APDS.readProximity();
237+
proximityCharacteristic.writeValue(proximity);
238+
}
239+
if (colorCharacteristic.subscribed() && APDS.colorAvailable()) {
240+
int color[4];
241+
APDS.readColor(color[0], color[1], color[2], color[3]);
242+
colorCharacteristic.writeValue((byte*)color, sizeof(color));
243+
}
244+
245+
if (pressureCharacteristic.subscribed()) {
246+
float pressure = BARO.readPressure();
247+
pressureCharacteristic.writeValue(pressure);
248+
}
249+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "ArduinoScienceJournal.h"
2+
3+
// Flash memory
4+
#include "SerialFlash.h"
5+
#include <SPI.h>
6+
7+
const int FlashChipSelect = 2;
8+
9+
#include <Arduino_APDS9960.h>
10+
#include <Arduino_HS300x.h> // hs3003
11+
#include <Arduino_LPS22HB.h>
12+
13+
// IMU
14+
// BMM150 and bmi 270
15+
//#include <BoschSensorClass.h>
16+
#include "Arduino_BMI270_BMM150.h"
17+
18+
19+
#include <avr/dtostrf.h>
20+
21+
22+
//BoschSensorClass BME = BoschSensorClass(Wire);
23+
24+
const uint32_t SHUNT_MICRO_OHM{100000}; ///< Shunt resistance in Micro-Ohm, e.g. 100000 is 0.1 Ohm
25+
const uint16_t MAXIMUM_AMPS{1}; ///< Max expected amps, clamped from 1A to a max of 1022A
26+
27+
// INA_Class INA;
28+
29+
SerialFlashFile file;
30+
31+
void blinkLoop() {
32+
while (1) {
33+
digitalWrite(LED_BUILTIN, HIGH);
34+
delay(500);
35+
digitalWrite(LED_BUILTIN, LOW);
36+
delay(500);
37+
}
38+
}
39+
40+
void sensorsInit() {
41+
// bmm150
42+
if (!IMU.begin()) {
43+
Serial.println("Failed to initialize IMU!");
44+
while (1);
45+
}
46+
Serial.println("IMU Initialized");
47+
48+
// Flash Init
49+
if (!SerialFlash.begin(FlashChipSelect)) {
50+
Serial.println(F("Failed to initialize Flash!"));
51+
while (1);
52+
}
53+
54+
Serial.println("Flash Initialized");
55+
56+
if (!APDS.begin()) {
57+
Serial.println("Failed to initialized APDS!");
58+
blinkLoop();
59+
}
60+
61+
if (!HS300x.begin()) {
62+
Serial.println("Failed to initialized HTS!");
63+
blinkLoop();
64+
}
65+
66+
if (!BARO.begin()) {
67+
Serial.println("Failed to initialized BARO!");
68+
blinkLoop();
69+
}
70+
}

0 commit comments

Comments
 (0)