Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
GyverLibs committed Dec 20, 2024
1 parent 7ed356a commit 0f9cc45
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 53 deletions.
39 changes: 39 additions & 0 deletions examples/flags/flags.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <Arduino.h>
#include <BitFlags.h>

#define MY_FLAG_0 bit(0)
#define KEK_FLAG bit(1)
#define SOME_F bit(2)

enum class Flags {
f1 = bit(0),
f2 = bit(1),
f3 = bit(2),
};

void setup() {
Serial.begin(115200);
{
BitFlags8 f;
f.set(Flags::f1);
f.set(Flags::f2);

Serial.println(f.read(Flags::f1));
Serial.println(f.read(Flags::f2));
Serial.println(f.read(Flags::f3));
}
{
BitFlags8 flags;
flags.set(KEK_FLAG | SOME_F); // установить два флага
Serial.println(flags.read(KEK_FLAG | SOME_F)); // стоит один из флагов
Serial.println(flags.isSet(KEK_FLAG | SOME_F)); // стоят все флаги

// операция compare берёт маску по первому аргументу и сравнивает со вторым
// фактически смысл такой: определение ситуации, когда из указанных флагов подняты только определённые
// здесь - из флагов KEK_FLAG и SOME_F поднят только SOME_F (KEK_FLAG опущен)
Serial.println(flags.compare(KEK_FLAG | SOME_F, SOME_F));
}
}

void loop() {
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=BitPack
version=1.3.4
version=1.3.5
author=AlexGyver <[email protected]>
maintainer=AlexGyver <[email protected]>
sentence=Library for packing bit flags into byte array
Expand Down
61 changes: 61 additions & 0 deletions src/BitFlags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once
#include <inttypes.h>

// ================= BIT FLAGS =================
template <typename T>
struct BitFlags {
// пакет флагов
T flags = 0;

// прочитать бит
template <typename T1>
inline bool read(const T1 x) const {
return flags & (T)x;
}

// установить биты маской
template <typename T1>
inline void set(const T1 x) {
flags |= (T)x;
}

// очистить биты маской
template <typename T1>
inline void clear(const T1 x) {
flags &= ~((T)x);
}

// записать бит
template <typename T1>
inline void write(const T1 x, const bool v) {
v ? set(x) : clear(x);
}

// получить маску
template <typename T1>
inline T mask(const T1 x) const {
return flags & (T)x;
}

// стоят все биты в маске
template <typename T1>
inline bool isSet(const T1 x) const {
return (flags & (T)x) == (T)x;
}

// очищены все биты в маске
template <typename T1>
inline bool isClear(const T1 x) const {
return !(flags & (T)x);
}

// сравнить маску со значением
template <typename T1, typename T2>
inline bool compare(const T1 x, const T2 y) const {
return (flags & (T)x) == (T)y;
}
};

struct BitFlags8 : public BitFlags<uint8_t> {};
struct BitFlags16 : public BitFlags<uint16_t> {};
struct BitFlags32 : public BitFlags<uint32_t> {};
54 changes: 2 additions & 52 deletions src/BitPack.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#pragma once
#include <Arduino.h>
#include "BitFlags.h"

#define BP_BYTE(pack, idx) pack[(idx) >> 3]
#define BP_BIT(pack, idx) ((idx) & 0b111)
Expand Down Expand Up @@ -282,55 +283,4 @@ class BitPackDyn : public BitPackExt {
_amount = rval._amount;
rval.pack = nullptr;
}
};

// ================= BIT FLAGS =================
template <typename T>
struct BitFlags {
// пакет флагов
T flags = 0;

// прочитать бит
inline bool read(const T x) const __attribute__((always_inline)) {
return flags & x;
}

// установить биты маской
inline void set(const T x) __attribute__((always_inline)) {
flags |= x;
}

// очистить биты маской
inline void clear(const T x) __attribute__((always_inline)) {
flags &= ~x;
}

// записать бит
inline void write(const T x, const bool v) __attribute__((always_inline)) {
v ? set(x) : clear(x);
}

// получить маску
inline T mask(const T x) const __attribute__((always_inline)) {
return flags & x;
}

// стоят все биты в маске
inline bool isSet(const T x) const __attribute__((always_inline)) {
return (flags & x) == x;
}

// очищены все биты в маске
inline bool isClear(const T x) const __attribute__((always_inline)) {
return !(flags & x);
}

// сравнить маску со значением
inline bool compare(const T x, const T y) const __attribute__((always_inline)) {
return (flags & x) == y;
}
};

struct BitFlags8 : public BitFlags<uint8_t> {};
struct BitFlags16 : public BitFlags<uint16_t> {};
struct BitFlags32 : public BitFlags<uint32_t> {};
};

0 comments on commit 0f9cc45

Please sign in to comment.