-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWireBusDevice.cpp
80 lines (69 loc) · 2.97 KB
/
WireBusDevice.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
/*
* 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 "WireBusDevice.h"
#include "WireDispatcher.h"
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
WireBusDevice::WireBusDevice(WireBusDeviceConfig const & config)
: _config{config}
{
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
IoResponse WireBusDevice::transfer(IoRequest & req)
{
return WireDispatcher::instance().dispatch(&req, &_config);
}
bool WireBusDevice::read(uint8_t * buffer, size_t len, bool stop)
{
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), _config.restart(), stop);
IoRequest req(nullptr, 0, buffer, len);
IoResponse rsp = WireDispatcher::instance().dispatch(&req, &config);
rsp->wait();
return true;
}
bool WireBusDevice::write(uint8_t * buffer, size_t len, bool stop)
{
bool const restart = !stop;
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), restart, _config.stop());
IoRequest req(buffer, len, nullptr, 0);
IoResponse rsp = WireDispatcher::instance().dispatch(&req, &config);
rsp->wait();
return true;
}
bool WireBusDevice::write_then_read(uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop)
{
/* Copy the Wire parameters from the device and modify only those
* which can be modified via the parameters of this function.
*/
bool const restart = !stop;
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), restart, _config.stop());
/* Fire off the IO request and await its response. */
IoRequest req(write_buffer, write_len, read_buffer, read_len);
IoResponse rsp = WireDispatcher::instance().dispatch(&req, &config);
rsp->wait();
/* TODO: Introduce error codes within the IoResponse and evaluate
* them here.
*/
return true;
}