-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpiBusDeviceConfig.h
82 lines (64 loc) · 2.72 KB
/
SpiBusDeviceConfig.h
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
/*
* 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
*/
#ifndef SPI_BUS_DEVICE_CONFIG_H_
#define SPI_BUS_DEVICE_CONFIG_H_
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <Arduino.h>
#include <SPI.h>
#include <functional>
/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
class SpiBusDeviceConfig
{
public:
typedef std::function<void(void)> SpiSelectFunc;
typedef std::function<void(void)> SpiDeselectFunc;
SpiBusDeviceConfig(arduino::HardwareSPI & spi, SPISettings const & spi_settings, SpiSelectFunc spi_select, SpiDeselectFunc spi_deselect, byte const fill_symbol = 0xFF)
: _spi{spi}
, _spi_settings{spi_settings}
, _spi_select{spi_select}
, _spi_deselect{spi_deselect}
, _fill_symbol{fill_symbol}
{ }
SpiBusDeviceConfig(arduino::HardwareSPI & spi, SPISettings const & spi_settings, int const cs_pin, byte const fill_symbol = 0xFF)
: SpiBusDeviceConfig
{spi,
spi_settings,
[cs_pin](){ digitalWrite(cs_pin, LOW); },
[cs_pin](){ digitalWrite(cs_pin, HIGH); },
fill_symbol
}
{ }
arduino::HardwareSPI & spi() { return _spi; }
SPISettings settings () const { return _spi_settings; }
void select () const { if (_spi_select) _spi_select(); }
void deselect () const { if (_spi_deselect) _spi_deselect(); }
byte fill_symbol() const { return _fill_symbol; }
SpiSelectFunc select_func () const { return _spi_select; }
SpiDeselectFunc deselect_func() const { return _spi_deselect; }
private:
arduino::HardwareSPI & _spi;
SPISettings _spi_settings;
SpiSelectFunc _spi_select{nullptr};
SpiDeselectFunc _spi_deselect{nullptr};
byte _fill_symbol{0xFF};
};
#endif /* SPI_BUS_DEVICE_CONFIG_H_ */