-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isp_quad_encoder_wbtn.spin2
122 lines (99 loc) · 3.92 KB
/
isp_quad_encoder_wbtn.spin2
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'' =================================================================================================
''
'' File....... isp_quad_encoder_wbtn.spin2
'' Purpose.... provide access to rotary values and button press (no smartpin use)
'' Authors.... Stephen M Moraco
'' -- Copyright (c) 2023 Iron Sheep Productions, LLC
'' -- see below for terms of use
'' E-mail..... [email protected]
'' Started.... May 2023
'' Updated.... 12 May 2023
''
'' =================================================================================================
''
CON { app constants }
ENC_STACK_SIZE_LONGS = 64
VAR
LONG position
LONG encBits
LONG encStack[ENC_STACK_SIZE_LONGS]
LONG pinEnc0
LONG pinEnc1
LONG pinEncBtn
pub null()
'' This is not a top-level object
PUB start(pnEnc0, pnEnc1, pnBtn)
'debug(`term term1 size 5 2 textsize 42 color blue)
'debug(`term term2 size 20 2 textsize 42 color yellow)
pinEnc0 := pnEnc0
pinEnc1 := pnEnc1
pinEncBtn := pnBtn
cogspin(NewCog, TaskContinuousEncoder(), @encStack)
repeat
'debug(`term1 '`(position/4)' 10) ' 10 == NewLine
'debug(`term2 'bits=`%(encBits)' 10) ' 10 == NewLine
waitms(200)
if ina.[pinEncBtn] == 0 ' Q: btn pressed? (no debounce)
position~
VAR
long sampleCt
PRI TaskContinuousEncoder()
'' read quad encoder signles on pins 1 & 2, update postion based on change
position := 0
encBits := -1
sampleCt := -1
encBits := getNExtEncValueNot(encBits)
repeat
case encBits
%00:
encBits := getNExtEncValueNot(encBits)
if encBits == %01
position--
elseif encBits == %10
position++
%01:
encBits := getNExtEncValueNot(encBits)
if encBits == %11
position--
elseif encBits == %00
position++
%10:
encBits := getNExtEncValueNot(encBits)
if encBits == %00
position--
elseif encBits == %11
position++
%11:
encBits := getNExtEncValueNot(encBits)
if encBits == %10
position--
elseif encBits == %01
position++
pri getNExtEncValueNot(wasValue) : newValue
sampleCt++
repeat
newValue := ina.[pinEnc1..pinEnc0]
until newValue <> wasValue
debug("enc: #", udec_(sampleCt), " bits=", ubin_(newValue), ", ", sdec(position))
CON { license }
{{
-------------------------------------------------------------------------------------------------
MIT License
Copyright (c) 2023 Iron Sheep Productions, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=================================================================================================
}}