File:  [Coherent Logic Development] / freem / src / ssvn_zrpi.c
Revision 1.2: download - view: text, annotated - select for diffs
Sun Mar 9 15:20:18 2025 UTC (12 months, 3 weeks ago) by snw
Branches: MAIN
CVS tags: HEAD
Begin formatting overhaul and REUSE compliance

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

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