-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_quadEncTimiPca_limitChk.spin2
235 lines (189 loc) · 8.41 KB
/
demo_quadEncTimiPca_limitChk.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'' =================================================================================================
''
'' File....... demo_quadEncTimiPca_limitChk.spin2
'' Purpose.... Using Quad encoder for control and Timi-130 display, let's determine limits of PCA9685 attached servos
'' 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.... 16 May 2023
''
'' =================================================================================================
CON { timing }
CLK_FREQ = 270_000_000 ' system freq as a constant
_clkfreq = CLK_FREQ ' set system clock
CON { fixed io pins }
RX1 = 63 { I } ' programming / debug
TX1 = 62 { O }
SF_CS = 61 { O } ' serial flash
SF_SCK = 60 { O }
SF_SDO = 59 { O }
SF_SDI = 58 { I }
CON { application io pins }
' NOTE: pins 24 & 25 also use 26 & 27 to determine rate of rotation
QUAD_ENC_CLK = 26 { I }
QUAD_ENC_DAT = 27 { I }
QUAD_ENC_BTN = 28 { I }
SRVO_OFST_RANGE = 750 ' NOTE customize this to adjut servo range span
SRVO_OFST_CNTR = 1500
SRVO_OFST_MIN = SRVO_OFST_CNTR - SRVO_OFST_RANGE
SRVO_OFST_MAX = SRVO_OFST_CNTR + SRVO_OFST_RANGE
SRVO_PULSE_WIDTH_MS = 50 ' pulse width in milliSeconds
MAX_SERVOS = 6
' multiply the runtime-determined spin rate of the encoder knob
ENC_RATE_SCALE = 4 ' 2 = 5 turns end-to-end, 4 = 3 turns end-to-end
' TIMI-130 display pins
TIMI_RX = 16
TIMI_TX = 17
TIMI_RST = 18
TIMI_BAUD_RATE = 9600
TIMI_PAGEID_DIAL = 0
TIMI_PAGEID_METER = 1
TIMI_PAGEID_LEDMTX = 2
TIMI_PAGEID_NET_MTR = 3
' the PCA9685 with attached servos
PCA9685_OE = 8
PCA9685_SCL = 9
PCA9685_SDA = 10
OBJ
quadEnc : "isp_quadEncWBtn_sp" ' 3-pin I/F to push button rotary encoder
timi : "isp_timi_mates" ' serial I/F to display
memstr : "isp_mem_strings" ' in-ram formatter for strings
pcaServos : "isp_i2c_pca9685" ' 16-channel servo controller
DAT
servoIdx LONG 0
servoInvertIdx LONG 0
servoOffset LONG 1500
stateIdx LONG 0
msgLOW BYTE "LO",0
msgHIGH BYTE "HI",0
Title0 BYTE "Ser Limit",0
Title1 BYTE "SRVO-1 LO",0
rateMsg BYTE " 1500",0
deltaPosition LONG 0
priorPosition LONG 0
desiredOffset LONG 0
PUB main() | pageId, bDidSucceed, currPosn, currRate, tmpOffset, bIsPressed, bIsDouble, bDevicePresent
' start our input device
quadEnc.start(QUAD_ENC_CLK, QUAD_ENC_DAT, QUAD_ENC_BTN)
quadEnc.setScale(ENC_RATE_SCALE)
' start our display device
timi.start(TIMI_TX, TIMI_RX, TIMI_RST, TIMI_BAUD_RATE)
' start our output set of servos
pcaServos.init(PCA9685_SCL, PCA9685_SDA, PCA9685_OE)
pcaServos.enableOutput(FALSE)
' interfae prototype
' button pressess selects next servo or high/low range
' knob rotates incrementing servo offset value
' current effort is shown on the TIMI display
' connect to servos
bDevicePresent := pcaServos.start(PCA9685_SCL, PCA9685_SDA, PCA9685_OE)
if bDevicePresent
debug("* connected to PCA9685")
pcaServos.configureController()
pcaServos.enableOutput(TRUE)
' center our servos
pcaServos.setAllServos(SRVO_OFST_CNTR)
debug("* all servos PWMing.... 1500 uSec...")
'debug("* all servos PWMing.... 1500 uSec, holding")
'repeat
debug(" ")
'debug("test... ")
{
repeat pageId from 0 to 3
bDidSucceed := timi.setPage(pageId, timi.MATES_RESPONSE_LTIMEOUT)
if not bDidSucceed
quit
waitms(2000) ' show for 2 sec
'}
' select the 4x LED matrix page
servoInvertIdx := 15 - servoIdx
pcaServos.gotoMicroSec(servoIdx, servoOffset)
bDidSucceed := timi.setPage(TIMI_PAGEID_LEDMTX, timi.MATES_RESPONSE_LTIMEOUT)
if bDidSucceed
' set titles
timi.updateDotMatrixWidget(0, @Title0)
updateDisplay()
repeat
bIsPressed, bIsDouble := quadEnc.isButtonPressed()
if bIsPressed
if bIsPressed and not bIsDouble
debug(" ")
debug("* FINDING ---- ", zstr_(@Title1), " = ", sdec_(servoOffset), "----")
debug(" ")
'debug("* Button Pressed, isDbl=", sdec_(bIsDouble))
stateIdx += (bIsDouble) ? -1 : +1
if stateIdx > 1
stateIdx := 0
'pcaServos.gotoMicroSec(servoIdx, SRVO_OFST_CNTR) ' reset to center as we leave
servoIdx++
if servoIdx >= MAX_SERVOS
servoIdx := 0
elseif stateIdx < 0
stateIdx := 1
'pcaServos.gotoMicroSec(servoIdx, SRVO_OFST_CNTR) ' reset to center as we leave
servoIdx--
if servoIdx < 0
servoIdx := MAX_SERVOS - 1
servoOffset := SRVO_OFST_CNTR
servoInvertIdx := 15 - servoIdx
updateStateDisplay()
deltaPosition := quadEnc.positionScaled()
if deltaPosition <> 0
tmpOffset := servoOffset + deltaPosition
servoOffset := SRVO_OFST_MIN #> tmpOffset <# SRVO_OFST_MAX
'debug("* Offset=", sdec_(servoOffset), ", delta=", sdec_(deltaPosition))
'debug(" ")
updateOffsetDisplay()
pcaServos.gotoMicroSec(servoIdx, servoOffset)
debug("done... holding...")
repeat
PRI updateDisplay()
updateStateDisplay()
updateOffsetDisplay()
PRI updateStateDisplay() | pStateTitle
pStateTitle := genStateMsg(servoIdx, stateIdx)
timi.updateDotMatrixWidget(1, pStateTitle)
updateInstruDisplay()
PRI updateOffsetDisplay() | pRateMsg
pRateMsg := rightAlignValue(servoOffset, 9)
timi.updateDotMatrixWidget(2, pRateMsg)
PRI updateInstruDisplay() | pInstruMsg, pText
pText := (stateIdx == 0) ? @"to 750 -" : @"to 2250 -"
pInstruMsg := rightAlignString(pText, 9)
timi.updateDotMatrixWidget(3, pInstruMsg)
PRI genStateMsg(nServo, nState) : pMessage | lenBytes, pStateMsg
pStateMsg := (nState == 0) ? @msgLOW : @msgHIGH
lenBytes := memstr.sFormatStr2(@Title1, @"SRVO-%d %s", nServo, pStateMsg)
pMessage := @Title1
PRI rightAlignValue(value, width) : pMessage | lenBytes, BYTE fmtStr[20]
lenBytes := memstr.sFormatStr1(@fmtStr, @"\%%dd", width)
lenBytes := memstr.sFormatStr1(@rateMsg, @fmtStr, value)
pMessage := @rateMsg
PRI rightAlignString(pStr, width) : pMessage | lenBytes, BYTE fmtStr[20]
lenBytes := memstr.sFormatStr1(@fmtStr, @"\%%ds", width)
lenBytes := memstr.sFormatStr1(@rateMsg, @fmtStr, pStr)
pMessage := @rateMsg
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.
=================================================================================================
}}