Skip to content

Commit f811c2d

Browse files
authored
Merge pull request #4 from bcmi-labs/hide-config-objects
Hide configuration objects and simplify API.
2 parents 30ef095 + d041968 commit f811c2d

12 files changed

+176
-34
lines changed

examples/ts_spi/ts_spi.ino

+1-13
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ static size_t constexpr NUM_THREADS = 20;
1818
* FUNCTION DECLARATION
1919
**************************************************************************************/
2020

21-
void bmp388_select();
22-
void bmp388_deselect();
2321
byte bmp388_read_reg(byte const reg_addr);
2422
void bmp388_thread_func();
2523

2624
/**************************************************************************************
2725
* GLOBAL VARIABLES
2826
**************************************************************************************/
2927

30-
SpiBusDevice bmp388{"SPI", SpiBusDeviceConfig {SPISettings{1000000, MSBFIRST, SPI_MODE0}, BMP388_CS_PIN}};
28+
SpiBusDevice bmp388 = BusDeviceCreator.create(SPI, BMP388_CS_PIN, 1000000, MSBFIRST, SPI_MODE0);
3129

3230
static char thread_name[NUM_THREADS][32];
3331

@@ -60,16 +58,6 @@ void loop()
6058
* FUNCTION DEFINITION
6159
**************************************************************************************/
6260

63-
void bmp388_select()
64-
{
65-
digitalWrite(BMP388_CS_PIN, LOW);
66-
}
67-
68-
void bmp388_deselect()
69-
{
70-
digitalWrite(BMP388_CS_PIN, HIGH);
71-
}
72-
7361
byte bmp388_read_reg(byte const reg_addr)
7462
{
7563
byte const write_buf[3] =

examples/ts_wire/ts_wire.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void lsm6dsox_thread_func();
2424
* GLOBAL VARIABLES
2525
**************************************************************************************/
2626

27-
WireBusDevice lsm6dsox{"Wire", WireBusDeviceConfig{LSM6DSOX_ADDRESS}};
27+
WireBusDevice lsm6dsox = BusDeviceCreator.create(Wire, LSM6DSOX_ADDRESS);
2828

2929
static char thread_name[NUM_THREADS][32];
3030

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ SpiBusDevice KEYWORD1
1212
SpiBusDeviceConfig KEYWORD1
1313
WireBusDevice KEYWORD1
1414
WireBusDeviceConfig KEYWORD1
15+
BusDeviceCreator KEYWORD1
1516

1617
#######################################
1718
# Methods and Functions (KEYWORD2)
1819
#######################################
1920

2021
transfer KEYWORD2
22+
create KEYWORD2
2123

2224
#######################################
2325
# Constants (LITERAL1)

src/Arduino_ThreadsafeIO.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* INCLUDE
2424
**************************************************************************************/
2525

26+
#include "BusDeviceCreator.h"
2627
#include "spi/SpiBusDevice.h"
2728
#include "wire/WireBusDevice.h"
2829

src/BusDeviceCreator.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of the Arduino_ThreadsafeIO library.
3+
* Copyright (c) 2021 Arduino SA.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
/**************************************************************************************
20+
* INCLUDE
21+
**************************************************************************************/
22+
23+
#include "BusDeviceCreator.h"
24+
25+
/**************************************************************************************
26+
* NAMESPACE
27+
**************************************************************************************/
28+
29+
namespace impl
30+
{
31+
32+
/**************************************************************************************
33+
* PUBLIC MEMBER FUNCTIONS
34+
**************************************************************************************/
35+
36+
SpiBusDevice create(arduino::SPIClass & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol)
37+
{
38+
return SpiBusDevice(SpiBusDeviceConfig{spi,
39+
spi_settings,
40+
cs_pin,
41+
fill_symbol
42+
});
43+
}
44+
45+
SpiBusDevice BusDeviceCreator::create(arduino::SPIClass & spi, int const cs_pin, uint32_t const spi_clock, BitOrder const spi_bit_order, SPIMode const spi_bit_mode, byte const fill_symbol)
46+
{
47+
return SpiBusDevice(SpiBusDeviceConfig{spi,
48+
SPISettings(spi_clock, spi_bit_order, spi_bit_mode),
49+
cs_pin,
50+
fill_symbol
51+
});
52+
}
53+
54+
SpiBusDevice BusDeviceCreator::create(arduino::SPIClass & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol)
55+
{
56+
return SpiBusDevice(SpiBusDeviceConfig{spi, spi_settings, spi_select, spi_deselect, fill_symbol});
57+
}
58+
59+
WireBusDevice BusDeviceCreator::create(arduino::HardwareI2C & wire, byte const slave_addr)
60+
{
61+
return create(wire, slave_addr, true, true);
62+
}
63+
64+
WireBusDevice BusDeviceCreator::create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart)
65+
{
66+
return create(wire, slave_addr, restart, true);
67+
}
68+
69+
WireBusDevice BusDeviceCreator::create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop)
70+
{
71+
return WireBusDevice(WireBusDeviceConfig{wire, slave_addr, restart, stop});
72+
}
73+
74+
/**************************************************************************************
75+
* NAMESPACE
76+
**************************************************************************************/
77+
78+
} /* namespace impl */
79+
80+
/**************************************************************************************
81+
* GLOBAL VARIABLES
82+
**************************************************************************************/
83+
84+
impl::BusDeviceCreator BusDeviceCreator;

