Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PactVizFramework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jrevans
PactVizFramework
Commits
22f7da7b
Commit
22f7da7b
authored
5 years ago
by
Joe Revans
Browse files
Options
Downloads
Patches
Plain Diff
cleaned up pot and enc libs; started on auto naming
parent
591abbba
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
NameDictionary.h
+55
-0
55 additions, 0 deletions
NameDictionary.h
Potentiometer.h
+40
-51
40 additions, 51 deletions
Potentiometer.h
RotaryEncoder.h
+55
-14
55 additions, 14 deletions
RotaryEncoder.h
with
150 additions
and
65 deletions
NameDictionary.h
0 → 100644
+
55
−
0
View file @
22f7da7b
#ifndef NAMEDICTIONARY_h
#define NAMEDICTIONARY_h
typedef
struct
{
String
key
;
String
value
;
}
keyValuePair
;
class
NameDictionary
{
keyValuePair
data
[
2
]
=
{
{
"1a6b16"
,
"joe"
},
{
"2b7c27"
,
"cat"
}
};
int
_arraySize
;
public
:
NameDictionary
()
{
_arraySize
=
sizeof
(
data
)
/
sizeof
(
keyValuePair
);
};
String
get
(
String
key
)
{
for
(
int
i
=
0
;
i
<
_arraySize
;
i
++
)
{
if
(
key
==
data
[
i
].
key
)
{
return
data
[
i
].
value
;
}
}
return
"null"
;
};
void
list
()
{
for
(
int
i
=
0
;
i
<
_arraySize
;
i
++
)
{
Serial
.
println
(
data
[
i
].
key
+
" : "
+
data
[
i
].
value
);
}
};
void
values
()
{
for
(
int
i
=
0
;
i
<
_arraySize
;
i
++
)
{
Serial
.
println
(
data
[
i
].
value
);
}
};
void
keys
()
{
for
(
int
i
=
0
;
i
<
_arraySize
;
i
++
)
{
Serial
.
println
(
data
[
i
].
key
);
}
};
int
length
()
{
return
_arraySize
;
}
};
#endif
This diff is collapsed.
Click to expand it.
Potentiometer.h
+
40
−
51
View file @
22f7da7b
...
...
@@ -4,36 +4,47 @@
class
Potentiometer
{
int
EMA_S
=
0
;
//initialization of EMA S
float
EMA_a
=
0.6
;
int
_pin
;
int
_id
;
int
_unstableValue
;
int
_stableValue
;
int
_value
;
int
_previousReading
;
bool
_readingChange
;
int
_previousValue
;
bool
_inputFlag
=
false
;
bool
_changeFlag
=
false
;
unsigned
long
_previousTimer
;
int
_
delay
=
200
;
int
_
interval
=
200
;
void
(
*
_cb
)(
Potentiometer
*
,
uint8_t
,
uint8_t
);
// Callback function
int
_read
()
{
int
sensorValue
=
analogRead
(
_pin
);
//read the sensor value using ADC
EMA_S
=
(
EMA_a
*
sensorValue
)
+
((
1
-
EMA_a
)
*
EMA_S
);
//run the EMA
int
mappedValue
=
map
(
EMA_S
,
5
,
1023
,
0
,
100
);
return
mappedValue
;
}
void
_setValue
(
int
x
)
{
_value
=
x
;
}
public
:
static
const
uint8_t
kEventStableUpdate
=
0
;
static
const
uint8_t
kEventUnstableUpdate
=
1
;
static
const
uint8_t
kEventStableUpdate
=
0
;
static
const
uint8_t
kEventUnstableUpdate
=
1
;
Potentiometer
(
int
pin
,
int
id
)
:
_pin
(
pin
),
_id
(
id
)
{
Potentiometer
(
int
pin
,
int
id
=
99
)
:
_pin
(
pin
),
_id
(
id
)
{
pinMode
(
pin
,
INPUT
);
EMA_S
=
analogRead
(
_pin
);
//set EMA S for t=1
_
stableV
alue
=
EMA_S
;
_
v
alue
=
EMA_S
;
_previousReading
=
EMA_S
;
_previousTimer
=
millis
();
_
readingChange
=
true
;
_
inputFlag
=
true
;
};
void
setEventHandler
(
void
(
*
function
)(
Potentiometer
*
,
uint8_t
,
uint8_t
))
{
...
...
@@ -41,7 +52,7 @@ class Potentiometer {
}
int
getValue
()
{
return
_
stableV
alue
;
return
_
v
alue
;
}
int
getId
()
{
...
...
@@ -49,51 +60,29 @@ class Potentiometer {
}
void
check
()
{
unsigned
long
_timer
=
millis
();
int
reading
=
_read
();
//int deltaReading = abs(reading - _previousReading);
int
deltaStable
=
abs
(
reading
-
getValue
());
int
deltaUnstable
=
abs
(
reading
-
_getUnstableValue
());
if
(
reading
!=
_previousReading
)
{
// When potentiometer reading has a new value:
_readingChange
=
true
;
// raise _readingChange flag
_previousTimer
=
_timer
;
// reset the timer
}
else
{
if
(
_timer
-
_previousTimer
>
_delay
&&
_readingChange
&&
deltaStable
>
1
)
{
// Once new reading stabilises:
_readingChange
=
false
;
// lower _readingChange flag
_stableValue
=
reading
;
// update publicly accessible sensor value
_cb
(
this
,
kEventStableUpdate
,
getValue
());
unsigned
long
timer
=
millis
();
unsigned
long
deltaTime
=
timer
-
_previousTimer
;
}
else
if
(
_readingChange
&&
deltaUnstable
>
1
)
{
_unstableValue
=
reading
;
_cb
(
this
,
kEventUnstableUpdate
,
_getUnstableValue
());
int
reading
=
_read
();
int
deltaValue
=
abs
(
reading
-
_value
);
}
if
(
reading
!=
_value
)
{
_inputFlag
=
true
;
}
_previousReading
=
reading
;
}
int
_getUnstableValue
()
{
return
_unstableValue
;
}
int
_read
()
{
float
EMA_a
=
0.6
;
//initialization of EMA alpha
int
sensorValue
=
0
;
//initialization of sensor variable, equivalent to EMA Y
int
mappedValue
=
0
;
sensorValue
=
analogRead
(
_pin
);
//read the sensor value using ADC
EMA_S
=
(
EMA_a
*
sensorValue
)
+
((
1
-
EMA_a
)
*
EMA_S
);
//run the EMA
mappedValue
=
map
(
EMA_S
,
5
,
1023
,
0
,
100
);
if
(
_inputFlag
==
true
&&
deltaValue
>
1
)
{
_inputFlag
=
false
;
_changeFlag
=
true
;
_previousTimer
=
timer
;
_setValue
(
reading
);
_cb
(
this
,
kEventUnstableUpdate
,
getValue
());
}
return
mappedValue
;
if
(
_changeFlag
==
true
&&
deltaTime
>
_interval
)
{
_changeFlag
=
false
;
_cb
(
this
,
kEventStableUpdate
,
getValue
());
}
}
};
#endif
This diff is collapsed.
Click to expand it.
RotaryEncoder.h
+
55
−
14
View file @
22f7da7b
...
...
@@ -7,12 +7,18 @@ class RotaryEncoder {
int
_pinA
;
int
_pinB
;
int
_id
;
int
_state
[
2
];
int
_position
;
volatile
int
_state
[
2
];
volatile
int
_position
;
unsigned
long
_timer
;
int
_debounceInterval
=
1
;
volatile
bool
_inputFlag
=
false
;
bool
_changeFlag
=
false
;
unsigned
long
_previousTimer
;
int
_interval
=
200
;
void
(
*
_cb
)(
RotaryEncoder
*
,
uint8_t
,
int
);
// Callback function
// {newPin2, newPin1, oldPin2, oldPin1}
int
movements
[
5
][
4
][
4
]
=
{
...
...
@@ -55,7 +61,7 @@ class RotaryEncoder {
_position
=
_position
+
delta
;
}
int
_findChange
(
int
state1
[
2
],
int
state2
[
2
])
{
int
_findChange
(
int
state1
[
2
],
volatile
int
state2
[
2
])
{
int
stateAppend
[]
=
{
state1
[
1
],
state1
[
0
],
state2
[
1
],
state2
[
0
]};
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
...
...
@@ -114,13 +120,18 @@ class RotaryEncoder {
public
:
// Public members:
static
const
uint8_t
kEventStableUpdate
=
0
;
static
const
uint8_t
kEventUnstableUpdate
=
1
;
//Public Functions:
RotaryEncoder
(
int
pinA
,
int
pinB
)
:
_pinA
(
pinA
),
_pinB
(
pinB
)
{
RotaryEncoder
(
int
pinA
,
int
pinB
,
int
id
=
99
)
:
_pinA
(
pinA
),
_pinB
(
pinB
)
,
_id
(
id
)
{
pinMode
(
_pinA
,
INPUT_PULLUP
);
pinMode
(
_pinB
,
INPUT_PULLUP
);
_
t
imer
=
millis
();
_
previousT
imer
=
millis
();
_setState
(
digitalRead
(
_pinA
),
digitalRead
(
_pinB
));
setPosition
(
0
);
}
...
...
@@ -130,22 +141,52 @@ class RotaryEncoder {
attachInterrupt
(
_pinB
,
function
,
CHANGE
);
}
void
setEventHandler
(
void
(
*
function
)(
RotaryEncoder
*
,
uint8_t
,
int
))
{
_cb
=
function
;
}
int
getPostition
()
{
return
_position
;
}
int
getId
()
{
return
_id
;
}
void
setPosition
(
int
value
)
{
_position
=
value
;
}
void
ti
ck
()
{
int
tempState
[]
=
{
digitalRead
(
_pinA
),
digitalRead
(
_pinB
)}
;
int
change
=
_findChange
(
tempState
,
_state
)
;
void
che
ck
()
{
unsigned
long
timer
=
millis
()
;
unsigned
long
deltaTime
=
timer
-
_previousTimer
;
if
(
change
!=
0
)
{
_incrementPosition
(
change
);
Serial
.
println
(
_position
);
if
(
_inputFlag
==
true
)
{
_inputFlag
=
false
;
_changeFlag
=
true
;
_previousTimer
=
timer
;
_cb
(
this
,
kEventUnstableUpdate
,
getPostition
());
}
if
(
_changeFlag
==
true
&&
deltaTime
>
_interval
)
{
_changeFlag
=
false
;
_cb
(
this
,
kEventStableUpdate
,
getPostition
());
}
}
void
ICACHE_RAM_ATTR
tick
()
{
int
tempState
[]
=
{
digitalRead
(
_pinA
),
digitalRead
(
_pinB
)};
int
delta
=
_findChange
(
tempState
,
_state
);
if
(
delta
!=
0
)
{
_incrementPosition
(
delta
);
_inputFlag
=
true
;
}
_setState
(
tempState
[
0
],
tempState
[
1
]);
}
};
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment