SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorLib.h
Go to the documentation of this file.
1
30#pragma once
31
32#if defined(ARDUINO)
33#include <Arduino.h>
34#include <SPI.h>
35#include <Wire.h>
36#else
37#include <stdint.h>
38#include <stdlib.h>
39#include <string.h>
40#endif
41
42#include "SensorBuildOpt.h"
43#include "SensorLib_Version.h"
44
45#if defined(INCLUDE_DEVICES_PINS)
46#include "DevicesPins.h"
47#endif
48
49#if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)
50#define SPIClass SPIClassRP2040
51#endif
52
53// ═══════════════════════════════════════════════════════════════════════════
54// Core utilities in namespace sensorlib
55// ═══════════════════════════════════════════════════════════════════════════
56
57namespace sensorlib
58{
59
60// ── Bit shift helpers (constexpr, type-safe) ────────────────────────────────
61
62constexpr uint32_t _bv(uint8_t b)
63{
64 return static_cast<uint32_t>(1) << b;
65}
66
67// ── Byte extraction ─────────────────────────────────────────────────────────
68
69constexpr uint8_t _lowByte(uint16_t w)
70{
71 return static_cast<uint8_t>(w & 0xFF);
72}
73constexpr uint8_t _highByte(uint16_t w)
74{
75 return static_cast<uint8_t>(w >> 8);
76}
77
78// ── Bit manipulation (template, type-safe) ──────────────────────────────────
79
80template<typename T>
81constexpr T _bitRead(T value, uint8_t bit)
82{
83 return (value >> bit) & static_cast<T>(1);
84}
85
86template<typename T>
87inline void _bitSet(T &value, uint8_t bit)
88{
89 value |= (static_cast<T>(1) << bit);
90}
91
92template<typename T>
93inline void _bitClear(T &value, uint8_t bit)
94{
95 value &= ~(static_cast<T>(1) << bit);
96}
97
98template<typename T>
99inline void _bitToggle(T &value, uint8_t bit)
100{
101 value ^= (static_cast<T>(1) << bit);
102}
103
104template<typename T>
105inline void _bitWrite(T &value, uint8_t bit, bool bitvalue)
106{
107 if (bitvalue) _bitSet(value, bit);
108 else _bitClear(value, bit);
109}
110
111template<typename T>
112constexpr bool _isBitSet(T value, uint8_t bit)
113{
114 return (value & (static_cast<T>(1) << bit)) == (static_cast<T>(1) << bit);
115}
116
117} // namespace sensorlib
118
119#if !defined(ARDUINO) && defined(ESP_PLATFORM)
120
121#ifndef INPUT
122#define INPUT (0x0)
123#endif
124
125#ifndef OUTPUT
126#define OUTPUT (0x1)
127#endif
128
129#ifndef RISING
130#define RISING (0x01)
131#endif
132
133#ifndef FALLING
134#define FALLING (0x02)
135#endif
136
137#ifndef LOW
138#define LOW (0)
139#endif
140
141#ifndef HIGH
142#define HIGH (1)
143#endif
144
145#endif
146
@license MIT License
@license MIT License
constexpr uint32_t _bv(uint8_t b)
Definition SensorLib.h:62
void _bitClear(T &value, uint8_t bit)
Definition SensorLib.h:93
constexpr uint8_t _lowByte(uint16_t w)
Definition SensorLib.h:69
void _bitToggle(T &value, uint8_t bit)
Definition SensorLib.h:99
void _bitWrite(T &value, uint8_t bit, bool bitvalue)
Definition SensorLib.h:105
void _bitSet(T &value, uint8_t bit)
Definition SensorLib.h:87
constexpr bool _isBitSet(T value, uint8_t bit)
Definition SensorLib.h:112
constexpr uint8_t _highByte(uint16_t w)
Definition SensorLib.h:73
constexpr T _bitRead(T value, uint8_t bit)
Definition SensorLib.h:81