src/BusDeviceCreator.h

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This file is part of the Arduino_ThreadsafeIO library.
3+
* Copyright (c) 2021 Arduino SA.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef BUS_DEVICE_CREATOR_H_
20+
#define BUS_DEVICE_CREATOR_H_
21+
22+
/**************************************************************************************
23+
* INCLUDE
24+
**************************************************************************************/
25+
26+
#include "spi/SpiBusDevice.h"
27+
#include "wire/WireBusDevice.h"
28+
29+
/**************************************************************************************
30+
* CLASS DECLARATION
31+
**************************************************************************************/
32+
33+
namespace impl
34+
{
35+
36+
class BusDeviceCreator
37+
{
38+
public:
39+
40+
SpiBusDevice create(arduino::SPIClass & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
41+
SpiBusDevice create(arduino::SPIClass & spi, int const cs_pin, uint32_t const spi_clock, BitOrder const spi_bit_order, SPIMode const spi_bit_mode, byte const fill_symbol = 0xFF);
42+
SpiBusDevice create(arduino::SPIClass & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
43+
44+
WireBusDevice create(arduino::HardwareI2C & wire, byte const slave_addr);
45+
WireBusDevice create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart);
46+
WireBusDevice create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop);
47+
48+
};
49+
50+
} /* namespace impl */
51+
52+
/**************************************************************************************
53+
* EXTERN DECLARATION
54+
**************************************************************************************/
55+
56+
extern impl::BusDeviceCreator BusDeviceCreator;
57+
58+
#endif /* BUS_DEVICE_CREATOR_H_ */

