acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

AutoBenchmark.ino (5808B)

      1 /*
      2  * A program that prints out the time (min/avg/max) taken by the
      3  * AceButton::check() method.
      4  */
      5 
      6 #include <AceButton.h>
      7 #include "ProfilingButtonConfig.h"
      8 using namespace ace_button;
      9 
     10 // The pin number attached to the button.
     11 const int BUTTON_PIN = 2;
     12 
     13 ProfilingButtonConfig buttonConfig;
     14 
     15 // One button wired using the ProfilingButtonConfig.
     16 AceButton button(&buttonConfig);
     17 
     18 const unsigned long STATS_PRINT_INTERVAL = 2000;
     19 unsigned long lastStatsPrintedTime;
     20 TimingStats stats;
     21 
     22 const uint8_t LOOP_MODE_START = 0;
     23 const uint8_t LOOP_MODE_IDLE = 1;
     24 const uint8_t LOOP_MODE_PRESS_RELEASE = 2;
     25 const uint8_t LOOP_MODE_CLICK = 3;
     26 const uint8_t LOOP_MODE_DOUBLE_CLICK = 4;
     27 const uint8_t LOOP_MODE_LONG_PRESS = 5;
     28 const uint8_t LOOP_MODE_END = 6;
     29 uint8_t loopMode;
     30 uint8_t loopEventType;
     31 
     32 void handleEvent(AceButton*, uint8_t, uint8_t);
     33 
     34 void setup() {
     35   delay(1000); // some microcontrollers reboot twice
     36   Serial.begin(115200);
     37   while (!Serial); // for Leonardo/Micro
     38   Serial.println(F("setup(): begin"));
     39 
     40   // Button uses the built-in pull up register.
     41   pinMode(BUTTON_PIN, INPUT_PULLUP);
     42   button.init(BUTTON_PIN);
     43 
     44   // Configure the ButtonConfig with the event handler, and enable all higher
     45   // level events.
     46   buttonConfig.setEventHandler(handleEvent);
     47   buttonConfig.setFeature(ButtonConfig::kFeatureClick);
     48   buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick);
     49   buttonConfig.setFeature(ButtonConfig::kFeatureLongPress);
     50   buttonConfig.setFeature(ButtonConfig::kFeatureRepeatPress);
     51   buttonConfig.setFeature(ButtonConfig::kFeatureSuppressAll);
     52   buttonConfig.setTimingStats(&stats);
     53 
     54   lastStatsPrintedTime = millis();
     55   loopMode = LOOP_MODE_START;
     56   loopEventType = AceButton::kEventPressed;
     57 
     58   Serial.println(F("setup(): end"));
     59 }
     60 
     61 void loop() {
     62   delay(1); // Decrease sampling frequency to about 1000 Hz
     63   button.check();
     64 
     65   switch (loopMode) {
     66     case LOOP_MODE_START:
     67       loopStart();
     68       break;
     69     case LOOP_MODE_IDLE:
     70       loopIdle();
     71       break;
     72     case LOOP_MODE_PRESS_RELEASE:
     73       loopPressRelease();
     74       break;
     75     case LOOP_MODE_CLICK:
     76       loopClick();
     77       break;
     78     case LOOP_MODE_DOUBLE_CLICK:
     79       loopDoubleClick();
     80       break;
     81     case LOOP_MODE_LONG_PRESS:
     82       loopLongPress();
     83       break;
     84     case LOOP_MODE_END:
     85       loopEnd();
     86       break;
     87   }
     88 }
     89 
     90 void loopStart() {
     91   static unsigned long start = millis();
     92 
     93   // Wait one iteration for things to cool down.
     94   if (millis() - start > STATS_PRINT_INTERVAL) {
     95     Serial.println(F("------------------------+-------------+---------+"));
     96     Serial.println(F("button event            | min/avg/max | samples |"));
     97     Serial.println(F("------------------------+-------------+---------+"));
     98     nextMode();
     99   }
    100 }
    101 
    102 void loopEnd() {
    103   Serial.println(F("------------------------+-------------+---------+"));
    104   nextMode();
    105 }
    106 
    107 void loopIdle() {
    108   static unsigned long start = millis();
    109 
    110   if (millis() - start > STATS_PRINT_INTERVAL) {
    111     Serial.print(F("idle                    | "));
    112     printStats();
    113     Serial.println(F("    |"));
    114     nextMode();
    115   }
    116 }
    117 
    118 void loopPressRelease() {
    119   static unsigned long start = millis();
    120 
    121   unsigned long now = millis();
    122   unsigned long elapsed = now - start;
    123   if (100 <= elapsed && elapsed < 1000) buttonConfig.setButtonState(LOW);
    124   if (1000 <= elapsed) buttonConfig.setButtonState(HIGH);
    125 
    126   if (millis() - start > STATS_PRINT_INTERVAL) {
    127     if (loopEventType == AceButton::kEventReleased) {
    128       Serial.print(F("press/release           | "));
    129       printStats();
    130       Serial.println(F("    |"));
    131     }
    132     nextMode();
    133   }
    134 }
    135 
    136 void loopClick() {
    137   static unsigned long start = millis();
    138 
    139   unsigned long now = millis();
    140   unsigned long elapsed = now - start;
    141   if (100 <= elapsed && elapsed < 200) buttonConfig.setButtonState(LOW);
    142   if (200 <= elapsed) buttonConfig.setButtonState(HIGH);
    143 
    144   if (millis() - start > STATS_PRINT_INTERVAL) {
    145     if (loopEventType == AceButton::kEventClicked) {
    146       Serial.print(F("click                   | "));
    147       printStats();
    148       Serial.println(F("    |"));
    149     }
    150     nextMode();
    151   }
    152 }
    153 
    154 void loopDoubleClick() {
    155   static unsigned long start = millis();
    156 
    157   unsigned long now = millis();
    158   unsigned long elapsed = now - start;
    159   if (100 <= elapsed && elapsed < 200) buttonConfig.setButtonState(LOW);
    160   if (200 <= elapsed && elapsed < 300) buttonConfig.setButtonState(HIGH);
    161   if (300 <= elapsed && elapsed < 400) buttonConfig.setButtonState(LOW);
    162   if (400 <= elapsed) buttonConfig.setButtonState(HIGH);
    163 
    164   if (millis() - start > STATS_PRINT_INTERVAL) {
    165     if (loopEventType == AceButton::kEventDoubleClicked) {
    166       Serial.print(F("double click            | "));
    167       printStats();
    168       Serial.println(F("    |"));
    169     }
    170     nextMode();
    171   }
    172 }
    173 
    174 void loopLongPress() {
    175   static unsigned long start = millis();
    176 
    177   unsigned long now = millis();
    178   unsigned long elapsed = now - start;
    179   if (100 <= elapsed) buttonConfig.setButtonState(LOW);
    180 
    181   if (millis() - start > STATS_PRINT_INTERVAL) {
    182     if (loopEventType == AceButton::kEventRepeatPressed) {
    183       Serial.print(F("long press/repeat press | "));
    184       printStats();
    185       Serial.println(F("    |"));
    186     }
    187     nextMode();
    188   }
    189 }
    190 
    191 void nextMode() {
    192   stats.reset();
    193   buttonConfig.setButtonState(HIGH);
    194   loopMode++;
    195 }
    196 
    197 void printStats() {
    198   printInt(stats.getMin());
    199   Serial.print('/');
    200   printInt(stats.getAvg());
    201   Serial.print('/');
    202   printInt(stats.getMax());
    203   Serial.print(F(" | "));
    204   printInt(stats.getCount());
    205 }
    206 
    207 // print integer within 3 characters, padded on left with spaces
    208 void printInt(uint16_t i) {
    209   if (i < 100) Serial.print(' ');
    210   if (i < 10) Serial.print(' ');
    211   Serial.print(i);
    212 }
    213 
    214 // An empty event handler.
    215 void handleEvent(AceButton* /* button */, uint8_t eventType,
    216     uint8_t /* buttonState */) {
    217   loopEventType = eventType;
    218 }