REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
TMP127Q1.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include "TMP127Q1.h"
7
8const SPISettings functions::TMP127Q1::DEFAULT_SPI_SETTINGS{4'000'000, MSBFIRST, SPI_MODE0};
9
11 auto raw = transfer16(0);
12 return raw_to_float(raw);
13}
14
16 auto signed_raw = raw_to_signed_raw(raw);
17 return static_cast<float>(signed_raw) * 0.03125f;
18}
19
21 // Convert to signed integer and use arithmetic right-shift, which keeps sign bit.
22 // Last two bits are always 1 and need to be shifted out to result in 14bit value.
23 uint16_t b = (a >> 2);
24 // However, for proper two complement after the shift we need to add the two 1
25 // again from the left.
26 if(a >= 0xC000) b |= 0xC000;
27 int16_t c = (int16_t) b;
28 return c;
29}
30
31functions::TMP127Q1::TMP127Q1(unsigned short address) : DataFunction(address, DEFAULT_SPI_SETTINGS) {}
A DataFunction class wraps SPI communication over the digital bus.
Definition functions.h:47
uint16_t transfer16(uint16_t data_in) const
TMP127Q1(unsigned short address)
Definition TMP127Q1.cpp:31
static float raw_to_float(uint16_t raw)
Definition TMP127Q1.cpp:15
static int16_t raw_to_signed_raw(uint16_t raw)
Definition TMP127Q1.cpp:20
static const SPISettings DEFAULT_SPI_SETTINGS
Definition TMP127Q1.h:8
float read_temperature() const
Definition TMP127Q1.cpp:10