-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBusDevice.cpp
121 lines (100 loc) · 4.86 KB
/
BusDevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* This file is part of the Arduino_ThreadsafeIO library.
* Copyright (c) 2021 Arduino SA.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "BusDevice.h"
#include "spi/SpiBusDevice.h"
#include "wire/WireBusDevice.h"
/**************************************************************************************
* BusDeviceBase PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
BusDevice BusDeviceBase::create(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol)
{
return BusDevice(new SpiBusDevice(SpiBusDeviceConfig{spi,
spi_settings,
cs_pin,
fill_symbol
}));
}
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)
{
return BusDevice(new SpiBusDevice(SpiBusDeviceConfig{spi,
SPISettings(spi_clock, spi_bit_order, spi_bit_mode),
cs_pin,
fill_symbol
}));
}
BusDevice BusDeviceBase::create(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol)
{
return BusDevice(new SpiBusDevice(SpiBusDeviceConfig{spi, spi_settings, spi_select, spi_deselect, fill_symbol}));
}
BusDevice BusDeviceBase::create(arduino::HardwareI2C & wire, byte const slave_addr)
{
return create(wire, slave_addr, true, true);
}
BusDevice BusDeviceBase::create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart)
{
return create(wire, slave_addr, restart, true);
}
BusDevice BusDeviceBase::create(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop)
{
return BusDevice(new WireBusDevice(WireBusDeviceConfig{wire, slave_addr, restart, stop}));
}
/**************************************************************************************
* BusDevice CTOR/DTOR
**************************************************************************************/
BusDevice::BusDevice(BusDeviceBase * dev)
: _dev{dev}
{ }
BusDevice::BusDevice(arduino::HardwareSPI & spi, int const cs_pin, SPISettings const & spi_settings, byte const fill_symbol)
{
*this = BusDeviceBase::create(spi, cs_pin, spi_settings, fill_symbol);
}
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)
{
*this = BusDeviceBase::create(spi, cs_pin, spi_clock, spi_bit_order, spi_bit_mode, fill_symbol);
}
BusDevice::BusDevice(arduino::HardwareSPI & spi, SpiBusDeviceConfig::SpiSelectFunc spi_select, SpiBusDeviceConfig::SpiDeselectFunc spi_deselect, SPISettings const & spi_settings, byte const fill_symbol)
{
*this = BusDeviceBase::create(spi, spi_select, spi_deselect, spi_settings, fill_symbol);
}
BusDevice::BusDevice(arduino::HardwareI2C & wire, byte const slave_addr)
{
*this = BusDeviceBase::create(wire, slave_addr);
}
BusDevice::BusDevice(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart)
{
*this = BusDeviceBase::create(wire, slave_addr, restart);
}
BusDevice::BusDevice(arduino::HardwareI2C & wire, byte const slave_addr, bool const restart, bool const stop)
{
*this = BusDeviceBase::create(wire, slave_addr, restart, stop);
}
IoResponse BusDevice::transfer(IoRequest & req)
{
return _dev->transfer(req);
}
SpiBusDevice & BusDevice::spi()
{
return *reinterpret_cast<SpiBusDevice *>(_dev.get());
}
WireBusDevice & BusDevice::wire()
{
return *reinterpret_cast<WireBusDevice *>(_dev.get());
}