Skip to content

Instantly share code, notes, and snippets.

@MBHelectronics
MBHelectronics / HyperbolicTan.cpp
Last active September 9, 2018 03:33
fast range-switching calculation of tanh(x) using polynomial approximation and table lookup/root-finding in C++11
#include <inttypes.h>
#include <math.h>
#include <cstddef>
#include <iostream>
#define PRECISION (8)
#define MAX_ITER (10)
/*Storage for logarithm look-up table, will be in program memory
of Harvard architecture MPU */
@MBHelectronics
MBHelectronics / CircularBuffer.h
Last active September 21, 2018 04:38
A circular buffer
template <typename Derived>
class CircularBufferCRTP {
public:
template <typename T>
static Derived* create(size_t max_size) {
return Derived::template create_<T>(max_size);
}
template <typename T>
bool push(const T& in_data) {
#ifndef STATEFULALLOCATOR_H
#define STATEFULALLOCATOR_H
#include <cstdint>
#include <new>
#include <memory>
#include <array>
#include <vector>
#include "debug_msg.h"
#include "uP_allocator.h"
@MBHelectronics
MBHelectronics / smart_ptr.h
Last active November 25, 2018 06:08
Shared smart pointer for embedded systems
#ifndef SMART_PTR_H_INCLUDED
#define SMART_PTR_H_INCLUDED
#include <cstddef>
#include "uP_allocator.h"
namespace SmartPtr {
using namespace uP_allocator;
#ifndef ABSTRACTPRNG_H
#define ABSTRACTPRNG_H
#include <stdint.h>
#include <vector>
#include <algorithm>
#include <avr/wdt.h>
#include "uP_allocator.h"
namespace PRNG {
@MBHelectronics
MBHelectronics / IcoSphere.h
Created May 28, 2017 19:29
Make an IcoSphere in Allegro 5
#ifndef ICOSPHERE_H
#define ICOSPHERE_H
#include <allegro5/allegro.h>
#include <vector>
#include <map>
#include <algorithm>
#include <memory>
#include "GraphicsTypes.h"
#include "DisplayObject3D.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define BUFSIZE (64)
int get_msr_value(uint64_t* reg_value) {
const char* cmd = "rdmsr -u 0x1FC";
char cmd_buf[BUFSIZE];
#ifndef UP_POOL_STRING_H_INCLUDED
#define UP_POOL_STRING_H_INCLUDED
#include <cstddef>
#include <string>
#include "uP_allocator.h"
namespace uP_pool_string {
@MBHelectronics
MBHelectronics / test.cpp
Last active December 18, 2016 17:24
formatting test
// Allocate memory
pointer allocate(size_type count = 1, const_pointer /* hint */ = nullptr)
{
if (count > _m_num_free_blocks * max_size()) {
throw std::bad_alloc();
}
if (_m_num_initialized < _m_num_blocks) {
auto p = reinterpret_cast<size_t*>(_addr_from_index(_m_num_initialized));
*p = _m_num_initialized + 1;
#include <cstdint>
#include <vector>
#include <memory>
#include <algorithm>
#include <random>
#include <iostream>
template <typename T>
class Foo {