Skip to content

Commit a28a8dc

Browse files
author
Paolo Calao
authored
Merge pull request #14 from bcmi-labs/refactor
Refactor
2 parents fbbfd77 + 0a43d1b commit a28a8dc

File tree

15 files changed

+244
-69
lines changed

15 files changed

+244
-69
lines changed

Arduino_BHY2/src/Arduino_BHY2.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "EslovHandler.h"
77
#include "DFUManager.h"
88

9-
Arduino_BHY2::Arduino_BHY2()
9+
Arduino_BHY2::Arduino_BHY2() :
10+
_debug(NULL)
1011
{
1112
}
1213

@@ -26,6 +27,23 @@ void Arduino_BHY2::update()
2627
{
2728
sensortec.update();
2829
bleHandler.update();
30+
31+
// While updating fw, detach the library from the sketch
32+
if (dfuManager.isPending()) {
33+
if (_debug) _debug->println("Start DFU procedure. Sketch execution is stopped.");
34+
// TODO: abort dfu
35+
while (dfuManager.isPending()) {
36+
bleHandler.update();
37+
}
38+
// Wait some time for acknowledgment retrieval
39+
auto timeRef = millis();
40+
while (millis() - timeRef < 1000) {
41+
bleHandler.update();
42+
}
43+
// Reboot after fw update
44+
if (_debug) _debug->println("DFU procedure terminated. Rebooting.");
45+
NVIC_SystemReset();
46+
}
2947
}
3048

3149
void Arduino_BHY2::configureSensor(SensorConfigurationPacket& config)
@@ -74,6 +92,7 @@ void Arduino_BHY2::parse(SensorDataPacket& data, DataOrientation& vector, float
7492

7593
void Arduino_BHY2::debug(Stream &stream)
7694
{
95+
_debug = &stream;
7796
eslovHandler.debug(stream);
7897
BLEHandler::debug(stream);
7998
sensortec.debug(stream);

Arduino_BHY2/src/Arduino_BHY2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Arduino_BHY2 {
2626
void parse(SensorDataPacket& data, DataOrientation& vector, float scaleFactor);
2727

2828
void debug(Stream &stream);
29+
30+
private:
31+
Stream *_debug;
2932
};
3033

3134
extern Arduino_BHY2 BHY2;

Arduino_BHY2/src/BLEHandler.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,9 @@ void BLEHandler::processDFUPacket(DFUType dfuType, BLECharacteristic characteris
4343
_debug->print("Size of data: ");
4444
_debug->println(sizeof(data));
4545
}
46-
_last = dfuManager.processPacket(dfuType, data);
47-
writeDFUAcknowledgment();
46+
dfuManager.processPacket(dfuType, data);
4847

49-
if (_last == 1) {
50-
// reboot
51-
delay(500);
52-
NVIC_SystemReset();
53-
}
48+
writeDFUAcknowledgment();
5449
}
5550

5651
void BLEHandler::receivedInternalDFU(BLEDevice central, BLECharacteristic characteristic)
@@ -123,6 +118,7 @@ void BLEHandler::update()
123118
}
124119

125120
}
121+
126122
}
127123

128124
void BLEHandler::debug(Stream &stream)

Arduino_BHY2/src/BLEHandler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class BLEHandler {
1818

1919
private:
2020
static Stream *_debug;
21-
uint8_t _last;
2221

2322
void writeDFUAcknowledgment();
2423
void processDFUPacket(DFUType dfuType, BLECharacteristic characteristic);

Arduino_BHY2/src/BoschSensortec.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ void BoschSensortec::begin()
1616
auto ret = bhy2_init(BHY2_SPI_INTERFACE, bhy2_spi_read, bhy2_spi_write, bhy2_delay_us, MAX_READ_WRITE_LEN, NULL, &_bhy2);
1717
if (_debug) _debug->println(get_api_error(ret));
1818

19+
// Print bhi status
1920
uint8_t stat;
2021
ret = bhy2_get_boot_status(&stat, &_bhy2);
2122
if (_debug) {
2223
_debug->println(get_api_error(ret));
2324
_debug->print("Boot status: ");
2425
_debug->println(stat, HEX);
2526
}
26-
2727
ret = bhy2_get_host_interrupt_ctrl(&stat, &_bhy2);
2828
if (_debug) {
2929
_debug->println(get_api_error(ret));
@@ -44,6 +44,7 @@ void BoschSensortec::begin()
4444
ret = bhy2_get_and_process_fifo(_workBuffer, WORK_BUFFER_SIZE, &_bhy2);
4545
if (_debug) _debug->println(get_api_error(ret));
4646

47+
// All sensors' data are handled in the same generic way
4748
for (uint8_t i = 1; i < BHY2_SENSOR_ID_MAX; i++) {
4849
bhy2_register_fifo_parse_callback(i, BoschParser::parseData, NULL, &_bhy2);
4950
}
@@ -69,11 +70,9 @@ bool BoschSensortec::readSensorData(SensorDataPacket &data)
6970

7071
void BoschSensortec::addSensorData(const SensorDataPacket &sensorData)
7172
{
72-
if (!_sensorQueue.full()) {
73-
_sensorQueue.push(sensorData);
74-
} else {
75-
// handle the queue by storing it in flash if full
76-
}
73+
// Overwrites oldest data when fifo is full
74+
_sensorQueue.push(sensorData);
75+
// Alternative: handle the full queue by storing it in flash
7776
}
7877

7978
void BoschSensortec::update()

Arduino_BHY2/src/BoschSensortec.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ extern "C"
2121

2222
#define MAX_READ_WRITE_LEN 256
2323

24-
// This will use the BHY functions for configuring sensors and retrieving data
2524
class BoschSensortec {
2625
public:
2726
BoschSensortec();

Arduino_BHY2/src/DFUManager.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#include "DFUManager.h"
22

3-
#if defined (TARGET_ANNA)
43
SPIFBlockDevice DFUManager::_bd(SPI_PSELMOSI0, SPI_PSELMISO0,
54
SPI_PSELSCK0, CS_FLASH);
6-
#else
7-
// half the flash (512KB) is dedicated as dfu temporary storage
8-
FlashIAPBlockDevice DFUManager::_bd(0x80000, 0x80000);
9-
#endif
105

116
mbed::LittleFileSystem DFUManager::_fs("fs");
127

138
DFUManager::DFUManager() :
149
_target(NULL),
1510
_acknowledgment(DFUNack),
11+
_transferPending(false),
1612
_debug(NULL)
1713
{
1814
}
@@ -33,9 +29,11 @@ void DFUManager::begin()
3329
}
3430
}
3531

36-
uint8_t DFUManager::processPacket(DFUType dfuType, const uint8_t* data)
32+
void DFUManager::processPacket(DFUType dfuType, const uint8_t* data)
3733
{
3834
DFUPacket* packet = (DFUPacket*)data;
35+
_transferPending = true;
36+
3937
if (_debug) {
4038
_debug->print("packet: ");
4139
_debug->println(packet->index);
@@ -71,11 +69,17 @@ uint8_t DFUManager::processPacket(DFUType dfuType, const uint8_t* data)
7169
_debug->print("Last packet received. Remaining: ");
7270
_debug->println(packet->remaining);
7371
}
74-
fclose(_target);
75-
_target = NULL;
72+
if (_acknowledgment == DFUAck) {
73+
fclose(_target);
74+
_target = NULL;
75+
_transferPending = false;
76+
}
7677
}
78+
}
7779

