-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters