Browse Source

support cmds from file, add advance and clear commands, change init

Don't start up w/ the first four channels on...  We only want that
for the LED to let us know it's running...
irr_shared
John-Mark Gurney 3 years ago
parent
commit
a5e5676405
2 changed files with 56 additions and 11 deletions
  1. +25
    -7
      irr_main.c
  2. +31
    -4
      lora.py

+ 25
- 7
irr_main.c View File

@@ -44,6 +44,8 @@ enum {
CMD_RUNFOR = 3,
CMD_PING = 4,
CMD_SETUNSET = 5,
CMD_ADV = 6,
CMD_CLEAR = 7,
};

/*
@@ -166,10 +168,11 @@ struct chaninfo {
bool init;
bool invert;
} chans[] = {
[0] = { .bank = GPIOB, .pinnum = GPIO_PIN_5, .init = true, .invert = true, },
[1] = { .bank = GPIOB, .pinnum = GPIO_PIN_6, .init = true, .invert = true, },
[2] = { .bank = GPIOB, .pinnum = GPIO_PIN_7, .init = true, .invert = true, },
[3] = { .bank = GPIOB, .pinnum = GPIO_PIN_9, .init = true, .invert = true, },
[0] = { .bank = GPIOB, .pinnum = GPIO_PIN_5, .invert = true, },
[1] = { .bank = GPIOB, .pinnum = GPIO_PIN_6, .invert = true, },
[2] = { .bank = GPIOB, .pinnum = GPIO_PIN_7, .invert = true, },
[3] = { .bank = GPIOB, .pinnum = GPIO_PIN_9, .invert = true, },
/* Turn on LED at start */
[4] = { .bank = GPIOB, .pinnum = GPIO_PIN_8, .init = true, },
};
#define nitems(x) (sizeof(x) / sizeof *(x))
@@ -212,8 +215,9 @@ static struct sched {
static int schedpos; /* position in schedule, % nitems(schedule)*/
static int schedcnt; /* total items waiting */

#define SCHED_HEAD (schedule[(schedpos) % nitems(schedule)])
#define SCHED_TAIL (schedule[(schedpos + schedcnt) % nitems(schedule)])
#define SCHED_ITEM(x) (schedule[(schedpos + x) % nitems(schedule)])
#define SCHED_HEAD SCHED_ITEM(0)
#define SCHED_TAIL SCHED_ITEM(schedcnt)

static void
start_sched(struct sched *sched)
@@ -271,7 +275,7 @@ static void
procmsg(struct pktbuf inbuf, struct pktbuf *outbuf)
{
uint32_t args[5];
int i, apos;
int i, apos, cnt;

i = 1;
apos = 0;
@@ -303,6 +307,20 @@ procmsg(struct pktbuf inbuf, struct pktbuf *outbuf)
set_chan(args[0], args[1]);
break;

case CMD_ADV:
cnt = 1;
if (apos == 1)
cnt = args[0];

for (i = 0; i < cnt && i < schedcnt; i++)
SCHED_ITEM(i).end_wait_tick = 0;
break;

case CMD_CLEAR:
if (schedcnt)
schedcnt = 1;
break;

default:
outbuf->pkt[0] = 0;
break;


+ 31
- 4
lora.py View File

@@ -49,6 +49,8 @@ CMD_WAITFOR = 2 # arg: (length): waits for length seconds
CMD_RUNFOR = 3 # arg: (chan, length): turns on chan for length seconds
CMD_PING = 4 # arg: (): a no op command
CMD_SETUNSET = 5 # arg: (chan, val): sets chan to val
CMD_ADV = 6 # arg: ([cnt]): advances to the next cnt (default 1) command
CMD_CLEAR = 7 # arg: (): clears all future commands, but keeps current running

class LORANode(object):
'''Implement a LORANode initiator.'''
@@ -128,6 +130,15 @@ class LORANode(object):
async def ping(self):
return await self._sendcmd(CMD_PING)

async def adv(self, cnt=None):
args = ()
if cnt is not None:
args = (cnt, )
return await self._sendcmd(CMD_ADV, *args)

async def clear(self):
return await self._sendcmd(CMD_CLEAR)

async def terminate(self):
return await self._sendcmd(CMD_TERMINATE)

@@ -223,6 +234,8 @@ async def main():

parser = argparse.ArgumentParser()

parser.add_argument('-f', dest='schedfile', metavar='filename', type=str,
help='Use commands from the file. One command per line.')
parser.add_argument('-r', dest='client', metavar='module:function', type=str,
help='Create a respondant instead of sending commands. Commands will be passed to the function.')
parser.add_argument('-s', dest='shared_key', metavar='shared_key', type=str, required=True,
@@ -289,18 +302,32 @@ async def main():

await l.start()

valid_cmds = { 'waitfor', 'setunset', 'runfor', 'ping', 'terminate', }
valid_cmds = {
'waitfor', 'setunset', 'runfor', 'ping', 'adv', 'clear',
'terminate',
}

if args.args and args.schedfile:
parser.error('only one of -f or arguments can be specified.')

if args.args:
cmds = list(args.args)
cmdargs = []
while cmds:
a, cmds = listsplit(cmds, '--')
cmdargs.append(a)
else:
with open(args.schedfile) as fp:
cmdargs = [ x.split() for x in fp.readlines() ]

cmdargs = list(args.args)
while cmdargs:
cmd = cmdargs.pop(0)
cmd, *args = cmdargs.pop(0)

if cmd not in valid_cmds:
print('invalid command:', repr(cmd))
sys.exit(1)

fun = getattr(l, cmd)
args, cmdargs = listsplit(cmdargs, '--')

await fun(*(int(x) for x in args))



Loading…
Cancel
Save