Macro.h

mail@pastecode.io avatar
unknown
c_cpp
a month ago
1.7 kB
3
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;

  MacroData(uint32_t _currentRun = 0) : currentRun(_currentRun) {}
};

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, MacroData data = MacroData());
void registerRepeatedMacro(RepeatedMacroFunction fn, byte button, MacroData data = MacroData());
void updateMacros(byte buttonStates);

#endif
Leave a Comment