Macro.h

mail@pastecode.io avatar
unknown
c_cpp
25 days ago
1.8 kB
1
Indexable
Never
#ifndef MACRO_h
#define MACRO_h

#ifndef BUTTON_LENGTH
  #define BUTTON_LENGTH 8
#endif

#include <Arduino.h>
#include <LinkedList.h>

struct MacroData {
  uint32_t currentRun;
  void* customData;

  MacroData(void* _customData = nullptr) : currentRun(0), customData(_customData) {}
};

struct NormalMacroEvent {
  MacroData* macroData;

  NormalMacroEvent(MacroData* _macroData = nullptr) : macroData(_macroData) {}
};

struct RepeatedMacroEvent {
  MacroData* macroData;
  long currentButtonMillis;
  long deltaMicros;
  uint16_t currentButtonHits;

  RepeatedMacroEvent(MacroData* _macroData = nullptr, long _currentButtonMillis = 0, long _deltaMicros = 0, uint16_t _currentButtonHits = 0)
    : macroData(_macroData), currentButtonMillis(_currentButtonMillis), deltaMicros(_deltaMicros), currentButtonHits(_currentButtonHits) {}
};

struct ButtonData {
  uint16_t hits;
  unsigned long pressedStartTimeMillis;
};

using NormalMacroFunction = void (*)(NormalMacroEvent*);
using RepeatedMacroFunction = void (*)(RepeatedMacroEvent*);

struct NormalMacro {
  NormalMacroFunction fn;
  MacroData data;

  NormalMacro(NormalMacroFunction _fn = nullptr, MacroData _data = MacroData()) : fn(_fn), data(_data) {}
};
struct RepeatedMacro {
  RepeatedMacroFunction fn;
  MacroData data;

  RepeatedMacro(RepeatedMacroFunction _fn = nullptr, MacroData _data = MacroData()) : fn(_fn), data(_data) {}
};

void registerNormalMacro(NormalMacroFunction fn, byte button);
void registerRepeatedMacro(RepeatedMacroFunction fn, byte button);

template<typename CustomData>
CustomData* registerNormalMacro(NormalMacroFunction fn, byte button);
template<typename CustomData>
CustomData* registerRepeatedMacro(RepeatedMacroFunction fn, byte button);

void updateMacros(byte buttonStates);

#endif
Leave a Comment