Kham
11-19-2009, 12:54 AM
Post if you found this useful :)
Code also attached as file.
"""
This script was a learning exercise for myself to get up to speed with Snappy.
I previsouly used Bascom AVR.
This was geared to convert an existing project over.
Basically a remote RFID Reader.
The functions I needed to learn was
-Pin monitoring
-Pin manipulation
-Serial processing
-peek/poke registers
-RPC's
The information was gathered from the forums and supplied docs/demos.
If I had this it would have saved me a few hours of hunting around.
So if you think like me, it should be most useful :)
Software: Portal 2.2.25
Hardware: ZIC2410 EVB3
http://forums.synapse-wireless.com/showthread.php?t=446&highlight=zic2410
We started with bit 0 of port 0 as IO 0, bit 1 of port 0 as IO 1, etc. So the resulting IO numbering is simply.
0 = P0.0 uart
1 = P0.1 uart
2 = P0.2
3 = P0.3
4 = P0.4
5 = P0.5
6 = P0.6
7 = P0.7
8 = P1.0
9 = P1.1
10 = P1.2
11 = P1.3
12 = P1.4
13 = P1.5
14 = P1.6
15 = P1.7
16 = P3.0
17 = P3.1
18 = P3.2
19 = P3.3
20 = P3.4
21 = P3.5
22 = P3.6
23 = P3.7
"""
#Rather than having to read multiple files, required definitions have copied over to this script.
#from synapse.evalBase import *
#from hexSupport.py import *
#from switchboard import *
#ZIC2410EVB3.py
#define constants
DS_NULL = 0
DS_UART0 = 1
DS_UART1 = 2
DS_TRANSPARENT = 3
DS_STDIO = 4
DS_CLI = 5
DS_PACKET_SERIAL = 6
LED1 = 3 # P0.3
LED2 = 2 # P0.2
LED3 = 1 # P0.1
LED4 = 16 # P3.0
SW1 = 11 # P1.3
SW2 = 12 # P1.4
SW3 = 18 # P3.2
SW4 = 19 # P3.3
#Define global Variables
# Track the current state of the LED, so we can toggle it
ledState = False
serial_in_data = ""
@setHook(HOOK_STARTUP)
def startupEvent():
"""Executed when the device boots up, this initializes the hardware and begins monitoring the button."""
# On an EVB3, LED4 is on the same pin as UART0 RX
# Shut down UART0 so we can control the pin manually
initUart(0,0)
# Initialize LED pins as outputs
setPinDir(LED1, True)
setPinDir(LED2, True)
setPinDir(LED3, True)
setPinDir(LED4, True)
#Turn them all off
writePin(LED1, False)
writePin(LED2, False)
writePin(LED3, False)
writePin(LED4, False)
setPinDir(SW1, False)
monitorPin(SW1, True)
#Default is check every 100ms. 2 = 10ms
setRate(2)
#Enable the uart , shortform, assume, 8,n,1 >
initUart(1, 19200)
#Must use character mode as no /r (carrige return) is sent by the device
#If character mode is not used and no /r is recieved, the buffer will just fill up until it overflows.
stdinMode(1, False) # Char mode, no echo
#Connect it to an output, otherwise print command won't go anywhere
crossConnect(DS_UART1, DS_STDIO)
#Was doing some network testing and didn't want to have to walk too far to loose the signal.
#Built in txPwr() not yet supported for the zic2410
setTXPowerLo()
@setHook(HOOK_GPIN)
#called on press and release of buttons
def buttonEvent(pin, isSet):
"""This event handler runs when the select switch (button) is pressed"""
global ledState, serial_in_data
# Take action on "press" of the button, when 'isSet' is False
if not isSet:
ledState = not ledState # Toggle the LED state variable
writePin(LED1, ledState) # ...and write that state to the hardware.
print "Button pressed"
print "\x41\x42\x43" #Prints hex eg hex41 = decmil 65 = A (uppercase A)
rpc("\x00\x00\x01", 'portal_print', "123")
#Calls a function on a remote node. In this case the portal.
#This function prints the supplied argument in the event log
#Handy for debugging when you are using the local UART for something
print serial_in_data
#needs to be reset somewhere, otherwise it will keep accunmlating
serial_in_data = ""
#time for some serial testing
#stdin line-buffer limit (40), from gps sample
#tested to 126? v2.2.25
@setHook(HOOK_STDIN)
def stdinEvent(serial_in_buffer):
global serial_in_data
print "rcvd:" , serial_in_buffer
print " rcvd chr ascii value:" , ord(serial_in_buffer)
print " rcvd chr hex value:" ,
printHex(ord(serial_in_buffer))
#if print hex is on same line as another print command then a none will be printed after the hex value
print ''
#or print '\r' ,
#note use of double == in conditional
if serial_in_buffer == '\x15':
print " hex check match"
buf_len = len(serial_in_buffer)
print " buffer len:" , len(serial_in_buffer) #buf_len
counter_1 = 0
#Read each character from the buffer into our variable
while(counter_1 < buf_len):
ser_char = serial_in_buffer[counter_1]
counter_1 += 1 #increment counter, shortform otherwise x = x + 1
serial_in_data += ser_char
#print "global ser:", serial_in_data
#serial_in_data = "serial rcvd"
#print serial_in_data
"""
if data == '?':
print "You want help?"
else:
print data
print "\r\n>",
"""
@setHook(HOOK_1S)
def timer1sEvent(currentS):
writePin(LED3, False)
pulsePin(LED4, 150, True)
#another way to check the pin state
if readPin(SW2) == False:
pulsePin(LED2, 150, True)
if readPin(SW3) == False:
writePin(LED3, True) #stays on until the timer event fires again.
#Functions
def hexNibble(nibble):
'''Convert a numeric nibble 0x0-0xF to its ASCII string representation "0"-"F"'''
hexStr = "0123456789ABCDEF"
return hexStr[nibble & 0xF]
def printHex(byte):
'''print a byte in hex - input is an integer, not a string'''
print hexNibble(byte >> 4),
print hexNibble(byte), # no trailing CR/LF
def dumpHex(str):
'''dump a string of bytes in hex'''
count = len(str)
index = 0
while index < count:
printHex(ord(str[index]))
index += 1
if (index & 15) == 0:
print
else:
print ' ',
print
'''
#pg 87, radio power level , ZIC2410.pdf
TX Output Power Level (dBm) TXPA0(0xA0) TXPA1(0xA1) TXPA2(0xA2)
8 10011111 11111111 01101111 159 , 255 , 111
-20 00010010 11100010 01101110 18 , 226 , 110
'''
def setTXPowerLo():
rpc("\x00\x00\x01", 'portal_print', "Set tx pwr low")
poke(0x22A0, 18)
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A1, 226)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A2, 110)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', memval)
rpc("\x00\x00\x01", 'portal_print', "end tx pwr set")
def setTXPowerHi():
rpc("\x00\x00\x01", 'portal_print', "Set tx pwr high")
poke(0x22A0, 159)
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A1, 255)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A2, 111)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', memval)
rpc("\x00\x00\x01", 'portal_print', "end tx pwr set")
def checkTXPower():
#return txPwr
#Function can only one value so we remote print values to portal
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'portal_print', memval)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'portal_print', memval)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', memval)
#return memval
#return peek(0x22A0)
#return peek(0x22A1)
#return peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', "end tx pwr check")
Code also attached as file.
"""
This script was a learning exercise for myself to get up to speed with Snappy.
I previsouly used Bascom AVR.
This was geared to convert an existing project over.
Basically a remote RFID Reader.
The functions I needed to learn was
-Pin monitoring
-Pin manipulation
-Serial processing
-peek/poke registers
-RPC's
The information was gathered from the forums and supplied docs/demos.
If I had this it would have saved me a few hours of hunting around.
So if you think like me, it should be most useful :)
Software: Portal 2.2.25
Hardware: ZIC2410 EVB3
http://forums.synapse-wireless.com/showthread.php?t=446&highlight=zic2410
We started with bit 0 of port 0 as IO 0, bit 1 of port 0 as IO 1, etc. So the resulting IO numbering is simply.
0 = P0.0 uart
1 = P0.1 uart
2 = P0.2
3 = P0.3
4 = P0.4
5 = P0.5
6 = P0.6
7 = P0.7
8 = P1.0
9 = P1.1
10 = P1.2
11 = P1.3
12 = P1.4
13 = P1.5
14 = P1.6
15 = P1.7
16 = P3.0
17 = P3.1
18 = P3.2
19 = P3.3
20 = P3.4
21 = P3.5
22 = P3.6
23 = P3.7
"""
#Rather than having to read multiple files, required definitions have copied over to this script.
#from synapse.evalBase import *
#from hexSupport.py import *
#from switchboard import *
#ZIC2410EVB3.py
#define constants
DS_NULL = 0
DS_UART0 = 1
DS_UART1 = 2
DS_TRANSPARENT = 3
DS_STDIO = 4
DS_CLI = 5
DS_PACKET_SERIAL = 6
LED1 = 3 # P0.3
LED2 = 2 # P0.2
LED3 = 1 # P0.1
LED4 = 16 # P3.0
SW1 = 11 # P1.3
SW2 = 12 # P1.4
SW3 = 18 # P3.2
SW4 = 19 # P3.3
#Define global Variables
# Track the current state of the LED, so we can toggle it
ledState = False
serial_in_data = ""
@setHook(HOOK_STARTUP)
def startupEvent():
"""Executed when the device boots up, this initializes the hardware and begins monitoring the button."""
# On an EVB3, LED4 is on the same pin as UART0 RX
# Shut down UART0 so we can control the pin manually
initUart(0,0)
# Initialize LED pins as outputs
setPinDir(LED1, True)
setPinDir(LED2, True)
setPinDir(LED3, True)
setPinDir(LED4, True)
#Turn them all off
writePin(LED1, False)
writePin(LED2, False)
writePin(LED3, False)
writePin(LED4, False)
setPinDir(SW1, False)
monitorPin(SW1, True)
#Default is check every 100ms. 2 = 10ms
setRate(2)
#Enable the uart , shortform, assume, 8,n,1 >
initUart(1, 19200)
#Must use character mode as no /r (carrige return) is sent by the device
#If character mode is not used and no /r is recieved, the buffer will just fill up until it overflows.
stdinMode(1, False) # Char mode, no echo
#Connect it to an output, otherwise print command won't go anywhere
crossConnect(DS_UART1, DS_STDIO)
#Was doing some network testing and didn't want to have to walk too far to loose the signal.
#Built in txPwr() not yet supported for the zic2410
setTXPowerLo()
@setHook(HOOK_GPIN)
#called on press and release of buttons
def buttonEvent(pin, isSet):
"""This event handler runs when the select switch (button) is pressed"""
global ledState, serial_in_data
# Take action on "press" of the button, when 'isSet' is False
if not isSet:
ledState = not ledState # Toggle the LED state variable
writePin(LED1, ledState) # ...and write that state to the hardware.
print "Button pressed"
print "\x41\x42\x43" #Prints hex eg hex41 = decmil 65 = A (uppercase A)
rpc("\x00\x00\x01", 'portal_print', "123")
#Calls a function on a remote node. In this case the portal.
#This function prints the supplied argument in the event log
#Handy for debugging when you are using the local UART for something
print serial_in_data
#needs to be reset somewhere, otherwise it will keep accunmlating
serial_in_data = ""
#time for some serial testing
#stdin line-buffer limit (40), from gps sample
#tested to 126? v2.2.25
@setHook(HOOK_STDIN)
def stdinEvent(serial_in_buffer):
global serial_in_data
print "rcvd:" , serial_in_buffer
print " rcvd chr ascii value:" , ord(serial_in_buffer)
print " rcvd chr hex value:" ,
printHex(ord(serial_in_buffer))
#if print hex is on same line as another print command then a none will be printed after the hex value
print ''
#or print '\r' ,
#note use of double == in conditional
if serial_in_buffer == '\x15':
print " hex check match"
buf_len = len(serial_in_buffer)
print " buffer len:" , len(serial_in_buffer) #buf_len
counter_1 = 0
#Read each character from the buffer into our variable
while(counter_1 < buf_len):
ser_char = serial_in_buffer[counter_1]
counter_1 += 1 #increment counter, shortform otherwise x = x + 1
serial_in_data += ser_char
#print "global ser:", serial_in_data
#serial_in_data = "serial rcvd"
#print serial_in_data
"""
if data == '?':
print "You want help?"
else:
print data
print "\r\n>",
"""
@setHook(HOOK_1S)
def timer1sEvent(currentS):
writePin(LED3, False)
pulsePin(LED4, 150, True)
#another way to check the pin state
if readPin(SW2) == False:
pulsePin(LED2, 150, True)
if readPin(SW3) == False:
writePin(LED3, True) #stays on until the timer event fires again.
#Functions
def hexNibble(nibble):
'''Convert a numeric nibble 0x0-0xF to its ASCII string representation "0"-"F"'''
hexStr = "0123456789ABCDEF"
return hexStr[nibble & 0xF]
def printHex(byte):
'''print a byte in hex - input is an integer, not a string'''
print hexNibble(byte >> 4),
print hexNibble(byte), # no trailing CR/LF
def dumpHex(str):
'''dump a string of bytes in hex'''
count = len(str)
index = 0
while index < count:
printHex(ord(str[index]))
index += 1
if (index & 15) == 0:
else:
print ' ',
'''
#pg 87, radio power level , ZIC2410.pdf
TX Output Power Level (dBm) TXPA0(0xA0) TXPA1(0xA1) TXPA2(0xA2)
8 10011111 11111111 01101111 159 , 255 , 111
-20 00010010 11100010 01101110 18 , 226 , 110
'''
def setTXPowerLo():
rpc("\x00\x00\x01", 'portal_print', "Set tx pwr low")
poke(0x22A0, 18)
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A1, 226)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A2, 110)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', memval)
rpc("\x00\x00\x01", 'portal_print', "end tx pwr set")
def setTXPowerHi():
rpc("\x00\x00\x01", 'portal_print', "Set tx pwr high")
poke(0x22A0, 159)
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A1, 255)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'portal_print', memval)
poke(0x22A2, 111)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', memval)
rpc("\x00\x00\x01", 'portal_print', "end tx pwr set")
def checkTXPower():
#return txPwr
#Function can only one value so we remote print values to portal
memval = peek(0x22A0)
rpc("\x00\x00\x01", 'portal_print', memval)
memval = peek(0x22A1)
rpc("\x00\x00\x01", 'portal_print', memval)
memval = peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', memval)
#return memval
#return peek(0x22A0)
#return peek(0x22A1)
#return peek(0x22A2)
rpc("\x00\x00\x01", 'portal_print', "end tx pwr check")