FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
mcp3x0x.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
22#ifndef MCP3X0X_HH
23#define MCP3X0X_HH
24
25#include "../spi.h"
26#include "../time.h"
27#include "../types_traits.h"
28#include "../utilities.h"
29
30namespace devices
31{
35 namespace mcp3x0x
36 {
37 }
38}
39
40namespace devices::mcp3x0x
41{
66 template<board::DigitalPin CS, typename CHANNEL_, uint16_t MASK, uint8_t RSHIFT, typename TYPE_ = uint16_t>
67 class MCP3x0x : public spi::SPIDevice<
68 CS, spi::ChipSelect::ACTIVE_LOW, spi::compute_clockrate(3'600'000UL),
69 spi::Mode::MODE_0, spi::DataOrder::MSB_FIRST>
70 {
72 static_assert(TRAIT::IS_INT && (TRAIT::SIZE == sizeof(uint16_t)), "TYPE must be uint16_t or int16_t");
73 static constexpr const bool IS_SIGNED = TRAIT::IS_SIGNED;
74 static constexpr const uint16_t SIGN_MASK = ((MASK >> RSHIFT) + 1) >> 1;
75 static constexpr const uint16_t NEGATIVE = 0xFFFF & ~(MASK >> RSHIFT);
76
77 public:
83 using CHANNEL = CHANNEL_;
84
89 using TYPE = TYPE_;
90
94 MCP3x0x() = default;
95
103 {
104 uint8_t result1;
105 uint8_t result2;
106 this->start_transfer();
107 if (sizeof(CHANNEL) == sizeof(uint16_t))
108 {
109 this->transfer(utils::high_byte(uint16_t(channel)));
110 result1 = this->transfer(utils::low_byte(uint16_t(channel)));
111 }
112 else
113 result1 = this->transfer(uint8_t(channel));
114 result2 = this->transfer(0x00);
115 this->end_transfer();
116 // Convert bytes pair to N-bits result
117 uint16_t value = (utils::as_uint16_t(result1, result2) & MASK) >> RSHIFT;
118 if (IS_SIGNED)
119 {
120 if (value & SIGN_MASK)
121 // value is negative, change it to negative int16_t
122 return TYPE(NEGATIVE | value);
123 else
124 // value is positive, directly cast it to int16_t
125 return TYPE(value);
126 }
127 else
128 return value;
129 }
130 };
131}
132
133#endif /* MCP3X0X_HH */
Generic class to support (almost) any chip of the SPI-based MicroChip ADC chips family (MCP3001-2-4-8...
Definition: mcp3x0x.h:70
TYPE_ TYPE
The analog output type for this device, either uint16_t or int16_t.
Definition: mcp3x0x.h:89
CHANNEL_ CHANNEL
The enum class type defining all possible analog input channels handled by the device.
Definition: mcp3x0x.h:83
TYPE read_channel(CHANNEL channel)
Read an analog channel from this device.
Definition: mcp3x0x.h:102
MCP3x0x()=default
Create a new device driver for an MCP chip.
uint8_t transfer(uint8_t data)
Transfer one byte to the currently selected SPI slave device through MOSI pin, and get the byte retur...
Definition: spi.h:169
Base class for any SPI slave device.
Definition: spi.h:331
Defines the API for MicroChip ADC chips family support.
Definition: mcp3001.h:27
Defines all API for all external devices supported by FastArduino.
constexpr uint8_t high_byte(uint16_t word)
Extract the high order byte of a 16-bits word.
Definition: utilities.h:316
constexpr uint16_t as_uint16_t(uint8_t high, uint8_t low)
Convert 2 bytes into an unsigned int.
Definition: utilities.h:327
constexpr uint8_t low_byte(uint16_t word)
Extract the low order byte of a 16-bits word.
Definition: utilities.h:308
This trait allows static checks (at compile-time) of properties of various types.
Definition: types_traits.h:47
static constexpr bool IS_INT
Indicates if T is an integer type.
Definition: types_traits.h:49
static constexpr bool IS_SIGNED
Indicates if T is a signed integer type.
Definition: types_traits.h:51
static constexpr size_t SIZE
Indicates the size in bytes of T.
Definition: types_traits.h:53