Skip to content

Commit 77bc88b

Browse files
committed
Replace class BusDeviceCreator with static member function BusDeviceBase::create(...)
1 parent 93a70bf commit 77bc88b

11 files changed

+113
-156
lines changed

examples/ts_spi/ts_spi.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void bmp388_thread_func();
2525
* GLOBAL VARIABLES
2626
**************************************************************************************/
2727

28-
SpiBusDevice bmp388 = BusDeviceCreator.create(SPI, BMP388_CS_PIN, 1000000, MSBFIRST, SPI_MODE0);
28+
BusDevice bmp388 = BusDeviceBase::create(SPI, BMP388_CS_PIN, 1000000, MSBFIRST, SPI_MODE0);
2929

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

@@ -69,7 +69,7 @@ byte bmp388_read_reg(byte const reg_addr)
6969
byte read_buf[3] = {0};
7070

7171
IoRequest req(write_buf, sizeof(write_buf), read_buf, sizeof(read_buf));
72-
IoResponse rsp = bmp388.transfer(req);
72+
IoResponse rsp = bmp388->transfer(req);
7373

7474
/* Do other stuff */
7575

examples/ts_wire/ts_wire.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void lsm6dsox_thread_func();
2424
* GLOBAL VARIABLES
2525
**************************************************************************************/
2626

27-
WireBusDevice lsm6dsox = BusDeviceCreator.create(Wire, LSM6DSOX_ADDRESS);
27+
BusDevice lsm6dsox = BusDeviceBase::create(Wire, LSM6DSOX_ADDRESS);
2828

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

@@ -64,7 +64,7 @@ byte lsm6dsox_read_reg(byte const reg_addr)
6464
byte read_buf = 0;
6565

6666
IoRequest req(write_buf, read_buf);
67-
IoResponse rsp = lsm6dsox.transfer(req);
67+
IoResponse rsp = lsm6dsox->transfer(req);
6868

6969
/* Optionally do other stuff */
7070

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SpiBusDevice KEYWORD1
1212
SpiBusDeviceConfig KEYWORD1
1313
WireBusDevice KEYWORD1
1414
WireBusDeviceConfig KEYWORD1
15-
BusDeviceCreator KEYWORD1
15+
BusDevice KEYWORD1
1616

1717
#######################################
1818
# Methods and Functions (KEYWORD2)

src/Arduino_ThreadsafeIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* INCLUDE
2424
**************************************************************************************/
2525

26-
#include "BusDeviceCreator.h"
26+
#include "BusDevice.h"
2727
#include "spi/SpiBusDevice.h"
2828
#include "wire/WireBusDevice.h"
2929

src/BusDevice.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 "BusDevice.h"
24+
25+
#include "spi/SpiBusDevice.h"
26+
#include "wire/WireBusDevice.h"
27+
28+
/**************************************************************************************
29+
* PUBLIC MEMBER FUNCTIONS
30+
**************************************************************************************/
31+
32+
BusDevice BusDeviceBase(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol)
33+
{
34+
return BusDevice(new SpiBusDevice(SpiBusDeviceConfig{spi,
35+
spi_settings,
36+
cs_pin,
37+
fill_symbol
38+
}));
39+
}
40+
41+
BusDevice BusDeviceBase::create(arduino::HardwareSPI & spi, int const cs_pin, uint32_t const spi_clock, BitOrder const spi_bit_order, SPIMode const spi_bit_mode, byte const fill_symbol)
42+
{
43+
return BusDevice(new SpiBusDevice(SpiBusDeviceConfig{spi,
44+
SPISettings(spi_clock, spi_bit_order, spi_bit_mode),
45+
cs_pin,
46+
fill_symbol
47+
}));
48+
}
49+
50+
BusDevice BusDeviceBase::create(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol)
51+
{
52+
return BusDevice(new SpiBusDevice(SpiBusDeviceConfig{spi, spi_settings, spi_select, spi_deselect, fill_symbol}));
53+
}
54+
55+
BusDevice BusDeviceBase::create(arduino::HardwareI2C & wire, byte const slave_addr)
56+
{
57+
return create(wire, slave_addr, true, true);
58+
}
59+
60+
BusDevice BusDeviceBase::create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart)
61+
{
62+
return create(wire, slave_addr, restart, true);
63+
}
64+
65+
BusDevice BusDeviceBase::create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop)
66+
{
67+
return BusDevice(new WireBusDevice(WireBusDeviceConfig{wire, slave_addr, restart, stop}));
68+
}

