Skip to content

Commit fdb24c0

Browse files
authored
Merge pull request #5 from bcmi-labs/use-ctor-for-object-creation
Replace creation class/method via construction by Ctor
2 parents 93a70bf + 368e6a1 commit fdb24c0

11 files changed

+171
-154
lines changed

Diff for: examples/ts_spi/ts_spi.ino

+1-1
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(SPI, BMP388_CS_PIN, 1000000, MSBFIRST, SPI_MODE0);
2929

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

Diff for: 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 = BusDeviceCreator.create(Wire, LSM6DSOX_ADDRESS);
27+
BusDevice lsm6dsox(Wire, LSM6DSOX_ADDRESS);
2828

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

Diff for: keywords.txt

+1-1
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)

Diff for: src/Arduino_ThreadsafeIO.h

+1-1
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

Diff for: src/BusDevice.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
* BusDeviceBase PUBLIC MEMBER FUNCTIONS
30+
**************************************************************************************/
31+
32+
BusDevice BusDeviceBase::create(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+
}
69+
70+
/**************************************************************************************
71+
* BusDevice CTOR/DTOR
72+
**************************************************************************************/
73+
74+
BusDevice::BusDevice(BusDeviceBase * dev)
75+
: _dev{dev}
76+
{ }
77+
78+
BusDevice::BusDevice(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol)
79+
{
80+
*this = BusDeviceBase::create(spi, cs_pin, spi_settings, fill_symbol);
81+
}
82+
83+
BusDevice::BusDevice(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)
84+
{
85+
*this = BusDeviceBase::create(spi, cs_pin, spi_clock, spi_bit_order, spi_bit_mode, fill_symbol);
86+
}
87+
88+
BusDevice::BusDevice(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol)
89+
{
90+
*this = BusDeviceBase::create(spi, spi_select, spi_deselect, spi_settings, fill_symbol);
91+
}
92+
93+
BusDevice::BusDevice(arduino::HardwareI2C & wire, byte const slave_addr)
94+
{
95+
*this = BusDeviceBase::create(wire, slave_addr);
96+
}
97+
98+
BusDevice::BusDevice(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart)
99+
{
100+
*this = BusDeviceBase::create(wire, slave_addr, restart);
101+
}
102+
103+
BusDevice::BusDevice(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop)
104+
{
105+
*this = BusDeviceBase::create(wire, slave_addr, restart, stop);
106+
}
107+
108+
IoResponse BusDevice::transfer(IoRequest & req)
109+
{
110+
return _dev->transfer(req);
111+
}

Diff for: src/BusDevice.h

+50-2
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,64 @@
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 BusDevice;
41+
2842
/**************************************************************************************
2943
* CLASS DECLARATION
3044
**************************************************************************************/
3145

32-
class BusDevice
46+
class BusDeviceBase
3347
{
3448
public:
35-
virtual ~BusDevice() { }
49+
50+
virtual ~BusDeviceBase() { }
3651

3752
virtual IoResponse transfer(IoRequest & req) = 0;
53+
54+
55+
static BusDevice create(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
56+
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);
57+
static BusDevice create(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
58+
59+
static BusDevice create(arduino::HardwareI2C & wire, byte const slave_addr);
60+
static BusDevice create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart);
61+
static BusDevice create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop);
62+
63+
};
64+
65+
class BusDevice
66+
{
67+
public:
68+
69+
BusDevice(BusDeviceBase * dev);
70+
71+
BusDevice(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
72+
BusDevice(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);
73+
BusDevice(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol = 0xFF);
74+
75+
BusDevice(arduino::HardwareI2C & wire, byte const slave_addr);
76+
BusDevice(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart);
77+
BusDevice(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop);
78+
79+
IoResponse transfer(IoRequest & req);
80+
81+
82+
private:
83+
84+
mbed::SharedPtr<BusDeviceBase> _dev;
85+
3886
};
3987

4088
#endif /* BUS_DEVICE_H_ */

Diff for: src/BusDeviceCreator.cpp

-84
This file was deleted.

Diff for: src/BusDeviceCreator.h

-58
This file was deleted.

Diff for: src/spi/SpiBusDevice.h

+1-1
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

0 commit comments

Comments
 (0)