forked from zynthian/zyncoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zynrv112.c
323 lines (296 loc) · 8.14 KB
/
zynrv112.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* ******************************************************************
* ZYNTHIAN PROJECT: RV112 + ADS1115
*
* Library for reading RV112 infinite potentiometer using ADS1115.
*
* Copyright (C) 2015-2021 Fernando Moyano <[email protected]>
*
* ******************************************************************
*
* 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; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the LICENSE.txt file.
*
* ******************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <boost/circular_buffer.hpp>
#include "zynads1115.h"
#include "zynpot.h"
#include "zynrv112.h"
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
rv112_t rv112s[MAX_NUM_RV112];
//-----------------------------------------------------------------------------
extern void (*zynpot_cb)(int8_t, int32_t);
//-----------------------------------------------------------------------------
// RV112's zynpot API
//-----------------------------------------------------------------------------
void init_rv112s() {
int i,j;
boost::circular_buffer<int32_t> *dvbuf;
for (i=0;i<MAX_NUM_RV112;i++) {
rv112s[i].enabled = 0;
rv112s[i].value = 0;
rv112s[i].zpot_i = -1;
rv112s[i].lastdv = 0;
rv112s[i].valraw = 0;
rv112s[i].dvavg = 0;
rv112s[i].dvbuf = dvbuf = new boost::circular_buffer<int32_t>(DVBUF_SIZE);
for (j=0;j<DVBUF_SIZE;j++) dvbuf->push_back(0);
}
}
void end_rv112s() {
int i;
for (i=0;i<MAX_NUM_RV112;i++) {
rv112s[i].enabled = 0;
rv112s[i].value = 0;
rv112s[i].zpot_i = -1;
rv112s[i].lastdv = 0;
rv112s[i].valraw = 0;
rv112s[i].dvavg = 0;
delete (boost::circular_buffer<int32_t> *)rv112s[i].dvbuf;
rv112s[i].dvbuf = NULL;
}
}
int get_num_rv112s() {
int i;
int n = 0;
for (i=0;i<MAX_NUM_RV112;i++) {
if (rv112s[i].enabled!=0) n++;
}
return n;
}
int setup_rv112(uint8_t i, ads1115_t *ads1115, uint8_t reversed_chans) {
if (i > MAX_NUM_RV112) {
fprintf(stderr, "ZynCore->setup_rv112(%d): Invalid index!\n", i);
return 0;
}
uint8_t pos = (i % 2) * 2;
rv112s[i].ads1115_node = ads1115;
if (reversed_chans == 0) {
rv112s[i].chA = pos + 1;
rv112s[i].chB = pos;
} else {
rv112s[i].chA = pos;
rv112s[i].chB = pos + 1;
}
rv112s[i].valA = ads1115_analog_read(ads1115, rv112s[i].chA);
rv112s[i].valB = ads1115_analog_read(ads1115, rv112s[i].chB);
rv112s[i].curseg = 0;
rv112s[i].lastdv = 0;
rv112s[i].value = 0;
rv112s[i].step = 1;
rv112s[i].valraw = 0;
rv112s[i].enabled = 1;
return 1;
}
int setup_behaviour_rv112(uint8_t i, int32_t step) {
if (i>=MAX_NUM_RV112 || rv112s[i].enabled==0) {
fprintf(stderr, "ZynCore->setup_step_rv112(%d, ...): Invalid index!\n", i);
return 0;
}
rv112s[i].step = step;
rv112s[i].valraw = 0;
rv112s[i].value = 0;
return 1;
}
int32_t get_value_rv112(uint8_t i) {
if (i>=MAX_NUM_RV112 || rv112s[i].enabled==0) {
fprintf(stderr, "ZynCore->get_value_rv112(%d): Invalid index!\n", i);
return 0;
}
int32_t res = rv112s[i].value;
if (res!=0) {
rv112s[i].valraw = 0;
rv112s[i].value = 0;
}
return res;
}
//-----------------------------------------------------------------------------
// RV112 specific functions
//-----------------------------------------------------------------------------
int16_t read_rv112(uint8_t i) {
int32_t vA = ads1115_analog_read(rv112s[i].ads1115_node, rv112s[i].chA);
int32_t vB = ads1115_analog_read(rv112s[i].ads1115_node, rv112s[i].chB);
int16_t d = 0;
switch (rv112s[i].curseg) {
case 0:
if (vB < RV112_ADS1115_RANGE_25) {
d = rv112s[i].valA - vA;
break;
}
else if (vA > RV112_ADS1115_RANGE_75) {
d = rv112s[i].valB - vB;
rv112s[i].curseg = 1;
break;
}
else if (vA < RV112_ADS1115_RANGE_25) {
d = vB - rv112s[i].valB;
rv112s[i].curseg = 3;
break;
}
else if (vB > RV112_ADS1115_RANGE_75) {
d = vA - rv112s[i].valA;
rv112s[i].curseg = 2;
break;
}
break;
case 1:
if (vA > RV112_ADS1115_RANGE_75) {
d = rv112s[i].valB - vB;
break;
}
else if (vB > RV112_ADS1115_RANGE_75) {
d = vA - rv112s[i].valA;
rv112s[i].curseg = 2;
break;
}
else if (vB < RV112_ADS1115_RANGE_25) {
d = rv112s[i].valA - vA;
rv112s[i].curseg = 0;
break;
}
else if (vA < RV112_ADS1115_RANGE_25) {
d = vB - rv112s[i].valB;
rv112s[i].curseg = 3;
break;
}
break;
case 2:
if (vB > RV112_ADS1115_RANGE_75) {
d = vA - rv112s[i].valA;
break;
}
else if (vA < RV112_ADS1115_RANGE_25) {
d = vB - rv112s[i].valB;
rv112s[i].curseg = 3;
break;
}
else if (vA > RV112_ADS1115_RANGE_75) {
d = rv112s[i].valB - vB;
rv112s[i].curseg = 1;
break;
}
else if (vB < RV112_ADS1115_RANGE_25) {
d = rv112s[i].valA - vA;
rv112s[i].curseg = 0;
break;
}
break;
case 3:
if (vA < RV112_ADS1115_RANGE_25) {
d = vB - rv112s[i].valB;
break;
}
else if (vB < RV112_ADS1115_RANGE_25) {
d = rv112s[i].valA - vA;
rv112s[i].curseg = 0;
break;
}
else if (vB > RV112_ADS1115_RANGE_75) {
d = vA - rv112s[i].valA;
rv112s[i].curseg = 2;
break;
}
else if (vA > RV112_ADS1115_RANGE_75) {
d = rv112s[i].valB - vB;
rv112s[i].curseg = 1;
break;
}
break;
}
rv112s[i].valA = vA;
rv112s[i].valB = vB;
//fprintf(stderr, "RV112[%d]: curseg = %d, vA = %d, vB = %d, d = %d\n", i, rv112s[i].curseg, vA, vB, d);
return d / RV112_ADS1115_NOISE_DIV;
}
void * poll_rv112(void *arg) {
int i = 0;
int j = 0;
int32_t vr, v;
boost::circular_buffer<int32_t> *dvbuf;
while (1) {
if (rv112s[i].enabled) {
rv112s[i].lastdv = read_rv112(i);
// Calculate moving average for adaptative speed variation
if (rv112s[i].step==0) {
dvbuf = (boost::circular_buffer<int32_t> *)rv112s[i].dvbuf;
dvbuf->push_back(abs(rv112s[i].lastdv));
rv112s[i].dvavg += (*dvbuf)[DVBUF_SIZE-1] - (*dvbuf)[0];
//fprintf(stderr, "DVAVG %d = %d\n", i, rv112s[i].dvavg);
}
if (rv112s[i].lastdv!=0) {
// Adaptative speed variation using a moving average
if (rv112s[i].step==0) {
if (rv112s[i].dvavg < 1000) rv112s[i].lastdv /= 8;
else if (rv112s[i].dvavg < 2000) rv112s[i].lastdv /= 4;
else if (rv112s[i].dvavg < 4000) rv112s[i].lastdv /= 2;
}
else {
rv112s[i].lastdv /= (8 * rv112s[i].step);
}
vr = rv112s[i].valraw + rv112s[i].lastdv;
// calculate value & call CB function
if (vr!=rv112s[i].valraw) {
rv112s[i].valraw = vr;
rv112s[i].value = vr / RV112_ADS1115_RAW_DIV;
if (rv112s[i].value!=0 && zynpot_cb) {
//fprintf(stderr, "RV112(%d): Vraw=%d, Value=%d\n", i, vr, rv112s[i].value);
zynpot_cb(rv112s[i].zpot_i, rv112s[i].value);
rv112s[i].valraw = 0;
rv112s[i].value = 0;
}
}
}
}
// Calc next rotary ...
i = (i + 1) % MAX_NUM_RV112;
// prioritize active rotaries, cycling among them, but force a full cyle from time to time ... ...
if (j<MAX_NUM_RV112) {
j++;
}
else if (j<3*MAX_NUM_RV112) {
for (int k=0; k<MAX_NUM_RV112; k++) {
if (rv112s[i].lastdv!=0) break;
i = (i + 1) % MAX_NUM_RV112;
}
j++;
}
//
else {
j = 0;
}
}
return NULL;
}
pthread_t init_poll_rv112() {
pthread_t tid;
int err=pthread_create(&tid, NULL, &poll_rv112, NULL);
if (err != 0) {
fprintf(stderr, "ZynCore: Can't create RV112 poll thread :[%s]", strerror(err));
return 0;
} else {
fprintf(stderr, "ZynCore: RV112 poll thread created successfully\n");
return tid;
}
}
//-----------------------------------------------------------------------------