-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencoderController.h
More file actions
96 lines (79 loc) · 2.08 KB
/
encoderController.h
File metadata and controls
96 lines (79 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright (c) 2020 David Radcliffe
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License
*/
// Analogue (PWM) Train Controller.
//
// Class: encoderControllerClass
//
// Methods:
// encoderControllerClass(int setPinA, int setPinB, int setPinS) Class Constructor
//#define ENCODER_USE_INTERRUPTS
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include "Encoder.h"
class encoderControllerClass
{
private:
Encoder enc;
bool ignorePush = false;
// -------------------------------------------------
public:
uint8_t newPos = 0;
uint8_t lastPos = 0;
unsigned long push = 0;
// -------------------------------------------------
// Constructor - initializes the member variables and state
encoderControllerClass (uint8_t setPinA,
uint8_t setPinB,
uint8_t setPinS)
{
enc = Encoder(setPinA, setPinB, setPinS);
}
bool read()
{
uint8_t val = enc.read();
if (val == 255) // button pressed
{
if (ignorePush) return false;
if (push == 0) push = millis();
enc.write(0);
newPos = 0;
lastPos = 0;
return true;
}
else // button released
{
ignorePush = false;
push = 0;
}
if (val == 1) // EStop value - not needed
{
if (lastPos == 0)
{
enc.write(2);
val = 2;
}
else // == 2
{
enc.write(0);
val = 0;
}
}
newPos = val;
return false;
}
void write(uint8_t value)
{
enc.write(value);
newPos = value;
lastPos = value;
push = 0;
ignorePush = true;
}
// -------------------------------------------------
private:
// -------------------------------------------------
// -------------------------------------------------
};