src/BusDevice.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,47 @@
2525

2626
#include "IoTransaction.h"
2727

28+
#include "spi/SpiBusDeviceConfig.h"
29+
30+
/**************************************************************************************
31+
* FORWARD DECLARATION
32+
**************************************************************************************/
33+
34+
namespace arduino
35+
{
36+
class HardwareSPI;
37+
class HardwareI2C;
38+
}
39+
40+
class BusDeviceBase;
41+
42+
/**************************************************************************************
43+
* TYPEDEF
44+
**************************************************************************************/
45+
46+
typedef mbed::SharedPtr<BusDeviceBase> BusDevice;
47+
2848
/**************************************************************************************
2949
* CLASS DECLARATION
3050
**************************************************************************************/
3151

32-
class BusDevice
52+
class BusDeviceBase
3353
{
3454
public:
35-
virtual ~BusDevice() { }
55+
56+
virtual ~BusDeviceBase() { }
3657

3758
virtual IoResponse transfer(IoRequest & req) = 0;
59+
60+
61+
static BusDevice create(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
62+
static BusDevice create(arduino::HardwareSPI & 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);
63+
static BusDevice create(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
64+
65+
static BusDevice create(arduino::HardwareI2C & wire, byte const slave_addr);
66+
static BusDevice create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart);
67+
static BusDevice create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop);
68+
3869
};
3970

4071
#endif /* BUS_DEVICE_H_ */

src/BusDeviceCreator.cpp

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/BusDeviceCreator.h

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/spi/SpiBusDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* CLASS DECLARATION
3636
**************************************************************************************/
3737

38-
class SpiBusDevice : public BusDevice
38+
class SpiBusDevice : public BusDeviceBase
3939
{
4040
public:
4141

src/spi/SpiBusDeviceConfig.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class SpiBusDeviceConfig
4141
typedef std::function<void(void)> SpiDeselectFunc;
4242

4343

44-
SpiBusDeviceConfig(arduino::SPIClass & spi, SPISettings const & spi_settings, SpiSelectFunc spi_select, SpiDeselectFunc spi_deselect, byte const fill_symbol = 0xFF)
44+
SpiBusDeviceConfig(arduino::HardwareSPI & spi, SPISettings const & spi_settings, SpiSelectFunc spi_select, SpiDeselectFunc spi_deselect, byte const fill_symbol = 0xFF)
4545
: _spi{spi}
4646
, _spi_settings{spi_settings}
4747
, _spi_select{spi_select}
4848
, _spi_deselect{spi_deselect}
4949
, _fill_symbol{fill_symbol}
5050
{ }
5151

52-
SpiBusDeviceConfig(arduino::SPIClass & spi, SPISettings const & spi_settings, int const cs_pin, byte const fill_symbol = 0xFF)
52+
SpiBusDeviceConfig(arduino::HardwareSPI & spi, SPISettings const & spi_settings, int const cs_pin, byte const fill_symbol = 0xFF)
5353
: SpiBusDeviceConfig
5454
{spi,
5555
spi_settings,
@@ -60,7 +60,7 @@ class SpiBusDeviceConfig
6060
{ }
6161

6262

63-
arduino::SPIClass & spi() { return _spi; }
63+
arduino::HardwareSPI & spi() { return _spi; }
6464
SPISettings settings () const { return _spi_settings; }
6565
void select () const { if (_spi_select) _spi_select(); }
6666
void deselect () const { if (_spi_deselect) _spi_deselect(); }
@@ -69,7 +69,7 @@ class SpiBusDeviceConfig
6969

7070
private:
7171

72-
arduino::SPIClass & _spi;
72+
arduino::HardwareSPI & _spi;
7373
SPISettings _spi_settings;
7474
SpiSelectFunc _spi_select{nullptr};
7575
SpiDeselectFunc _spi_deselect{nullptr};

src/wire/WireBusDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* CLASS DECLARATION
3636
**************************************************************************************/
3737

38-
class WireBusDevice : public BusDevice
38+
class WireBusDevice : public BusDeviceBase
3939
{
4040
public:
4141

0 commit comments

Comments
 (0)