Annotation of freem/src/ssvn_zrpi.c, revision 1.4

1.1       snw         1: /*
1.4     ! snw         2:  *   $Id: ssvn_zrpi.c,v 1.3 2025/03/09 19:50:47 snw Exp $
1.1       snw         3:  *    Support for Raspberry Pi single-board computers
                      4:  *
                      5:  *  
1.2       snw         6:  *   Author: Serena Willis <snw@coherent-logic.com>
1.1       snw         7:  *    Copyright (C) 1998 MUG Deutschland
1.3       snw         8:  *    Copyright (C) 2020, 2025 Coherent Logic Development LLC
1.1       snw         9:  *
                     10:  *
                     11:  *   This file is part of FreeM.
                     12:  *
                     13:  *   FreeM is free software: you can redistribute it and/or modify
                     14:  *   it under the terms of the GNU Affero General Public License as published by
                     15:  *   the Free Software Foundation, either version 3 of the License, or
                     16:  *   (at your option) any later version.
                     17:  *
                     18:  *   FreeM is distributed in the hope that it will be useful,
                     19:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21:  *   GNU Affero General Public License for more details.
                     22:  *
                     23:  *   You should have received a copy of the GNU Affero General Public License
                     24:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
                     25:  *
1.4     ! snw        26:  *   $Log: ssvn_zrpi.c,v $
        !            27:  *   Revision 1.3  2025/03/09 19:50:47  snw
        !            28:  *   Second phase of REUSE compliance and header reformat
        !            29:  *
1.3       snw        30:  *
                     31:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     32:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        33:  **/
                     34: 
                     35: #include <stdlib.h>
                     36: #include <stdio.h>
                     37: #include <string.h>
                     38: 
                     39: #include "mpsdef.h"
                     40: #include "mref.h"
                     41: #include "ssvn_zrpi.h"
                     42: 
                     43: #if defined(HAVE_WIRINGPI_H)
                     44: # include <wiringPi.h>
                     45: #endif
                     46: 
                     47: int rpi_pinmode[RPI_PINCOUNT];
                     48: 
                     49: void ssvn_zrpi_init (void)
                     50: {
1.4     ! snw        51:   
        !            52: #if defined(HAVE_WIRINGPI_H)
1.1       snw        53:     register int i;
                     54:     
                     55:     wiringPiSetup ();
                     56:     
                     57:     for (i = 0; i < RPI_PINCOUNT; i++) {
                     58:         pinMode (i, INPUT);
                     59:         rpi_pinmode[i] = INPUT;
                     60:     }
                     61: 
                     62: #endif
1.4     ! snw        63:     return;
1.1       snw        64: }
                     65: 
                     66: void ssvn_zrpi (short action, char *key, char *data)
                     67: {
                     68:     freem_ref_t *ref = (freem_ref_t *) malloc (sizeof (freem_ref_t));
                     69:     char *kbuf = (char *) malloc (STRLEN * sizeof (char));    
                     70:     char *verb = (char *) malloc (STRLEN * sizeof (char));
                     71: 
                     72:     NULLPTRCHK(ref,"ssvn_zrpi");
                     73:     NULLPTRCHK(kbuf,"ssvn_zrpi");
                     74:     NULLPTRCHK(verb,"ssvn_zrpi");
                     75:     
                     76: #if !defined(HAVE_WIRINGPI_H)
                     77: 
                     78:     /* not on a Raspberry Pi */
                     79: 
                     80:     *data = EOL;
                     81:     merr_raise (NORPI);
                     82: 
                     83:     goto done;
                     84: 
                     85: #else
                     86: 
                     87:     mref_init (ref, MREF_RT_SSVN, "");
                     88:     internal_to_mref (ref, key);
                     89: 
                     90:     stcpy (kbuf, key);
                     91: 
                     92: 
                     93:     if (strcmp (ref->subscripts[0], "INITIALIZE") == 0) {
                     94: 
                     95:         ssvn_zrpi_init ();
                     96: 
                     97:         *data = EOL;
                     98:         merr_raise (OK);
                     99: 
                    100:         goto done;
                    101: 
                    102:     }
                    103: 
                    104:     if (strcmp (ref->subscripts[0], "GPIO") != 0) {
                    105:         merr_raise (INVREF);
                    106:         goto done;
                    107:     }
                    108: 
                    109:     if (ref->subscript_count < 3) {
                    110:         merr_raise (INVREF);
                    111:         goto done;
                    112:     }
                    113: 
                    114:     pin = atol (ref->subscripts[1]); 
                    115:     strncpy (verb, ref->subscripts[2], 255);
                    116: 
                    117:     if (pin < 0 || pin > RPI_PINCOUNT) {
                    118:         merr_raise (INVREF);
                    119:         goto done;
                    120:     }
                    121:     
                    122:     switch (action) {
                    123: 
                    124:         
                    125:         case get_sym:
                    126: 
                    127:             if (strcmp (verb, "MODE") == 0) {
                    128: 
                    129:                 switch (rpi_pinmode[pin]) {
                    130: 
                    131:                     case INPUT:
                    132:                         sprintf (data, "INPUT\201");
                    133:                         break;
                    134: 
                    135:                     case OUTPUT:
                    136:                         sprintf (data, "OUTPUT\201");
                    137:                         break;
                    138: 
                    139:                     case PWM_OUTPUT:
                    140:                         sprintf (data, "PWM_OUTPUT\201");
                    141:                         break;
                    142: 
                    143:                     case GPIO_CLOCK:
                    144:                         sprintf (data, "GPIO_CLOCK\201");
                    145:                         break;
                    146:                 }
                    147:                        
                    148:                 
                    149:             }
                    150:             else if (strcmp (verb, "DIGITAL") == 0) {
                    151: 
                    152:                 sprintf (data, "%d\201", digitalRead (pin));
                    153:                 break;
                    154:                 
                    155:             }
                    156:             else if (strcmp (verb, "ANALOG") == 0) {
                    157: 
                    158:                 sprintf (data, "%d\201", analogRead (pin));
                    159:                 break;
                    160:                 
                    161:             }            
                    162:             else {
                    163:                 merr_raise (INVREF);
                    164:                 goto done;
                    165:             }
                    166: 
                    167:             goto done;
                    168: 
                    169:             
                    170:         case set_sym:
                    171: 
                    172:             
                    173:             if (strcmp (verb, "MODE") == 0) {
                    174: 
                    175:                 if (stcmp (data, "INPUT\201") == 0) {
                    176:                     pinMode (pin, INPUT);
                    177:                     rpi_pinmode[pin] = INPUT;
                    178:                 }
                    179:                 else if (stcmp (data, "OUTPUT\201") == 0) {
                    180:                     pinMode (pin, OUTPUT);
                    181:                     rpi_pinmode[pin] = OUTPUT;
                    182:                 }
                    183:                 else if (stcmp (data, "PWM_OUTPUT\201") == 0) {
                    184:                     pinMode (pin, PWM_OUTPUT);
                    185:                     rpi_pinmode[pin] = PWM_OUTPUT;
                    186:                 }
                    187:                 else if (stcmp (data, "GPIO_CLOCK\201") == 0) {
                    188:                     pinMode (pin, GPIO_CLOCK);
                    189:                     rpi_pinmode[pin] = GPIO_CLOCK;
                    190:                 }
                    191:                 else {
                    192:                     merr_raise (INVREF);
                    193:                     goto done;
                    194:                 }
                    195: 
                    196:                 goto done;
                    197:                 
                    198:             }
                    199:             else if (strcmp (verb, "DIGITAL") == 0) {
                    200: 
                    201:                 char dta[255];
                    202:                 int val;
                    203: 
                    204:                 stcpy (dta, data);
                    205:                 stcnv_m2c (dta);
                    206: 
                    207:                 val = atoi (dta);
                    208: 
                    209:                 switch (val) {
                    210: 
                    211:                     case 0:
                    212:                         digitalWrite (pin, LOW);
                    213:                         goto done;
                    214: 
                    215:                     case 1:
                    216:                         digitalWrite (pin, HIGH);
                    217:                         goto done;
                    218: 
                    219:                     default:
                    220:                         merr_raise (INVREF);
                    221:                         goto done;
                    222: 
                    223:                 }
                    224:                 
                    225:                 goto done;
                    226: 
                    227:             }
                    228:             else if (strcmp (verb, "ANALOG") == 0) {
                    229: 
                    230:                 char dta[255];
                    231:                 int val;
                    232: 
                    233:                 stcpy (dta, data);
                    234:                 stcnv_m2c (dta);
                    235: 
                    236:                 val = atoi (dta);
                    237: 
                    238:                 if (val < 0 || val > 1024) {
                    239:                     merr_raise (INVREF);
                    240:                     goto done;
                    241:                 }
                    242:                 
                    243:                 analogWrite (pin, val);
                    244:                 
                    245:                 goto done;
                    246:                 
                    247:             }
                    248:             else {
                    249:                 merr_raise (INVREF);
                    250:                 goto done;
                    251:             }
                    252: 
                    253:             goto done;
                    254: 
                    255: 
                    256:         default:
                    257: 
                    258:             merr_raise (INVREF);
                    259:             goto done;
                    260: 
                    261:             break;
                    262: 
                    263:     }
                    264:     
                    265: #endif
                    266: 
                    267:     
                    268: done:
                    269:                 
                    270:     free (ref);
                    271:     free (kbuf);
                    272:     free (verb);
                    273:     
                    274:     return;
                    275: 
                    276: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>