Skip to content

Commit 8bf78ad

Browse files
authored
Merge pull request #29 from arduino-libraries/jhansson-ard/nanoblerev2firmware
added nano ble sense rev2 firmware
2 parents 093308c + 8053411 commit 8bf78ad

File tree

4 files changed

+351
-0
lines changed

4 files changed

+351
-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: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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(9600);
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+
}
152+
153+
void loop() {
154+
BLE.poll(1000);
155+
while (BLE.connected()) {
156+
lowPowerBleWait(100);
157+
updateSubscribedCharacteristics();
158+
}
159+
}
160+
161+
void updateSubscribedCharacteristics() {
162+
if (accelerationCharacteristic.subscribed()) {
163+
if (IMU.accelerationAvailable()) {
164+
float x, y, z;
165+
IMU.readAcceleration(x, y, z);
166+
float acceleration[3];
167+
168+
acceleration[0] = x;
169+
acceleration[1] = y;
170+
acceleration[2] = z;
171+
accelerationCharacteristic.writeValue((byte*)acceleration, sizeof(acceleration));
172+
}
173+
}
174+
if (gyroscopeCharacteristic.subscribed()) {
175+
if (IMU.gyroscopeAvailable()) {
176+
float x, y, z;
177+
IMU.readGyroscope(x, y, z);
178+
float gyroscope[3];
179+
180+
gyroscope[0] = x;
181+
gyroscope[1] = y;
182+
gyroscope[2] = z;
183+
gyroscopeCharacteristic.writeValue((byte*)gyroscope, sizeof(gyroscope));
184+
}
185+
}
186+
187+
if (magneticFieldCharacteristic.subscribed()) {
188+
float x, y, z;
189+
if (IMU.magneticFieldAvailable()) {
190+
IMU.readMagneticField(x, y, z);
191+
float magneticField[3];
192+
magneticField[0] = x;
193+
magneticField[1] = y;
194+
magneticField[2] = z;
195+
magneticFieldCharacteristic.writeValue((byte*)magneticField, sizeof(magneticField));
196+
}
197+
}
198+
if (soundPressureCharacteristic.subscribed()) {
199+
uint16_t sound = getSoundAverage();
200+
soundPressureCharacteristic.writeValue(sound);
201+
}
202+
203+
bool doTemperature = temperatureCharacteristic.subscribed();
204+
bool doHumidity = humidityCharacteristic.subscribed();
205+
if (doTemperature || doHumidity) {
206+
float temperature = HS300x.readTemperature();
207+
if (doTemperature) {
208+
temperatureCharacteristic.writeValue(temperature);
209+
}
210+
if (doHumidity) {
211+
float humidity = HS300x.readHumidity();
212+
float dp = temperature - ((100.0 - humidity) / 5.0);
213+
float humidityCalibrated = 100.0 - (5.0 * (temperature - dp));
214+
humidityCharacteristic.writeValue(humidityCalibrated);
215+
}
216+
}
217+
218+
if (resistanceCharacteristic.subscribed()) {
219+
readVoltage();
220+
uint16_t measuredValue = getVoltageAverage();
221+
float voltageRatio = 1024.0f / measuredValue;
222+
resistanceCharacteristic.writeValue(voltageRatio);
223+
}
224+
225+
if (proximityCharacteristic.subscribed() && APDS.proximityAvailable()) {
226+
uint32_t proximity = APDS.readProximity();
227+
proximityCharacteristic.writeValue(proximity);
228+
}
229+
if (colorCharacteristic.subscribed() && APDS.colorAvailable()) {
230+
int color[4];
231+
APDS.readColor(color[0], color[1], color[2], color[3]);
232+
colorCharacteristic.writeValue((byte*)color, sizeof(color));
233+
}
234+
235+
if (pressureCharacteristic.subscribed()) {
236+
float pressure = BARO.readPressure();
237+
pressureCharacteristic.writeValue(pressure);
238+
}
239+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 "Arduino_BMI270_BMM150.h"
16+
17+
18+
#include <avr/dtostrf.h>
19+
20+
21+
22+
const uint32_t SHUNT_MICRO_OHM{100000}; ///< Shunt resistance in Micro-Ohm, e.g. 100000 is 0.1 Ohm
23+
const uint16_t MAXIMUM_AMPS{1}; ///< Max expected amps, clamped from 1A to a max of 1022A
24+
25+
// INA_Class INA;
26+
27+
SerialFlashFile file;
28+
29+
void blinkLoop() {
30+
while (1) {
31+
digitalWrite(LED_BUILTIN, HIGH);
32+
delay(500);
33+
digitalWrite(LED_BUILTIN, LOW);
34+
delay(500);
35+
}
36+
}
37+
38+
void sensorsInit() {
39+
// bmm150 and bmi270
40+
if (!IMU.begin()) {
41+
Serial.println("Failed to initialize IMU!");
42+
while (1);
43+
}
44+
Serial.println("IMU Initialized");
45+
46+
// Flash Init
47+
if (!SerialFlash.begin(FlashChipSelect)) {
48+
Serial.println(F("Failed to initialize Flash!"));
49+
while (1);
50+
}
51+
52+
Serial.println("Flash Initialized");
53+
54+
if (!APDS.begin()) {
55+
Serial.println("Failed to initialized APDS!");
56+
blinkLoop();
57+
}
58+
59+
if (!HS300x.begin()) {
60+
Serial.println("Failed to initialized HTS!");
61+
blinkLoop();
62+
}
63+
64+
if (!BARO.begin()) {
65+
Serial.println("Failed to initialized BARO!");
66+
blinkLoop();
67+
}
68+
}

0 commit comments

Comments
 (0)