Added command to export random integers as per suggestion in #63

Adds a `-R n/ --randint n` command which returns a sequence of n
16bit integers as generated by `true_rand()`.
This commit is contained in:
ea 2018-01-28 14:36:40 -06:00
parent 6ba460e766
commit eceb65a96a
2 changed files with 39 additions and 1 deletions

View File

@ -259,6 +259,11 @@ class GoodWatch:
def dmesg(self): def dmesg(self):
"""Returns the DMESG buffer.""" """Returns the DMESG buffer."""
return self.transact("\x04"); return self.transact("\x04");
def randint(self,n):
"""Returns n random 16bit integer. """
import struct
r = struct.unpack("<"+"H"*n,self.transact("\x05\x00"+chr16(n)));
return "%04x "*n%r
def radioonoff(self,on=1): def radioonoff(self,on=1):
"""Turns the radio on or off.""" """Turns the radio on or off."""
return self.transact("\x10"+chr(on)); return self.transact("\x10"+chr(on));
@ -301,7 +306,8 @@ if __name__=='__main__':
help='Write a string the LCD.'); help='Write a string the LCD.');
parser.add_argument('-D','--dmesg', parser.add_argument('-D','--dmesg',
help='Prints the dmesg.',action='count'); help='Prints the dmesg.',action='count');
parser.add_argument('-R','--randint',
help='Get RANDINT random 16bit integers.');
parser.add_argument('-b','--beacon', parser.add_argument('-b','--beacon',
help='Transmits a beacon.'); help='Transmits a beacon.');
parser.add_argument('-B','--beaconsniff', parser.add_argument('-B','--beaconsniff',
@ -330,6 +336,8 @@ if __name__=='__main__':
if args.dmesg>0: if args.dmesg>0:
print goodwatch.dmesg(); print goodwatch.dmesg();
if args.randint != None:
print goodwatch.randint(int(args.randint));
if args.beacon!=None: if args.beacon!=None:
print "Turning radio on."; print "Turning radio on.";

View File

@ -12,6 +12,7 @@
#include<string.h> #include<string.h>
#include "api.h" #include "api.h"
#include "rng.h"
enum { enum {
SETTURBOMODE = 0x00, SETTURBOMODE = 0x00,
@ -19,6 +20,7 @@ enum {
POKE = 0x02, POKE = 0x02,
LCDSTRING = 0x03, LCDSTRING = 0x03,
DMESG = 0x04, DMESG = 0x04,
RANDINT = 0x05,
RADIOONOFF = 0x10, RADIOONOFF = 0x10,
RADIOCONFIG = 0x11, RADIOCONFIG = 0x11,
@ -48,6 +50,30 @@ static void send_dmesg(){
uart_tx(0x00); uart_tx(0x00);
} }
//! Local command to generate and send n random integers.
static void send_randint(uint16_t n){
uint16_t i;
uint16_t len = n*2;
uint16_t rints[n]; // are var len arrays ok here?
for(i = 0; i < n; i++){ //pre-generate enough ints
rints[i] = true_rand();
}
//Start the frame
uart_tx(0x00);
uart_tx(0x80);
//Length
uart_tx(len&0xFF);
uart_tx(len>>8);
//Send integers byte by byte
for(i=0;i<len;i++){
uart_tx(((char*)rints)[i]);
}
// no checksum ?
uart_tx(0x00);
uart_tx(0x00);
}
//! Local pointer to packet.c's buffer. //! Local pointer to packet.c's buffer.
static uint8_t *packetbuf=0; static uint8_t *packetbuf=0;
@ -142,6 +168,10 @@ int monitor_handle(uint8_t *buffer, int len){
len=0; len=0;
break; break;
case RANDINT:
send_randint((uint16_t)buffer16[1]);
len=0;
break;
case RADIOONOFF: //One byte parameter, on or off. case RADIOONOFF: //One byte parameter, on or off.
if(buffer[1]){ if(buffer[1]){