-
Notifications
You must be signed in to change notification settings - Fork 23
/
zynpot.c
110 lines (97 loc) · 3.18 KB
/
zynpot.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
/*
* ******************************************************************
* ZYNTHIAN PROJECT: Zynpot, wrapper library for rotaries
*
* Library for interfacing rotaries of several types
*
* 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 <string.h>
#include "zynpot.h"
#include "zyncoder.h"
#include "zynrv112.h"
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
extern zyncoder_t zyncoders[MAX_NUM_ZYNCODERS];
extern rv112_t rv112s[MAX_NUM_RV112];
zynpot_t zynpots[MAX_NUM_ZYNPOTS];
void (*zynpot_cb)(int8_t, int32_t);
//-----------------------------------------------------------------------------
// Zynpot common API
//-----------------------------------------------------------------------------
void reset_zynpots() {
int i;
for (i=0;i<MAX_NUM_ZYNPOTS;i++) {
zynpots[i].type = ZYNPOT_NONE;
zynpots[i].data = NULL;
}
zynpot_cb = NULL;
}
int get_num_zynpots() {
int i;
int n = 0;
for (i=0;i<MAX_NUM_ZYNPOTS;i++) {
if (zynpots[i].type!=ZYNPOT_NONE) n++;
}
return n;
}
void setup_zynpot_cb(void (*cbfunc)(int8_t, int32_t)) {
zynpot_cb = cbfunc;
}
int setup_zynpot(uint8_t i, uint8_t type, uint8_t ii) {
if (i>MAX_NUM_ZYNPOTS) {
fprintf(stderr, "ZynCore->setup_zynpot(%d): Invalid index!\n", i);
return 0;
}
zynpots[i].type = type;
zynpots[i].i = ii;
switch (type) {
case ZYNPOT_ZYNCODER:
zyncoders[ii].zpot_i = (int8_t)i;
zynpots[i].data = (zynpot_data_t *) &zyncoders[ii];
zynpots[i].setup_behaviour = setup_behaviour_zyncoder;
zynpots[i].get_value = get_value_zyncoder;
break;
case ZYNPOT_RV112:
rv112s[ii].zpot_i = (int8_t)i;
zynpots[i].data = (zynpot_data_t *) &rv112s[ii];
zynpots[i].setup_behaviour = setup_behaviour_rv112;
zynpots[i].get_value = get_value_rv112;
break;
}
return 1;
}
int setup_behaviour_zynpot(uint8_t i, int32_t step) {
if (i>MAX_NUM_ZYNPOTS || zynpots[i].type==ZYNPOT_NONE) {
fprintf(stderr, "ZynCore->setup_behaviour_zynpot(%d): Invalid index!\n", i);
return 0;
}
return zynpots[i].setup_behaviour(zynpots[i].i, step);
}
int32_t get_value_zynpot(uint8_t i) {
if (i>=MAX_NUM_ZYNPOTS || zynpots[i].type==ZYNPOT_NONE) {
fprintf(stderr, "ZynCore->get_value_zynpot(%d): Invalid index!\n", i);
return 0;
}
return zynpots[i].get_value(zynpots[i].i);
}
//-----------------------------------------------------------------------------