pigpio/command.c

406 lines
12 KiB
C
Raw Normal View History

2013-12-12 11:27:22 +01:00
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
/*
2014-01-21 20:04:59 +01:00
This version is for pigpio version 11+
2013-12-12 11:27:22 +01:00
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include "pigpio.h"
#include "command.h"
2014-01-21 20:04:59 +01:00
/* retv
pigs pipe
0 "" <0 ERR %d
1 "" <0 ERR %d
2 %d <0 ERR %d
3 %08X %08X
4 %u %u
5 HELP HELP
*/
/* vfyt
1 cmd
2 cmd %d
3 cmd %d %d
4 cmd %d %x
6 HELP
7 cmd %x
8 MODES %d %c
9 PUD %d %c
10 PROG %s
*/
2013-12-12 11:27:22 +01:00
cmdInfo_t cmdInfo[]=
{
2014-01-21 20:04:59 +01:00
/* num str vfyt retv ext */
{PI_CMD_BC1, "BC1", 7, 1, 0},
{PI_CMD_BC2, "BC2", 7, 1, 0},
{PI_CMD_BR1, "BR1", 1, 3, 0},
{PI_CMD_BR2, "BR2", 1, 3, 0},
{PI_CMD_BS1, "BS1", 7, 1, 0},
{PI_CMD_BS2, "BS2", 7, 1, 0},
{PI_CMD_HELP, "H", 6, 5, 0},
{PI_CMD_HELP, "HELP", 6, 5, 0},
{PI_CMD_HWVER, "HWVER", 1, 4, 0},
{PI_CMD_MODEG, "MG" , 2, 2, 0},
{PI_CMD_MODEG, "MODEG", 2, 2, 0},
{PI_CMD_MODES, "M", 8, 0, 0},
{PI_CMD_MODES, "MODES", 8, 0, 0},
{PI_CMD_NB, "NB", 4, 0, 0},
{PI_CMD_NC, "NC", 2, 0, 0},
{PI_CMD_NO, "NO", 1, 2, 0},
{PI_CMD_NP, "NP", 2, 0, 0},
{PI_CMD_PFG, "PFG", 2, 2, 0},
{PI_CMD_PFS, "PFS", 3, 2, 0},
{PI_CMD_PIGPV, "PIGPV", 1, 4, 0},
{PI_CMD_PRG, "PRG", 2, 2, 0},
{PI_CMD_PROC, "PROC", 10, 2, 1},
{PI_CMD_PROCD, "PROCD", 2, 2, 0},
{PI_CMD_PROCR, "PROCR", 2, 2, 0},
{PI_CMD_PROCS, "PROCS", 2, 2, 0},
{PI_CMD_PRRG, "PRRG", 2, 2, 0},
{PI_CMD_PRS, "PRS", 3, 2, 0},
{PI_CMD_PUD, "PUD", 9, 0, 0},
{PI_CMD_PWM, "P", 3, 0, 0},
{PI_CMD_PWM, "PWM", 3, 0, 0},
{PI_CMD_READ, "R", 2, 2, 0},
{PI_CMD_READ, "READ", 2, 2, 0},
{PI_CMD_SERVO, "S", 3, 0, 0},
{PI_CMD_SERVO, "SERVO", 3, 0, 0},
{PI_CMD_WDOG, "WDOG", 3, 0, 0},
{PI_CMD_WRITE, "W", 3, 0, 0},
{PI_CMD_WRITE, "WRITE", 3, 0, 0},
{PI_CMD_TICK, "T", 1, 4, 0},
{PI_CMD_TICK, "TICK", 1, 4, 0},
{PI_CMD_TRIG, "TRIG", 5, 0, 1},
{PI_CMD_WVAS, "WVAS", 11, 2, 3},
{PI_CMD_WVBSY, "WVBSY", 1, 2, 0},
{PI_CMD_WVCLR, "WVCLR", 1, 2, 0},
{PI_CMD_WVGO, "WVGO" , 1, 2, 0},
{PI_CMD_WVGOR, "WVGOR", 1, 2, 0},
{PI_CMD_WVHLT, "WVHLT", 1, 2, 0},
{PI_CMD_WVSC, "WVSC", 2, 2, 0},
{PI_CMD_WVSM, "WVSM", 2, 2, 0},
{PI_CMD_WVSP, "WVSP", 2, 2, 0},
2013-12-12 11:27:22 +01:00
};
char * cmdUsage = "\
BC1 x clear gpios in bank 1\n\
BC2 x clear gpios in bank 2\n\
2014-01-21 20:04:59 +01:00
BR1 read gpios bank 1\n\
BR2 read gpios bank 2\n\
2013-12-12 11:27:22 +01:00
BS1 x set gpios in bank 1\n\
BS2 x set gpios in bank 2\n\
2014-01-21 20:04:59 +01:00
H displays command help\n\
HELP displays command help\n\
2013-12-12 11:27:22 +01:00
HWVER return hardware version\n\
2014-01-21 20:04:59 +01:00
M g m set gpio mode\n\
MG g get gpio mode\n\
MODEG g get gpio mode\n\
MODES g m set gpio mode\n\
2013-12-12 11:27:22 +01:00
NB h x start notification\n\
NC h close notification\n\
2014-01-21 20:04:59 +01:00
NO request notification handle\n\
NP h pause notification\n\
P u d set PWM value for gpio\n\
2013-12-12 11:27:22 +01:00
PFG u get PWM frequency for gpio\n\
2014-01-21 20:04:59 +01:00
PFS u d set PWM frequency for gpio\n\
2014-01-12 22:31:59 +01:00
PIGPV return pigpio version\n\
2013-12-12 11:27:22 +01:00
PRG u get PWM range for gpio\n\
2014-01-21 20:04:59 +01:00
PROC t validate and store script\n\
PROCD s delete script\n\
PROCR s run script\n\
PROCS s stop script\n\
2013-12-12 11:27:22 +01:00
PRRG u get PWM real range for gpio\n\
2014-01-21 20:04:59 +01:00
PRS u d set PWM range for gpio\n\
2013-12-12 11:27:22 +01:00
PUD g p set gpio pull up/down\n\
2014-01-21 20:04:59 +01:00
PWM u d set PWM value for gpio\n\
R g read gpio\n\
READ g read gpio\n\
S u d set servo value for gpio\n\
SERVO u d set servo value for gpio\n\
T return current tick\n\
TICK return current tick\n\
TRIG u pl l trigger level l for pl micros on gpio\n\
W g l write level to gpio\n\
2013-12-12 11:27:22 +01:00
WDOG u d set watchdog on gpio\n\
2014-01-21 20:04:59 +01:00
WRITE g l write level to gpio\n\
WVAS u b t wave add serial data\n\
WVBSY check if wave busy\n\
WVCLR wave clear\n\
WVGO wave transmit\n\
WVGOR wave transmit repeat\n\
WVHLT wave stop\n\
WVSC ws wave get cbs stats\n\
WVSM ws wave get micros stats\n\
WVSP ws wave get pulses stats\n\
2013-12-12 11:27:22 +01:00
\n\
2014-01-21 20:04:59 +01:00
b = baud rate\n\
2013-12-12 11:27:22 +01:00
d = decimal value\n\
g = gpio (0-53)\n\
h = handle (0-31)\n\
2014-01-21 20:04:59 +01:00
l = level (0-1)\n\
2013-12-12 11:27:22 +01:00
m = mode (RW540123)\n\
p = pud (ODU)\n\
2014-01-21 20:04:59 +01:00
pl = pulse length (0-100)\n\
s = script id\n\
t = text\n\
2013-12-12 11:27:22 +01:00
u = user gpio (0-31)\n\
x = hex value\n\
";
typedef struct
{
int error;
char * str;
} errInfo_t;
static errInfo_t errInfo[]=
{
{PI_INIT_FAILED , "pigpio initialisation failed"},
{PI_BAD_USER_GPIO , "gpio not 0-31"},
{PI_BAD_GPIO , "gpio not 0-53"},
{PI_BAD_MODE , "mode not 0-7"},
{PI_BAD_LEVEL , "level not 0-1"},
{PI_BAD_PUD , "pud not 0-2"},
{PI_BAD_PULSEWIDTH , "pulsewidth not 0 or 500-2500"},
2014-01-21 20:04:59 +01:00
{PI_BAD_DUTYCYCLE , "dutycycle outside set range"},
2013-12-12 11:27:22 +01:00
{PI_BAD_TIMER , "timer not 0-9"},
{PI_BAD_MS , "ms not 10-60000"},
{PI_BAD_TIMETYPE , "timetype not 0-1"},
{PI_BAD_SECONDS , "seconds < 0"},
{PI_BAD_MICROS , "micros not 0-999999"},
{PI_TIMER_FAILED , "gpioSetTimerFunc failed"},
{PI_BAD_WDOG_TIMEOUT , "timeout not 0-60000"},
{PI_NO_ALERT_FUNC , "DEPRECATED"},
{PI_BAD_CLK_PERIPH , "clock peripheral not 0-1"},
{PI_BAD_CLK_SOURCE , "clock source not 0-1"},
{PI_BAD_CLK_MICROS , "clock micros not 1, 2, 4, 5, 8, or 10"},
{PI_BAD_BUF_MILLIS , "buf millis not 100-10000"},
2013-12-12 11:32:49 +01:00
{PI_BAD_DUTYRANGE , "dutycycle range not 25-40000"},
2013-12-12 11:27:22 +01:00
{PI_BAD_SIGNUM , "signum not 0-63"},
{PI_BAD_PATHNAME , "can't open pathname"},
{PI_NO_HANDLE , "no handle available"},
{PI_BAD_HANDLE , "unknown notify handle"},
{PI_BAD_IF_FLAGS , "ifFlags > 3"},
{PI_BAD_CHANNEL , "DMA channel not 0-14"},
{PI_BAD_SOCKET_PORT , "socket port not 1024-30000"},
{PI_BAD_FIFO_COMMAND , "unknown fifo command"},
{PI_BAD_SECO_CHANNEL , "DMA secondary channel not 0-6"},
{PI_NOT_INITIALISED , "function called before gpioInitialise"},
{PI_INITIALISED , "function called after gpioInitialise"},
{PI_BAD_WAVE_MODE , "waveform mode not 0-1"},
{PI_BAD_CFG_INTERNAL , "bad parameter in gpioCfgInternals call"},
{PI_BAD_WAVE_BAUD , "baud rate not 100-250000"},
{PI_TOO_MANY_PULSES , "waveform has too many pulses"},
{PI_TOO_MANY_CHARS , "waveform has too many chars"},
{PI_NOT_SERIAL_GPIO , "no serial read in progress on gpio"},
2014-01-12 22:31:59 +01:00
{PI_BAD_SERIAL_STRUC , "bad (null) serial structure parameter"},
{PI_BAD_SERIAL_BUF , "bad (null) serial buf parameter"},
2013-12-12 11:32:49 +01:00
{PI_NOT_PERMITTED , "no permission to update gpio"},
{PI_SOME_PERMITTED , "no permission to update one or more gpios"},
2014-01-21 20:04:59 +01:00
{PI_BAD_WVSC_COMMND , "bad WVSC subcommand"},
{PI_BAD_WVSM_COMMND , "bad WVSM subcommand"},
{PI_BAD_WVSP_COMMND , "bad WVSP subcommand"},
{PI_BAD_PULSELEN , "trigger pulse > 100 microseconds"},
{PI_BAD_SCRIPT , "invalid script"},
{PI_BAD_SCRIPT_ID , "unknown script id"},
{PI_BAD_SER_OFFSET , "add serial data offset > 30 minute"},
2013-12-12 11:27:22 +01:00
};
static char * fmtMdeStr="RW540123";
static char * fmtPudStr="ODU";
static int cmdMatch(char * str)
{
int i;
for (i=0; i<(sizeof(cmdInfo)/sizeof(cmdInfo_t)); i++)
{
if (strcasecmp(str, cmdInfo[i].name) == 0) return i;
}
return -1;
}
2014-01-21 20:04:59 +01:00
int cmdParse(char *buf, cmdCmd_t *cmd, int argc, char *argv[], gpioExtent_t *ext)
2013-12-12 11:27:22 +01:00
{
char str[8];
int f, valid, idx, val;
2014-01-21 20:04:59 +01:00
char *ptr;
2013-12-12 11:27:22 +01:00
char c, t;
sscanf(buf, " %7s", str);
cmd->cmd = -1;
idx = cmdMatch(str);
if (idx < 0) return idx;
valid = 0;
cmd->cmd = cmdInfo[idx].cmd;
cmd->p1 = 0;
cmd->p2 = 0;
switch (cmdInfo[idx].vt)
{
2014-01-21 20:04:59 +01:00
case 1: /* BR1 BR2 HWVER NO PIGPV TICK WVBSY WVCLR WVGO WVGOR
WVHLT
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %c", str, &t);
if (f == 1) valid = 1;
break;
2014-01-21 20:04:59 +01:00
case 2: /* MODEG NC NP PFG PRG PROCD PROCR PROCS PRRG READ
WVSC WVSM WVSP
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %d %c", str, &cmd->p1, &t);
if (f == 2) valid = 1;
break;
2014-01-21 20:04:59 +01:00
case 3: /* PFS PRS PWM SERVO WDOG WRITE
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %d %d %c", str, &cmd->p1, &cmd->p2, &t);
if (f == 3) valid = 1;
break;
2014-01-21 20:04:59 +01:00
case 4: /* NB
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %d %x %c", str, &cmd->p1, &cmd->p2, &t);
if (f == 3) valid = 1;
break;
2014-01-21 20:04:59 +01:00
case 5: /* TRIG
*/
f = sscanf(buf, " %7s %d %d %d %c",
str, &cmd->p1, &cmd->p2, &ext[0].data, &t);
if (f == 4)
{
ext[0].n = sizeof(unsigned);
ext[0].ptr = &ext[0].data;
valid = 1;
}
break;
case 6: /* HELP
*/
2013-12-12 11:27:22 +01:00
valid = 1;
break;
2014-01-21 20:04:59 +01:00
case 7: /* BC1 BC2 BS1 BS2
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %x %c", str, &cmd->p1, &t);
if (f == 2) valid = 1;
break;
2014-01-21 20:04:59 +01:00
case 8: /* MODES
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %d %c %c", str, &cmd->p1, &c, &t);
if (f == 3)
{
val = toupper(c);
ptr = strchr(fmtMdeStr, val);
if (ptr != NULL)
{
val = ptr - fmtMdeStr;
cmd->p2 = val;
valid = 1;
}
}
break;
2014-01-21 20:04:59 +01:00
case 9: /* PUD
*/
2013-12-12 11:27:22 +01:00
f = sscanf(buf, " %7s %d %c %c", str, &cmd->p1, &c, &t);
if (f == 3)
{
val = toupper(c);
ptr = strchr(fmtPudStr, val);
if (ptr != NULL)
{
val = ptr - fmtPudStr;
cmd->p2 = val;
valid = 1;
}
}
break;
2014-01-21 20:04:59 +01:00
case 10: /* PROC
*/
if (argc == 3)
{
cmd->p1 = strlen(argv[2]);
ext[0].n = cmd->p1;
ext[0].ptr = argv[2];
valid = 1;
}
break;
case 11: /* WVAS
*/
if (argc == 6)
{
f = sscanf(buf, " %7s %d %d %d ",
str, &cmd->p1, &ext[0].data, &ext[1].data);
if (f == 4)
{
ext[0].n = sizeof(unsigned);
ext[0].ptr = &ext[0].data;
ext[1].n = sizeof(unsigned);
ext[1].ptr = &ext[1].data;
cmd->p2 = strlen(argv[5]);
ext[2].n = cmd->p2;
ext[2].ptr = argv[5];
valid = 1;
}
}
break;
2013-12-12 11:27:22 +01:00
}
if (valid) return idx;
else return -1;
}
char * cmdErrStr(int error)
{
int i;
for (i=0; i<(sizeof(errInfo)/sizeof(errInfo_t)); i++)
{
if (errInfo[i].error == error) return errInfo[i].str;
}
return "unknown error";
}