78-
return packet->last;
80+
bool DFUManager::isPending()
81+
{
82+
return _transferPending;
7983
}
8084

8185
// acknowledgment flag is reset when read

Arduino_BHY2/src/DFUManager.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33

44
#include "Arduino.h"
55

6-
#define TARGET_ANNA
7-
8-
#if defined (TARGET_ANNA)
96
#include "SPIFBlockDevice.h"
10-
#else
11-
#include "FlashIAPBlockDevice.h"
12-
#endif
137

148
#include "LittleFileSystem.h"
159

@@ -38,20 +32,19 @@ class DFUManager {
3832
virtual ~DFUManager();
3933

4034
void begin();
41-
uint8_t processPacket(DFUType dfuType, const uint8_t* data);
35+
void processPacket(DFUType dfuType, const uint8_t* data);
36+
37+
bool isPending();
4238

4339
uint8_t acknowledgment();
4440

4541
private:
46-
#if defined (TARGET_ANNA)
4742
static SPIFBlockDevice _bd;
48-
#else
49-
static FlashIAPBlockDevice _bd;
50-
#endif
5143
static mbed::LittleFileSystem _fs;
5244
FILE* _target;
5345

5446
uint8_t _acknowledgment;
47+
bool _transferPending;
5548

5649
private:
5750
friend class Arduino_BHY2;

Arduino_BHY2/src/EslovHandler.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ EslovHandler::EslovHandler() :
77
_rxIndex(0),
88
_rxBuffer(),
99
_state(ESLOV_AVAILABLE_SENSOR_STATE),
10-
_last(0),
1110
_debug(NULL)
1211
{
1312
}
@@ -18,7 +17,7 @@ EslovHandler::~EslovHandler()
1817

1918
void EslovHandler::begin()
2019
{
21-
Wire1.begin(ESLOV_DEFAULT_ADDRESS);
20+
Wire1.begin(ESLOV_DEFAULT_ADDRESS);
2221
Wire1.onReceive(EslovHandler::onReceive);
2322
Wire1.onRequest(EslovHandler::onRequest);
2423
}
@@ -61,12 +60,6 @@ void EslovHandler::requestEvent()
6160
_debug->println(ack);
6261
}
6362
Wire1.write(ack);
64-
65-
if (_last == 1) {
66-
// reboot
67-
delay(500);
68-
NVIC_SystemReset();
69-
}
7063
}
7164
}
7265

@@ -83,7 +76,7 @@ void EslovHandler::receiveEvent(int length)
8376
// Check if packet is complete depending on its opcode
8477
if (_rxBuffer[0] == ESLOV_DFU_EXTERNAL_OPCODE) {
8578
if (_rxIndex == sizeof(DFUPacket) + 1) {
86-
_last = dfuManager.processPacket(DFU_EXTERNAL, &_rxBuffer[1]);
79+
dfuManager.processPacket(DFU_EXTERNAL, &_rxBuffer[1]);
8780

8881
_state = ESLOV_DFU_ACK_STATE;
8982

@@ -93,7 +86,7 @@ void EslovHandler::receiveEvent(int length)
9386

9487
} else if (_rxBuffer[0] == ESLOV_DFU_INTERNAL_OPCODE) {
9588
if (_rxIndex == sizeof(DFUPacket) + 1) {
96-
_last = dfuManager.processPacket(DFU_INTERNAL, &_rxBuffer[1]);
89+
dfuManager.processPacket(DFU_INTERNAL, &_rxBuffer[1]);
9790

9891
_state = ESLOV_DFU_ACK_STATE;
9992

Arduino_BHY2/src/EslovHandler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class EslovHandler {
3838
uint8_t _rxBuffer[ESLOV_MAX_LENGTH];
3939

4040
EslovState _state;
41-
uint8_t _last;
4241

4342
private:
4443
friend class Arduino_BHY2;

0 commit comments

Comments
 (0)