Compare commits

..

3 Commits

Author SHA1 Message Date
Ikatono
805925c92d started Neopixel structs 2023-12-19 01:38:32 -06:00
2185c25f12 added Queue.h 2023-12-08 22:42:15 -06:00
1b1734c15e added workspace file 2023-12-08 21:22:21 -06:00
9 changed files with 296 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
{
"folders": [
{
"path": "."
},
{
"path": "../../pico-sdk"
}
],
"settings": {}
}

View File

@@ -0,0 +1,21 @@
#include "CriticalSection.h"
CriticalSection::CriticalSection()
{
critical_section_init(&_critical);
}
void CriticalSection::lock()
{
critical_section_enter_blocking(&_critical);
}
void CriticalSection::unlock()
{
critical_section_exit(&_critical);
}
CriticalSection::~CriticalSection()
{
critical_section_deinit(&_critical);
}

View File

@@ -0,0 +1,20 @@
#ifndef H_10337EC23C94444EB1628220BA87E2D7
#define H_10337EC23C94444EB1628220BA87E2D7
#include "pico/sync.h"
class CriticalSection
{
private:
critical_section_t _critical;
public:
CriticalSection();
void lock();
void unlock();
~CriticalSection();
};
#endif //H_10337EC23C94444EB1628220BA87E2D7

View File

@@ -0,0 +1,16 @@
#include "Mutex.h"
Mutex::Mutex()
{
mutex_init(&_mutex);
}
void Mutex::lock()
{
mutex_enter_blocking(&_mutex);
}
void Mutex::unlock()
{
mutex_exit(&_mutex);
}

View File

@@ -0,0 +1,18 @@
#ifndef H_A295368D75B1464CB9B5336EEFFB6239
#define H_A295368D75B1464CB9B5336EEFFB6239
#include "pico/stdlib.h"
#include "pico/mutex.h"
class Mutex
{
private:
mutex_t _mutex;
public:
Mutex();
void lock();
void unlock();
};
#endif //H_A295368D75B1464CB9B5336EEFFB6239

View File

@@ -0,0 +1,42 @@
#include "NeoPacket.h"
namespace NeoPacket
{
GreenPixel& GreenPixel::operator=(GreenPixel& other)
{
this->value = other.value;
return *this;
}
GreenPixel& GreenPixel::operator=(uint8_t val)
{
this->value = val;
return *this;
}
RedPixel& RedPixel::operator=(RedPixel& other)
{
this->value = other.value;
return *this;
}
RedPixel& RedPixel::operator=(uint8_t val)
{
this->value = val;
return *this;
}
BluePixel& BluePixel::operator=(BluePixel& other)
{
this->value = other.value;
return *this;
}
BluePixel& BluePixel::operator=(uint8_t val)
{
this->value = val;
return *this;
}
}

View File

@@ -0,0 +1,83 @@
#ifndef H_3FE5AF534D5E46BD9700F872DB5C50CD
#define H_3FE5AF534D5E46BD9700F872DB5C50CD
#include <array>
#include <cstdint>
namespace NeoPacket
{
class RedPixel
{
public:
RedPixel& operator=(RedPixel& other);
RedPixel& operator=(uint8_t val);
uint8_t val() const;
private:
uint8_t padding1;
uint8_t value;
uint8_t padding2;
};
class GreenPixel
{
public:
GreenPixel& operator=(GreenPixel& other);
GreenPixel& operator=(uint8_t val);
uint8_t val() const;
private:
uint8_t value;
uint8_t padding1;
uint8_t padding2;
};
class BluePixel
{
public:
BluePixel& operator=(BluePixel& other);
BluePixel& operator=(uint8_t val);
uint8_t val() const;
private:
uint8_t padding1;
uint8_t padding2;
uint8_t value;
};
class Pixel
{
public:
uint8_t green;
uint8_t red;
uint8_t blue;
void apply(GreenPixel g);
void apply(RedPixel r);
void apply(BluePixel b);
};
template <std::size_t N>
class NeoPacket
{
public:
std::array<GreenPixel, N>& as_green()
{
return reinterpret_cast<std::array<GreenPixel, N>&>(*this);
}
std::array<RedPixel, N>& as_red()
{
return reinterpret_cast<std::array<RedPixel, N>&>(*this);
}
std::array<BluePixel, N>& as_blue()
{
return reinterpret_cast<std::array<BluePixel, N>&>(*this);
}
private:
std::array<Pixel, N> data;
};
}
#endif //H_3FE5AF534D5E46BD9700F872DB5C50CD

View File

@@ -0,0 +1,85 @@
#ifndef H_97BF73781549459A88697C808E132C22
#define H_97BF73781549459A88697C808E132C22
#include "pico/util/queue.h"
#include <type_traits>
//#include <utility>
template <typename T>
class Queue
{
private:
queue_t queue;
public:
static constexpr bool wrapped = !std::is_pod<T>();
Queue(uint size)
{
if constexpr (wrapped)
{
queue_init(&queue, sizeof(T*), size);
}
else
{
queue_init(&queue, sizeof(T), size);
}
}
bool try_add(T *item)
{
if constexpr (wrapped)
{
T* temp = new T(*item);
return queue_try_add(&queue, &temp);
}
else
{
return queue_try_add(&queue, item);
}
}
void blocking_add(T* item)
{
if constexpr (wrapped)
{
T* temp = new T(*item);
queue_add_blocking(&queue, &temp);
}
else
{
queue_add_blocking(&queue, item);
}
}
bool try_remove(T* item)
{
if constexpr (wrapped)
{
T** temp;
bool success = queue_try_remove(&queue, temp);
if (success)
*item = **temp;
return success;
}
else
{
return queue_try_remove(&queue, item);
}
}
void blocking_remove(T* item)
{
if constexpr (wrapped)
{
T** temp;
queue_remove_blocking(&queue, temp);
*item = **temp;
}
else
{
queue_remove_blocking(&queue, item);
}
}
~Queue()
{
queue_free(&queue);
}
};
#endif //H_97BF73781549459A88697C808E132C22

View File