src/spi/SpiBusDevice.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class SpiBusDevice : public BusDevice
3939
{
4040
public:
4141

42-
SpiBusDevice(std::string const & spi_bus, SpiBusDeviceConfig const & config)
42+
SpiBusDevice(SpiBusDeviceConfig const & config)
4343
: _config{config}
44-
{ /* TODO: Select SPI bus based in string. */ }
44+
{ }
4545

4646

4747
virtual IoResponse transfer(IoRequest & req) override

src/spi/SpiBusDeviceConfig.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,26 @@ class SpiBusDeviceConfig
4141
typedef std::function<void(void)> SpiDeselectFunc;
4242

4343

44-
SpiBusDeviceConfig(SPISettings const & spi_settings, SpiSelectFunc spi_select, SpiDeselectFunc spi_deselect, byte const fill_symbol = 0xFF)
45-
: _spi_settings{spi_settings}
44+
SpiBusDeviceConfig(arduino::SPIClass & spi, SPISettings const & spi_settings, SpiSelectFunc spi_select, SpiDeselectFunc spi_deselect, byte const fill_symbol = 0xFF)
45+
: _spi{spi}
46+
, _spi_settings{spi_settings}
4647
, _spi_select{spi_select}
4748
, _spi_deselect{spi_deselect}
4849
, _fill_symbol{fill_symbol}
4950
{ }
5051

51-
SpiBusDeviceConfig(SPISettings const & spi_settings, int const cs_pin, byte const fill_symbol = 0xFF)
52+
SpiBusDeviceConfig(arduino::SPIClass & spi, SPISettings const & spi_settings, int const cs_pin, byte const fill_symbol = 0xFF)
5253
: SpiBusDeviceConfig
53-
{spi_settings,
54+
{spi,
55+
spi_settings,
5456
[cs_pin](){ digitalWrite(cs_pin, LOW); },
5557
[cs_pin](){ digitalWrite(cs_pin, HIGH); },
5658
fill_symbol
5759
}
5860
{ }
5961

6062

63+
arduino::SPIClass & spi() { return _spi; }
6164
SPISettings settings () const { return _spi_settings; }
6265
void select () const { if (_spi_select) _spi_select(); }
6366
void deselect () const { if (_spi_deselect) _spi_deselect(); }
@@ -66,6 +69,7 @@ class SpiBusDeviceConfig
6669

6770
private:
6871

72+
arduino::SPIClass & _spi;
6973
SPISettings _spi_settings;
7074
SpiSelectFunc _spi_select{nullptr};
7175
SpiDeselectFunc _spi_deselect{nullptr};

src/spi/SpiDispatcher.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void SpiDispatcher::processSpiIoRequest(SpiIoTransaction * spi_io_transaction)
138138

139139
config->select();
140140

141-
SPI.beginTransaction(config->settings());
141+
config->spi().beginTransaction(config->settings());
142142

143143
size_t bytes_received = 0,
144144
bytes_sent = 0;
@@ -153,11 +153,11 @@ void SpiDispatcher::processSpiIoRequest(SpiIoTransaction * spi_io_transaction)
153153
else
154154
tx_byte = config->fill_symbol();
155155

156-
byte const rx_byte = SPI.transfer(tx_byte);
156+
byte const rx_byte = config->spi().transfer(tx_byte);
157157

158158
io_request->read_buf[bytes_received] = rx_byte;
159159
}
160-
SPI.endTransaction();
160+
config->spi().endTransaction();
161161

162162
config->deselect();
163163

src/wire/WireBusDevice.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class WireBusDevice : public BusDevice
3939
{
4040
public:
4141

42-
WireBusDevice(std::string const & spi_bus, WireBusDeviceConfig const & config)
42+
WireBusDevice(WireBusDeviceConfig const & config)
4343
: _config{config}
44-
{ /* TODO: Select Wire bus based in string. */ }
44+
{ }
4545

4646

4747
virtual IoResponse transfer(IoRequest & req) override

src/wire/WireBusDeviceConfig.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include <Arduino.h>
2727

28+
#include <Wire.h>
29+
2830
/**************************************************************************************
2931
* CLASS DECLARATION
3032
**************************************************************************************/
@@ -33,20 +35,23 @@ class WireBusDeviceConfig
3335
{
3436
public:
3537

36-
WireBusDeviceConfig(byte const slave_addr, bool const restart = true, bool const stop = true)
37-
: _slave_addr{slave_addr}
38+
WireBusDeviceConfig(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop)
39+
: _wire{wire}
40+
, _slave_addr{slave_addr}
3841
, _restart{restart}
3942
, _stop{stop}
4043
{ }
4144

4245

46+
inline arduino::HardwareI2C & wire() { return _wire; }
4347
inline byte slave_addr() const { return _slave_addr; }
4448
inline bool restart() const { return _restart; }
4549
inline bool stop() const { return _stop; }
4650

4751

4852
private:
4953

54+
arduino::HardwareI2C & _wire;
5055
byte _slave_addr{0x00};
5156
bool _restart{true}, _stop{true};
5257

src/wire/WireDispatcher.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,33 @@ void WireDispatcher::processWireIoRequest(WireIoTransaction * wire_io_transactio
136136
IoResponse io_response = wire_io_transaction->rsp;
137137
WireBusDeviceConfig * config = wire_io_transaction->config;
138138

139-
Wire.beginTransmission(config->slave_addr());
139+
config->wire().beginTransmission(config->slave_addr());
140140

141141
size_t bytes_written = 0;
142142
for (; bytes_written < io_request->bytes_to_write; bytes_written++)
143143
{
144-
Wire.write(io_request->write_buf[bytes_written]);
144+
config->wire().write(io_request->write_buf[bytes_written]);
145145
}
146146
io_response->bytes_written = bytes_written;
147147

148148
if (config->restart() && (io_request->bytes_to_read > 0))
149-
Wire.endTransmission(false /* stop */);
149+
config->wire().endTransmission(false /* stop */);
150150
else
151-
Wire.endTransmission(true /* stop */);
151+
config->wire().endTransmission(true /* stop */);
152152

153153
if (io_request->bytes_to_read > 0)
154154
{
155-
Wire.requestFrom(config->slave_addr(), io_request->bytes_to_read, config->stop());
155+
config->wire().requestFrom(config->slave_addr(), io_request->bytes_to_read, config->stop());
156156

157-
while(Wire.available() != static_cast<int>(io_request->bytes_to_read))
157+
while(config->wire().available() != static_cast<int>(io_request->bytes_to_read))
158158
{
159159
/* TODO: Insert a timeout. */
160160
}
161161

162162
size_t bytes_read = 0;
163163
for (; bytes_read < io_request->bytes_to_read; bytes_read++)
164164
{
165-
io_request->read_buf[bytes_read] = Wire.read();
165+
io_request->read_buf[bytes_read] = config->wire().read();
166166
}
167167
io_response->bytes_read = bytes_read;
168168
}

0 commit comments

Comments
 (0)