chiark / gitweb /
wiringpi: much implementation
[chiark-tcl.git] / wiringpi / wiringpi.c
1 /*
2  * wiringpi setup [MODE]
3  *     MODE is one of -g -1 -p, as for gpio(1)
4  *     optional, if not called is equivalent to saying just "setup"
5  *     may not be called after any other wiringpi use in whole program
6  *     there is no way to un-setup
7  *
8  * Most of the rest are very similar to gpio(1):
9  *   boardRev => integer (see wiringPi.h)
10  *   boardId => [list MODEL REV MEM MAKER OVERVOLTED]
11  *   mode PIN MODE
12  *       MODEs are
13  *             in out pwm pwmTone clock up down tri off
14  *             alt0 alt1 alt2 alt3 alt4 alt5
15  *   read PIN => 0|1
16  *   write PIN 0|1
17  *   aread PIN => VAL
18  *   write PIN VAL
19  *   pwm PIN VAL
20  *   clock PIN FREQ
21  *   pwm-bal|pwm-ms
22  *   pwmr RANGE
23  *   pwmc DIVIDER
24  *   pwmTone PIN FREQ
25  */
26 /* ---8<--- end of documentation comment --8<-- */
27
28 /*
29  * wiringpi.c - wiringPi binding for Tcl
30  * Copyright 2016 Ian Jackson
31  *
32  * This program is free software; you can redistribute it and/or
33  * modify it under the terms of the GNU General Public License as
34  * published by the Free Software Foundation; either version 2 of the
35  * License, or (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful, but
38  * WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
40  * General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this library; if not, see <http://www.gnu.org/licenses/>.
44  */
45
46 #define _GNU_SOURCE
47
48 #include <stdio.h>
49
50 #include "chiark_tcl_wiringpi.h"
51
52 #include "wiringPi.h"
53 #include <piFace.h>
54
55 /*---------- important types and forward declarations ----------*/
56
57 #define CHECK_SETUP                             \
58   int rc;                                       \
59   rc = ensure_setup(ip);                        \
60   if (rc) return rc; else
61
62 #define CHECK_RANGE(val, min, max, what, VAL) do{               \
63     if (val < (min))                                            \
64       return cht_staticerr(ip, what " too small"                \
65                            " (must be at least" #min ")",       \
66                            "WIRINGPI RANGE " VAL " MIN " #min); \
67     if (val > (max))                                            \
68       return cht_staticerr(ip, what " too large"                \
69                            " (must be at most" #max ")",        \
70                            "WIRINGPI RANGE " VAL " MAX " #max); \
71   }while(0)
72
73 static int setup_done;
74
75 /*---------- operations ----------*/
76
77 static int setup_failure(Tcl_Interp *ip, int r) {
78 #define N 4
79   Tcl_Obj *codelist[N];
80   codelist[0]= cht_ret_string(ip,"WIRINGPI");
81   codelist[1]= cht_ret_string(ip,"ERROR");
82   codelist[2]= cht_ret_string(ip,"SETUP");
83   codelist[3]= cht_ret_int(ip,r);
84   Tcl_SetObjResult(ip, Tcl_NewListObj(N, codelist));
85 #undef N
86   Tcl_SetResult(ip, (char*)"wiringpi initialisation error", TCL_STATIC);
87   return TCL_ERROR;
88 }
89
90 static int ensure_setup(Tcl_Interp *ip) {
91   if (!setup_done) {
92     int r = wiringPiSetup();
93     if (r) return setup_failure(ip, r);
94     setup_done = 1;
95   }
96   return TCL_OK;
97 }
98
99 int cht_do_wiringpitcl_setup(ClientData cd, Tcl_Interp *ip,
100                              int objc, Tcl_Obj *const *objv) {
101   int r;
102   int rc;
103
104   if (setup_done)
105     return cht_staticerr(ip,"wiringpi setup called again",
106                          "WIRINGPI FORBIDDEN-REINIT SETUP");
107
108   if (*objv) {
109     const char *modeopt;
110     rc = cht_pat_string(ip,*objv++,&modeopt);
111     if (rc) return rc;
112     if (modeopt[0] != '-' || !modeopt[1] || modeopt[2])
113       return cht_staticerr(ip,"wiringpi setup option wrong syntax", 0);
114     if (*objv)
115       return cht_staticerr(ip,"wiringpi setup too many arguments", 0);
116     switch (modeopt[1]) {
117     case 'g':  r = wiringPiSetupGpio();                             break;
118     case '1':  r = wiringPiSetupPhys();                             break;
119     case 'p':  r = piFaceSetup(2000 /* ?! copied from gpio.c* */);  break;
120     default:
121       return cht_staticerr(ip,"wiringpi setup option unknown value", 0);
122     }
123   } else {
124     r = wiringPiSetup();
125   }
126   if (r) return setup_failure(ip, r);
127   return TCL_OK;
128 }
129
130 int cht_do_wiringpitcl_pwmc(ClientData cd, Tcl_Interp *ip, int divider) {
131   CHECK_SETUP;
132   CHECK_RANGE(divider,1,4095,"divider for pwmc","DIVIDER");
133   pwmSetClock(divider);
134   return TCL_OK;
135 }
136
137 int cht_do_wiringpitcl_pwmr(ClientData cd, Tcl_Interp *ip, int range) {
138   CHECK_SETUP;
139   CHECK_RANGE(range,1,INT_MAX,"pwm range","RANGE");
140   pwmSetRange(range);
141   return TCL_OK;
142 }
143
144 int cht_do_wiringpitcl_boardId(ClientData cd, Tcl_Interp *ip,
145                                Tcl_Obj **result) {
146 #define N 5
147   CHECK_SETUP;
148   int ints[N];
149   Tcl_Obj *objl[N];
150   piBoardId(&ints[0],
151             &ints[1],
152             &ints[2],
153             &ints[3],
154             &ints[4]);
155   int i;
156   for (i=0; i<N; i++) objl[i]= Tcl_NewIntObj(ints[i]);
157   *result= Tcl_NewListObj(N,objl);
158   return TCL_OK;
159 #undef N
160 }
161
162 int cht_do_wiringpitcl_boardRev(ClientData cd, Tcl_Interp *ip, int *result) {
163   CHECK_SETUP;
164   *result = piBoardRev();
165   return TCL_OK;
166 }
167
168 /*---------- families operations ----------*/
169
170 int cht_do_wiringpitcl_aread(ClientData cd, Tcl_Interp *ip, int pin, int *result) {
171   CHECK_SETUP;
172   *result = analogRead(pin);
173   return TCL_OK;
174 }
175
176 #define SIMPLE_WRITER_OP(op, min, max, wpicall)                 \
177   int cht_do_wiringpitcl_##op(ClientData cd, Tcl_Interp *ip,    \
178                               int pin, int val) {               \
179     CHECK_SETUP                                                 \
180     CHECK_RANGE(val,min,max, "value for " #op, "VAL");          \
181     wpicall(pin, val);                                          \
182     return TCL_OK;                                              \
183   }
184
185 SIMPLE_WRITER_OP(pwm,     INT_MIN,INT_MAX, pwmWrite)
186 SIMPLE_WRITER_OP(awrite,  INT_MIN,INT_MAX, analogWrite)
187 SIMPLE_WRITER_OP(write,   0,      1,       digitalWrite)
188 SIMPLE_WRITER_OP(pwmTone, INT_MIN,INT_MAX, pwmToneWrite)
189 SIMPLE_WRITER_OP(clock,   INT_MIN,INT_MAX, gpioClockSet)
190
191 #define MODES(M)                                                \
192   M(in,       { pinMode        (pin, INPUT); })                 \
193   M(input,    { pinMode        (pin, INPUT); })                 \
194   M(out,      { pinMode        (pin, OUTPUT); })                \
195   M(output,   { pinMode        (pin, OUTPUT); })                \
196   M(pwm,      { pinMode        (pin, PWM_OUTPUT); })            \
197   M(pwmTone,  { pinMode        (pin, PWM_TONE_OUTPUT); })       \
198   M(clock,    { pinMode        (pin, GPIO_CLOCK); })            \
199   M(up,       { pullUpDnControl(pin, PUD_UP); })                \
200   M(down,     { pullUpDnControl(pin, PUD_DOWN); })              \
201   M(tri,      { pullUpDnControl(pin, PUD_OFF); })               \
202   M(off,      { pullUpDnControl(pin, PUD_OFF); })               \
203   M(alt0,     { pinModeAlt(pin, 0b100); })                      \
204   M(alt1,     { pinModeAlt(pin, 0b101); })                      \
205   M(alt2,     { pinModeAlt(pin, 0b110); })                      \
206   M(alt3,     { pinModeAlt(pin, 0b111); })                      \
207   M(alt4,     { pinModeAlt(pin, 0b011); })                      \
208   M(alt5,     { pinModeAlt(pin, 0b010); })
209
210 #define MODE_FUNC(name, body) static void mode_##name(int pin) { body }
211   MODES(MODE_FUNC)
212
213 const WiringPiTclModeInfo cht_wiringpitclmodeinfo_entries[] = {
214 #define MODEINFO_ENTRY(name, body) { #name, mode_##name },
215   MODES(MODEINFO_ENTRY)
216   { 0 },
217 };
218
219 int cht_do_wiringpitcl_mode(ClientData cd, Tcl_Interp *ip, int pin,
220                             const WiringPiTclModeInfo *mode) {
221   CHECK_SETUP;
222   mode->func(pin);
223   return TCL_OK;
224 }
225
226
227 /*---------- main hooks for tcl ----------*/
228
229 CHT_INIT(wiringpi, {}, CHTI_COMMANDS(cht_wiringpitoplevel_entries))