|
| 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 | +} |
0 commit comments