chiark / gitweb /
f3b7cb8f3e64650ee4fefbe4e41814333a2942a4
[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
54 /*---------- important types and forward declarations ----------*/
55
56 static int ensure_setup(void) {
57   return TCL_OK;
58 }
59
60 /*---------- operations ----------*/
61
62 #define CHECK_SETUP                             \
63   int rc;                                       \
64   rc = ensure_setup();                          \
65   if (rc) return rc; else
66
67 #define CHECK_RANGE(val, min, max, what, VAL) do{               \
68     if (val < (min))                                            \
69       return cht_staticerr(ip, what " too small"                \
70                            " (must be at least" #min ")",       \
71                            "WIRINGPI RANGE " VAL " MIN " #min); \
72     if (val > (max))                                            \
73       return cht_staticerr(ip, what " too large"                \
74                            " (must be at most" #max ")",        \
75                            "WIRINGPI RANGE " VAL " MAX " #max); \
76   }while(0)
77
78 int cht_do_wiringpitcl_pwmc(ClientData cd, Tcl_Interp *ip, int divider) {
79   CHECK_SETUP;
80   CHECK_RANGE(divider,1,4095,"divider for pwmc","DIVIDER");
81   pwmSetClock(divider);
82   return TCL_OK;
83 }
84
85 int cht_do_wiringpitcl_boardId(ClientData cd, Tcl_Interp *ip,
86                                Tcl_Obj **result) {
87 #define N 5
88   CHECK_SETUP;
89   int ints[N];
90   Tcl_Obj *objl[N];
91   piBoardId(&ints[0],
92             &ints[1],
93             &ints[2],
94             &ints[3],
95             &ints[4]);
96   int i;
97   for (i=0; i<N; i++) objl[i]= Tcl_NewIntObj(ints[i]);
98   *result= Tcl_NewListObj(N,objl);
99   return TCL_OK;
100 #undef N
101 }
102
103 int cht_do_wiringpitcl_boardRev(ClientData cd, Tcl_Interp *ip, int *result) {
104   CHECK_SETUP;
105   *result = piBoardRev();
106   return TCL_OK;
107 }
108
109 /*---------- families operations ----------*/
110
111 int cht_do_wiringpitcl_aread(ClientData cd, Tcl_Interp *ip, int pin, int *result) {
112   CHECK_SETUP;
113   *result = analogRead(pin);
114   return TCL_OK;
115 }
116
117 #define SIMPLE_WRITER_OP(op, min, max, wpicall)                 \
118   int cht_do_wiringpitcl_##op(ClientData cd, Tcl_Interp *ip,    \
119                               int pin, int val) {               \
120     CHECK_SETUP                                                 \
121     CHECK_RANGE(val,min,max, "value for " #op, "VAL");          \
122     wpicall(pin, val);                                          \
123     return TCL_OK;                                              \
124   }
125
126 SIMPLE_WRITER_OP(pwm,     INT_MIN,INT_MAX, pwmWrite)
127 SIMPLE_WRITER_OP(awrite,  INT_MIN,INT_MAX, analogWrite)
128 SIMPLE_WRITER_OP(write,   0,      1,       digitalWrite)
129 SIMPLE_WRITER_OP(pwmTone, INT_MIN,INT_MAX, pwmToneWrite)
130 SIMPLE_WRITER_OP(clock,   INT_MIN,INT_MAX, gpioClockSet)
131
132 #define MODES(M)                                                \
133   M(in,       { pinMode        (pin, INPUT); })                 \
134   M(input,    { pinMode        (pin, INPUT); })                 \
135   M(out,      { pinMode        (pin, OUTPUT); })                \
136   M(output,   { pinMode        (pin, OUTPUT); })                \
137   M(pwm,      { pinMode        (pin, PWM_OUTPUT); })            \
138   M(pwmTone,  { pinMode        (pin, PWM_TONE_OUTPUT); })       \
139   M(clock,    { pinMode        (pin, GPIO_CLOCK); })            \
140   M(up,       { pullUpDnControl(pin, PUD_UP); })                \
141   M(down,     { pullUpDnControl(pin, PUD_DOWN); })              \
142   M(tri,      { pullUpDnControl(pin, PUD_OFF); })               \
143   M(off,      { pullUpDnControl(pin, PUD_OFF); })               \
144   M(alt0,     { pinModeAlt(pin, 0b100); })                      \
145   M(alt1,     { pinModeAlt(pin, 0b101); })                      \
146   M(alt2,     { pinModeAlt(pin, 0b110); })                      \
147   M(alt3,     { pinModeAlt(pin, 0b111); })                      \
148   M(alt4,     { pinModeAlt(pin, 0b011); })                      \
149   M(alt5,     { pinModeAlt(pin, 0b010); })
150
151 #define MODE_FUNC(name, body) static void mode_##name(int pin) { body }
152   MODES(MODE_FUNC)
153
154 const WiringPiTclModeInfo cht_wiringpitclmodeinfo_entries[] = {
155 #define MODEINFO_ENTRY(name, body) { #name, mode_##name },
156   MODES(MODEINFO_ENTRY)
157   { 0 },
158 };
159
160 int cht_do_wiringpitcl_mode(ClientData cd, Tcl_Interp *ip, int pin,
161                             const WiringPiTclModeInfo *mode) {
162   int rc;
163   rc = ensure_setup();  if (rc) return rc;
164   mode->func(pin);
165   return TCL_OK;
166 }
167
168
169 /*---------- main hooks for tcl ----------*/
170
171 CHT_INIT(wiringpi, {}, CHTI_COMMANDS(cht_wiringpitoplevel_entries))