Skip to content

Latest commit

 

History

History
237 lines (179 loc) · 7.62 KB

File metadata and controls

237 lines (179 loc) · 7.62 KB

This is an automatic translation and may be incorrect in some places. See the source README and examples for authoritative information.

latest PIO Foo Foo Foo

Foo

uEncoder

Another class of encoder and encoder with a button (based onuButton) for Arduino.

On the API, it's 99% equivalent.EncButtonBut it's a little lighter, it's 1.5 times quicker to poll and it's written in a more readable finite-automatic style, rather than on flags.

Compatibility

Compatible with all Arduino platforms (Arduino features are used)

Dependencies

Contents

Use of use

Define settings

Announce before connecting the library

#define UE_FAST_TIME 32  // time between ticks for a quick turn

Classes

uEncoderVirt

Virtual encoder class without a button.

// ==========================================

// encoder
enum class uEType {
    Step4Low,   // Active low signal (VCC lift) Full period (4 phases) per click
    Step4High,  // Active high signal (lift to GND). Full period (4 phases) per click
    Step2,      // half period (2 phases) per click
    Step1,      // quarter period (1 phase) per click
};

// uEType:::Step4Low, uEType::Step4High, uEType:::Step2, uEType::Step1
void setEncType(uEType type);

// invert
void setEncReverse(bool rev);

// initialization
void initEnc(bool e0, bool e1);

// ==================================================

enum class State {
    Idle,           // idle
    Left,           // left-turn
    LeftFast,       // fast-left
    LeftHold,       // left-turn
    LeftHoldFast,   // push-left
    Right,          // right-turn
    RightFast,      // right-turn
    RightHold,      // right-turn
    RightHoldFast,  // right-hander
};
State getState();   // state
void reset();       // Reset the condition (forced to complete processing)

// =============================================================

// It was a turnaround.
uint8_t turn();

// direction of encoder (1 or -1) [state]
int8_t dir();

// direction of the encoder (0 or 1) [state]
bool dirBool();

// The Encoder's Turn [Event]
bool turnH();

// fast turn of the encoder [state]
bool fast();

// turn right [event]
bool right();

// turn left [event]
bool left();

// turn to the right [event]
bool rightH();

// turn to the left [event]
bool leftH();

// Encoder button pressed [state]
bool encHolding();

// =========================================

// poll
bool tick(bool e0, bool e1, bool pressed = false);

// interrogate
State poll(bool e0, bool e1, bool pressed = false);

uEncoder

Class classuEncodersucceeduEncoderVirtsending data from him topollinsidetick.

// specify pins and their mode of operation
uEncoder(uint8_t p0, uint8_t p1, uint8_t mode = INPUT);

// poll
bool tick(bool pressed = false);

// interrogate
void tickISR(bool pressed = false);

uEncButton

Class classuEncButtonsucceeduEncoderanduButtoninterviewing them. All button events are available. When you rotate the encoder, when you press the button, all the events of the button are reset, except for letting go.

uEncButton(uint8_t encA, uint8_t encB, uint8_t btn, uint8_t modeEnc = INPUT, uint8_t modeBtn = INPUT_PULLUP);

// interview. Returns true at the event
bool tick();

// interrogate
void tickISR();

Examples

// timeout settings, ms.
// #define UE FAST TIME 32 // Time between ticks for a quick turn

#include <uEncButton.h>

uEncButton eb(2, 3, 4);

// interrogation
// void isr() {
//     eb.tickISR();
// }

void setup() {
    Serial.begin(115200);

    // interrogation
    // attachInterrupt(0, isr, CHANGE);
    // attachInterrupt(1, isr, CHANGE);
}

void loop() {
    eb.tick();

    // cornering
    if (eb.turn()) {
        Serial.print("turn: dir ");
        Serial.print(eb.dir());
        Serial.print(", fast ");
        Serial.print(eb.fast());
        Serial.print(", hold ");
        Serial.print(eb.pressing());
        Serial.print(", clicks ");
        Serial.print(eb.getClicks());
        Serial.println();
    }

    // cornering
    if (eb.left()) Serial.println("left");
    if (eb.right()) Serial.println("right");
    if (eb.leftH()) Serial.println("leftH");
    if (eb.rightH()) Serial.println("rightH");

    // button
    if (eb.press()) Serial.println("press");
    if (eb.click()) Serial.println("click");
    if (eb.release()) Serial.println("release");

    // time-out
    if (eb.timeout(1000)) Serial.println("timeout");
}

Versions

  • v1.0

Installation

  • The library can be found under the name uEncoder and installed through the library manager in:
    • Arduino IDE
    • Arduino IDE v2
    • PlatformIO
  • Download the library.zip archive for manual installation:
    • Unpack and put in C:\Program Files (x86)\Arduino\libraries (Windows x64)
    • Unpack and put in C:\Program Files\Arduino\libraries (Windows x32)
    • Unpack and put in *Documents/Arduino/libraries/ *
    • (Arduino IDE) Automatic installation from .zip: Sketch/Connect library/Add .ZIP library... and specify downloaded archive
  • Read more detailed instructions for installing librarieshere

Update

  • I recommend always updating the library: new versions fix errors and bugs, as well as optimize and add new features.
  • Through the library manager IDE: find the library as when installing and click "Update"
  • Manually: Delete the folder with the old version and then put the new one in its place. “Replacement” can not be done: sometimes new versions delete files that will remain when replaced and can lead to errors!

Bugs and feedback

If you find bugs, create Issue, or better write to the mail immediately.alex@alexgyver.ru
The library is open for revision and your *Pull Requests!

When reporting bugs or incorrect work of the library, it is necessary to specify:

  • Library version
  • What is used by the IC
  • SDK version (for ESP)
  • Arduino IDE version
  • Are embedded examples that use features and designs that cause bugs in your code working correctly?
  • What code was downloaded, what work was expected from it and how it works in reality
  • Ideally, attach the minimum code in which the bug is observed. Not a canvas of a thousand lines, but a minimum code.