1+ '' =================================================================================================
2+ ''
3+ '' File....... jm_formatted_strings_test.spin2
4+ '' Purpose....
5+ '' Author..... Jon "JonnyMac" McPhalen
6+ '' Copyright (c) 2020 Jon McPhalen
7+ '' -- see below for terms of use
8+ 9+ '' Started....
10+ '' Updated.... 19 AUG 2020
11+ ''
12+ '' =================================================================================================
13+
14+
15+ con { timing }
16+
17+ CLK_FREQ = 200_000_000 ' system freq as a constant
18+ MS_001 = CLK_FREQ / 1_000 ' ticks in 1ms
19+ US_001 = CLK_FREQ / 1_000_000 ' ticks in 1us
20+
21+ BR_TERM = 115_200 ' terminal baud rate
22+
23+ _clkfreq = CLK_FREQ ' set system clock
24+
25+
26+ con { fixed io pins }
27+
28+ RX1 = 63 { I } ' programming / debug
29+ TX1 = 62 { O }
30+
31+ FS_CS = 61 { O } ' flash storage
32+ FS_SCLK = 60 { O }
33+ FS_MOSI = 59 { O }
34+ FS_MISO = 58 { I }
35+
36+ SD_SCLK = 61 { O } ' usd card storage
37+ SD_CS = 60 { O }
38+ SD_MOSI = 59 { O }
39+ SD_MISO = 58 { I }
40+
41+ SDA1 = 57 { IO } ' i2c (optional)
42+ SCL1 = 56 { IO }
43+
44+
45+ con
46+
47+ BUF_SIZE = 32
48+
49+
50+ obj
51+
52+ ' main ' * master Spin cog
53+ term : "jm_fullduplexserial" ' * serial IO for terminal
54+
55+
56+ var
57+
58+ byte buffer[BUF_SIZE]
59+
60+
61+ dat
62+
63+ Device byte "P2X8C4M64P", 0
64+
65+
66+ pub main() | x, y
67+
68+ setup()
69+
70+ wait_for_terminal(true)
71+
72+ term.fstr1(string("%s Formatted Strings Demo\r"), @Device)
73+ term.fstr1(string("%033c\r\r"), "-")
74+
75+ term.fstr0(string("Please enter your name: "))
76+ get_str(BUF_SIZE-2)
77+ term.fstr1(string("\r\rHello, %s, let me show you some \rformatted strings...\r\r"), @buffer)
78+ waitms(1000)
79+
80+ ' use \nnn for arbitrary character
81+ ' -- \176 is the degrees symbol in PST
82+
83+ x := 23
84+ term.fstr2(string("%d\176C --> %d\176F\r\r"), x, x * 9 / 5 + 32)
85+
86+ ' negative width fields are left justified
87+ ' positive width fields are right justified
88+
89+ x := 123
90+ repeat 6
91+ term.fstr2(string("%-10d %13.3f\r"), x, x)
92+ x *= 10
93+ waitms(5)
94+
95+ term.txn(13, 2)
96+
97+ ' fixed field widths with leading spaces
98+
99+ term.fstr0(string("DEC HEX OCT QRT BIN\r"))
100+ term.fstr0(string("--- --- --- --- ----\r"))
101+
102+ repeat x from 0 to 15
103+ term.fstr5(string("%3d %3x %3o %3q %4b\r"), x, x, x, x, x)
104+ waitms(5)
105+
106+ term.txn(13, 2)
107+
108+ ' fixed field width with leading 0s
109+
110+ term.fstr0(string("DEC HEX OCT QRT BIN\r"))
111+ term.fstr0(string("--- --- --- --- ----\r"))
112+
113+ repeat x from 0 to 15
114+ term.fstr5(string("%0.3d %0.3x %0.3o %0.3q %0.4b\r"), x, x, x, x, x)
115+ waitms(5)
116+
117+ repeat
118+ waitct(0)
119+
120+
121+ pub get_str(maxlen) : len | k
122+
123+ bytefill(@buffer, 0, BUF_SIZE) ' clear input buffer
124+
125+ term.rxflush() ' clear trash from terminal
126+
127+ repeat
128+ k := term.rx() ' wait for a character
129+ case k
130+ 31..127 : ' if valid
131+ if (len < maxlen) ' and room
132+ buffer[len++] := k ' add to buffer
133+
134+ term.BKSP :
135+ if (len > 0) ' if character(s) in buffer
136+ buffer[--len] := 0 ' backup and erase last
137+
138+ term.CR :
139+ buffer[len] := 0 ' terminate string
140+ return ' and return to caller
141+
142+
143+ pub wait_for_terminal(clear)
144+
145+ term.rxflush()
146+ term.rx() ' wait for keypress
147+ if (clear)
148+ term.tx(term.CLS)
149+
150+
151+ pub setup()
152+
153+ term.start(RX1, TX1, %0000, BR_TERM) ' start terminal serial
154+
155+
156+ con { license }
157+
158+ {{
159+
160+ MIT License
161+
162+ Permission is hereby granted, free of charge, to any person obtaining a copy
163+ of this software and associated documentation files (the "Software"), to deal
164+ in the Software without restriction, including without limitation the rights
165+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
166+ copies of the Software, and to permit persons to whom the Software is
167+ furnished to do so, subject to the following conditions:
168+
169+ The above copyright notice and this permission notice shall be included in all
170+ copies or substantial portions of the Software.
171+
172+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
175+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
177+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
178+ SOFTWARE.
179+
180+ }}
0 commit comments