-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo_p2gw_filerw.spin2
157 lines (127 loc) · 6.05 KB
/
demo_p2gw_filerw.spin2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'' =================================================================================================
''
'' File....... demo_p2gw_filerw.spin2
'' Purpose.... Demonstrate sending an email message
'' Authors.... Stephen M Moraco
'' -- Copyright (c) 2022 Iron Sheep Productions, LLC
'' -- see below for terms of use
'' E-mail..... stephen@ironsheep.biz
'' Started.... Jan 2022
'' Updated.... 14 Jan 2022
''
'' =================================================================================================
CON { timing }
CLK_FREQ = 200_000_000 ' system freq as a constant
_clkfreq = CLK_FREQ ' set system clock
CON { fixed io pins }
RX1 = 63 { I } ' programming / debug
TX1 = 62 { O }
SF_CS = 61 { O } ' serial flash
SF_SCK = 60 { O }
SF_SDO = 59 { O }
SF_SDI = 58 { I }
CON { RPi Gateway io pins }
RX_GW = 25 { I } ' Raspberry Pi (RPi) Gateway
TX_GW = 24 { O }
GW_BAUDRATE = 624_000 ' 624kb/s - allow P2 rx to keep up!
OBJ { Objects Used by this Object }
IoT_GW : "isp_rpi_iot_gw" ' serial I/O
DAT { our email and hardware ID strings }
p2HardwareID byte "P2 Edge - GWDemo2",0
filename byte "p2TmpFile",0
filename2 byte "p2TmpFile2",0
ourStringVar byte "Some random longish text",0 ' no embedded newlines (but who cares?)
ourVariable long $f00d
CON
RX_BFFR_LEN = 80
VAR { demo buffers }
byte rdBuffer[RX_BFFR_LEN+1]
PUB main() | opStatus, fileId, tmpValue, pRetString, bCompStatus, lenResult, nIdx
'' DEMO exercise various collection reads and writes
IoT_GW.startx(RX_GW, TX_GW, GW_BAUDRATE, IoT_GW.PU_15K)
' (one time) tell the RPi about how to identify this hardware
IoT_GW.identify(@p2HardwareID)
' Let's connect to our file
opStatus, fileId := IoT_GW.fileAccess(IoT_GW.EFI_TMP, IoT_GW.FM_WRITE_CREATE, @filename)
if opStatus == IoT_GW.OP_SUCCESS
' now write a named LONG value to it
opStatus := IoT_GW.fileWriteLong(fileId, string("ourVariable"), ourVariable)
if opStatus == IoT_GW.OP_SUCCESS
' and then read the LONG back
opStatus, tmpValue := IoT_GW.fileReadLong(fileId, string("ourVariable"))
if opStatus == IoT_GW.OP_SUCCESS
' good read, compare results
if ourVariable == tmpValue
' good read
debug("* main: GOOD match!")
else
' failed read
debug("* main: READ data BAD match!")
else
' FAILED read
debug("* main: READ Error!")
else
' FAILED write
debug("* main: WRITE Error!")
else
' FAILED open
debug("* main: OPEN Error!")
' Let's connect to our file
opStatus, fileId := IoT_GW.fileAccess(IoT_GW.EFI_TMP, IoT_GW.FM_WRITE_CREATE, @filename2)
if opStatus == IoT_GW.OP_SUCCESS
' now write a named STRING value to it
opStatus := IoT_GW.fileWriteString(fileId, string("ourStringVar"), @ourStringVar)
if opStatus == IoT_GW.OP_SUCCESS
' and then read the STRING back
opStatus, pRetString := IoT_GW.fileReadString(fileId, string("ourStringVar"), @rdBuffer, RX_BFFR_LEN)
if opStatus == IoT_GW.OP_SUCCESS
' good read, compare results
lenResult := strsize(pRetString)
if strsize(@rdBuffer) == lenResult
' good read
debug("* main: GOOD length!")
bCompStatus := True
repeat nIdx from 0 to lenResult - 1
if byte[@rdBuffer][nIdx] <> byte[pRetString][nIdx]
bCompStatus := False
quit
if bCompStatus == True
debug("* main: GOOD string compare!")
else
debug("* main: BAD string compare FAIL!")
else
' failed read
debug("* main: READ data Length mismatch!")
else
' FAILED read
debug("* main: READ Error!")
else
' FAILED write
debug("* main: WRITE Error!")
else
' FAILED open
debug("* main: OPEN Error!")
' terminate communication with our gateway RPi
IoT_GW.stop()
CON { license }
{{
-------------------------------------------------------------------------------------------------
MIT License
Copyright (c) 2022 Iron Sheep Productions, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 OR COPYRIGHT HOLDERS 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.
=================================================================================================
}}