-
Notifications
You must be signed in to change notification settings - Fork 24
/
zyncontrol_vx.c
236 lines (205 loc) · 6.37 KB
/
zyncontrol_vx.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
/*
* ******************************************************************
* ZYNTHIAN PROJECT: Zyncontrol Library for Zynthian Kits V1-V4
*
* Initialize & configure control hardware for Zynthian Kits V1-V4
*
* 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gpiod_callback.h"
#include "zynmcp23008.h"
#include "zynpot.h"
#include "zyncoder.h"
#ifdef ZYNAPTIK_CONFIG
#include "zynaptik.h"
#endif
#ifdef ZYNTOF_CONFIG
#include "zyntof.h"
#endif
//-----------------------------------------------------------------------------
// GPIO Expander
//-----------------------------------------------------------------------------
#if defined(MCP23017_ENCODERS)
// pins 100-115 are located on the MCP23017
#define MCP23017_BASE_PIN 100
// default I2C Address for MCP23017
#if !defined(MCP23017_I2C_ADDRESS)
#define MCP23017_I2C_ADDRESS 0x20
#endif
// default interrupt pins for the MCP23017
#if !defined(MCP23017_INTA_PIN)
#define MCP23017_INTA_PIN 27
#endif
#if !defined(MCP23017_INTB_PIN)
#define MCP23017_INTB_PIN 25
#endif
#elif defined(MCP23008_ENCODERS)
// pins 100-107 are located on the MCP23008
#define MCP23008_BASE_PIN 100
#define MCP23008_I2C_ADDRESS 0x20
#endif
#if defined(MCP23017_ENCODERS)
// two ISR routines for the two banks
void zynmcp23017_ISR_bankA() {
zynmcp23017_ISR(0, 0);
}
void zynmcp23017_ISR_bankB() {
zynmcp23017_ISR(0, 1);
}
void (*zynmcp23017_ISRs[2]) = {
zynmcp23017_ISR_bankA,
zynmcp23017_ISR_bankB
};
void init_zynmcp23017s() {
reset_zynmcp23017s();
setup_zynmcp23017(0, MCP23017_BASE_PIN, MCP23017_I2C_ADDRESS, wpi2gpio[MCP23017_INTA_PIN], wpi2gpio[MCP23017_INTB_PIN], zynmcp23017_ISRs);
}
#endif
#if defined(MCP23008_ENCODERS)
void init_zynmcp23008s() {
reset_zynmcp23008s();
setup_zynmcp23008(0, MCP23008_BASE_PIN, MCP23008_I2C_ADDRESS);
}
#endif
//-----------------------------------------------------------------------------
// Get wiring config from environment
//-----------------------------------------------------------------------------
#define NUM_ZYNSWITCHES 36
#define NUM_ZYNPOTS 4
int16_t zynswitch_pins[NUM_ZYNSWITCHES];
int16_t zyncoder_pins_a[NUM_ZYNPOTS];
int16_t zyncoder_pins_b[NUM_ZYNPOTS];
extern uint16_t num_zynswitches;
void reset_wiring_config() {
int16_t i;
for (i=0;i<NUM_ZYNSWITCHES;i++) {
zynswitch_pins[i] = -1;
}
for (i=0;i<NUM_ZYNPOTS;i++) {
zyncoder_pins_a[i] = -1;
zyncoder_pins_b[i] = -1;
}
}
uint16_t parse_envar2intarr(const char *envar_name, int16_t *result, int16_t limit) {
uint16_t i = 0;
const char *envar_ptr = getenv(envar_name);
if (envar_ptr) {
char envar_cpy[128];
char *save_ptr;
int16_t res;
strcpy(envar_cpy, envar_ptr);
char *token = strtok_r(envar_cpy, ",", &save_ptr);
while (token!=NULL && i<limit) {
res = atoi(token);
// Convert low pins (RPi pins) from wiringPi to GPIO numbers
if (res >= 0 && res < 100) res = wpi2gpio[res];
result[i++] = res;
token = strtok_r(NULL, ",", &save_ptr);
}
}
return i;
}
void get_wiring_config() {
reset_wiring_config();
num_zynswitches = parse_envar2intarr("ZYNTHIAN_WIRING_SWITCHES", zynswitch_pins, NUM_ZYNSWITCHES);
fprintf(stderr, "ZynCore: Configured %d x Logical Zynswitches...\n", num_zynswitches);
parse_envar2intarr("ZYNTHIAN_WIRING_ENCODER_A", zyncoder_pins_a, NUM_ZYNPOTS);
parse_envar2intarr("ZYNTHIAN_WIRING_ENCODER_B", zyncoder_pins_b, NUM_ZYNPOTS);
}
//-----------------------------------------------------------------------------
// 8 x ZynSwitches
//-----------------------------------------------------------------------------
void init_zynswitches() {
reset_zynswitches();
int16_t i, count;
for (i=0, count=0; i<NUM_ZYNSWITCHES; i++) {
if (zynswitch_pins[i] >= 0) {
//fprintf(stderr, "ZynCore: Setting-up zynswitch in pin %d...\n", zynswitch_pins[i]);
setup_zynswitch(i, zynswitch_pins[i], 1);
count++;
}
}
fprintf(stderr, "ZynCore: Setting-up %d x Physical Zynswitches...\n", count);
}
//-----------------------------------------------------------------------------
// 4 x Zynpots (zyncoder => Incremental encoder)
//-----------------------------------------------------------------------------
void init_zynpots() {
reset_zynpots();
reset_zyncoders();
int16_t i, count;
for (i=0, count=0; i<NUM_ZYNPOTS; i++) {
if (zyncoder_pins_a[i] >= 0 && zyncoder_pins_b[i] >= 0) {
//fprintf(stderr, "ZynCore: Setting-up zyncoder in pins (%d, %d)...\n", zyncoder_pins_a[i], zyncoder_pins_b[i]);
setup_zyncoder(i, zyncoder_pins_a[i], zyncoder_pins_b[i]);
setup_zynpot(i, ZYNPOT_ZYNCODER, i);
count++;
}
}
fprintf(stderr, "ZynCore: Setting-up %d x Zynpots (zyncoders)...\n", count);
}
//-----------------------------------------------------------------------------
// Zyncontrol Initialization
//-----------------------------------------------------------------------------
int init_zyncontrol() {
gpiod_init_callbacks();
get_wiring_config();
#if defined(MCP23017_ENCODERS)
init_zynmcp23017s();
#endif
#if defined(MCP23008_ENCODERS)
init_zynmcp23008s();
#endif
init_zynswitches();
init_zynpots();
#ifdef ZYNAPTIK_CONFIG
init_zynaptik();
#endif
#ifdef ZYNTOF_CONFIG
init_zyntof();
#endif
gpiod_start_callbacks();
#if defined(MCP23008_ENCODERS)
init_poll_zynswitches();
#endif
return 1;
}
int end_zyncontrol() {
#if defined(MCP23008_ENCODERS)
end_poll_zynswitches();
#endif
gpiod_stop_callbacks();
#ifdef ZYNTOF_CONFIG
end_zyntof();
#endif
#ifdef ZYNAPTIK_CONFIG
end_zynaptik();
#endif
reset_zynpots();
reset_zyncoders();
reset_zynswitches();
#if defined(MCP23017_ENCODERS)
reset_zynmcp23017s();
#endif
return 1;
}
//-----------------------------------------------------------------------------