### Author Topic: SB π  (Read 7541 times)

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #120 on: March 31, 2019, 03:41:26 PM »
Here is the Fren example.

Code: Script BASIC
1. ' Fern
2.
3. IMPORT bbc.bas
4.
5. BBC::OPEN "ScriptBasic BBC fern - Raspbery Pi"
6. t1 = BBC::TIME()
7. BBC::MODE 31
8. BBC::ORIGIN 200, 100
9. BBC::OFF
10. BBC::GCOL 0, 10
11. x = 0
12. y = 0
13. FOR i = 1 TO 80000
14.   r = BBC::RND(1)
15.   IF r <= 0.1 THEN
16.     a = 0
17.     b = 0
18.     c = 0
19.     d = 0.16
20.     e = 0
21.     f = 0
22.   END IF
23.   IF r > 0.1 AND r <= 0.86 THEN
24.     a = .85
25.     b = .04
26.     c = -.04
27.     d = .85
28.     e = 0
29.     f = 1.6
30.   END IF
31.   IF r > 0.86 AND r <= 0.93 THEN
32.     a = .2
33.     b = -.26
34.     c = .23
35.     d = .22
36.     e = 0
37.     f = 1.6
38.   END IF
39.   IF r > 0.93 THEN
40.     a = -.15
41.     b = .28
42.     c = .26
43.     d = .24
44.     e = 0
45.     f = .44
46.   END IF
47.   newx = a * x + b * y + e
48.   newy = c * x + d * y + f
49.   x = newx
50.   y = newy
51.   BBC::MOVE 600 + 96 * x, 32 + 96 * y
52.   BBC::DRAW 600 + 96 * x, 32 + 96 * y
53. NEXT i
54. t2 = BBC::TIME()
55. t3 = (t2-t1)/1000
56. BBC::VDUSTR "Time: " & FORMAT("%.4f",t3) & " seconds"
57. BBC::WAITKEY
58. BBC::CLOSE
59.
« Last Edit: April 06, 2019, 03:29:24 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #121 on: March 31, 2019, 03:58:48 PM »
Finally the graph example. Hopefully other BBC BASIC fans will contribute examples as well.

Code: Script BASIC
1. ' Graph Demo
2.
3. IMPORT bbc.bas
4.
5. CONST SDLK_ESCAPE = 27
6.
7. BBC::OPEN "ScriptBasic BBC graphdemo - Raspberry Pi"
8. t1 = BBC::TIME()
9. BBC::MODE 31
10. BBC::ORIGIN 800, 600
11. xlow = -10
12. xhigh = 10
13. ylow = -10
14. yhigh = 10
15. depth = 10
16. xscale = 30
17. yscale = 12
18. c = -4000
19.
20. FOR x = xlow TO xhigh
21.   BBC::MOVE xscale * (x + ylow), yscale * (ylow - x) + c / (x * x + ylow * ylow + depth)
22.   FOR y = ylow TO yhigh
23.     BBC::DRAW xscale * (x + y), yscale * (y - x) + c / (x * x + y * y + depth)
24.   NEXT
25. NEXT
26. FOR y = ylow TO yhigh
27.   BBC::MOVE xscale * (xlow + y), yscale * (y - xlow) + c / (xlow * xlow + y * y + depth)
28.   FOR x = xlow TO xhigh
29.     BBC::DRAW xscale * (x + y), yscale * (y - x) + c / (x * x + y * y + depth)
30.   NEXT
31. NEXT
32. t2 = BBC::TIME()
33. t3 = (t2 - t1) / 1000
34. BBC::OFF
35. BBC::VDUSTR "Time: " & FORMAT("%.4f", t3) & " seconds."
36. WHILE BBC::GETKEY(1) <> SDLK_ESCAPE
37. WEND
38. BBC::CLOSE
39.
« Last Edit: April 06, 2019, 03:29:41 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #122 on: March 31, 2019, 05:21:04 PM »
For those not into the retro BBC BASIC graphics primitives, I ported the SDL_gfx extension module to the Raspberry Pi.

Code: Script BASIC
1. ' ScriptBasic GFX - Alpha Circles
2.
3. IMPORT gfx.bas
4.
5. scrn = gfx::Window(640, 480, "ScriptBasic GFX - Alpha Circles - Raspberry Pi")
6. ' Random Value Arrays
7. RANDOMIZE(gfx::Time())
8. FOR i = 0 TO 512
9.   rx[i] = RND() % 640
10.   ry[i] = 60 + RND() % 480 - 80
11.   rz[i] = RND() % 64
12.   rr[i] = RND() AND  255
13.   rg[i] = RND() AND  255
14.   rb[i] = RND() AND  255
15.   af = rx[i] / 640
16.   ra[i] = INT(255 * af)
17. NEXT
18.
19. ts = gfx::Time()
20. FOR i = 0 TO 512
21.   gfx::filledCircleRGBA scrn, rx[i], ry[i], rz[i], rr[i], rg[i], rb[i], ra[i]
22. NEXT
23. te = gfx::Time()
24. gfx::stringColor scrn, 20, 15, "Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
25. gfx::Update
26. WHILE gfx::KeyName(1) <> "+escape"
27. WEND
28. gfx::Close
29.
« Last Edit: April 06, 2019, 03:29:55 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #123 on: March 31, 2019, 05:32:48 PM »
This shows some of the SDL_gfx graphic primitives.

Code: Script BASIC
1. ' SDL_gfx Demo
2.
3. IMPORT gfx.bas
4.
5. screen = gfx::Window(700, 600, "ScriptBasic GFX Demo - Raspberry Pi")
6. gfx::pixelRGBA(screen, 10, 15, 255, 255, 255, 255)
7. gfx::lineRGBA(screen, 20, 10, 70, 90, 255, 0, 0, 255)
8. gfx::trigonRGBA(screen, 500, 50, 550, 200, 600, 150, 0, 255, 255, 255)
9. gfx::filledTrigonRGBA(screen, 200, 200, 300, 50, 400, 200, 0, 0, 255, 255)
10. gfx::rectangleRGBA(screen, 10, 300, 100, 380, 0, 255, 0, 255)
11. gfx::boxRGBA(screen, 210, 76, 325, 300, 255, 0, 0, 150)
12. gfx::ellipseRGBA(screen, 600, 400, 50, 90, 255, 255, 0, 200)
13. gfx::filledEllipseRGBA(screen, 600, 400, 25, 150, 0, 255, 0, 255)
14. x[0] = 350
15. x[1] = 275
16. x[2] = 300
17. x[3] = 325
18. x[4] = 350
19. x[5] = 400
20. y[0] = 325
21. y[1] = 325
22. y[2] = 390
23. y[3] = 390
24. y[4] = 375
25. gfx::polygonRGBA(screen, x, y, 6, 255, 255, 255, 155)
26. s[0] = 400
27. s[1] = 450
28. s[2] = 450
29. s[3] = 425
30. s[4] = 300
31. t[0] = 400
32. t[1] = 410
33. t[2] = 450
34. t[3] = 425
35. t[4] = 500
36. gfx::filledPolygonRGBA(screen, s, t, 5, 255, 0, 255, 155)
37. gfx::Update
38. WHILE gfx::KeyName(1) <> "+escape"
39. WEND
40. gfx::Close
41.
« Last Edit: April 06, 2019, 03:30:11 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #124 on: March 31, 2019, 06:53:29 PM »
I'm pleased  to announce a release of ScriptBasic for the Raspberry Pi. You can do a traditional install with a .deb file or as an AppImage.

DEB Install:

Code: Bash
1. sudo apt install ./scriptbasic-2.1-linux-armhf.deb

DEB Uninstall:

Code: Bash
1. sudo apt remove scriptbasic-2.1-linux-armhf

To run a script use scriba <script nane> when using the DEB Install.

To use the AppImage version ScriptBasic, unzip the file in the /usr/local/bin (or a directory in the system path) and use sb <script_name> to run your scripts. To remove ScriptBasic, remove the sb file.

ScriptBasic User and Developer Guides

Most of the ScriptBasic development activity is happening on the All BASIC Forum.

Language Features
• Variant style variables (no definition or declaration  needed)
• Indexed, associative arrays (or combo of both) no practical limits
• Muilt-threaded - asynchronous / synchronous execution
• Unlimited seamless expansion via the extension API
• Embeddable API
• Footprint less than 800 KB
• Runs on everything with a single source tree (written in ANSI C)
• Application proxy web server that runs ScriptBasic code
• Debugger with single step execution, break points and more

Extension Modules
• libcurl + SSL
• MySQL
• SQLite
• ODBC
• json
• XML
• regex
• CGI
• zlib
• IUP - cross platform portable GUI
• SDL_gfx graphics primatives with alpha channel support
• BBC BASIC graphic library
• more ..

Free - Open Source - MIT License

Attached are the extension modules and dependencies need for the bbc and gfx extension modules. These extension only work with the DEB install at this time.

One must install the SDL 1.2 and SDL_gfx 1.2 runtime libraries.

sudo apt-get install libsdl1.2debian-1.2.15+dfsg1-4+rpt2
sudo apt-get install libsdl-gfx1.2-5-2.0.25-5

BBC
1. unzip the bbc.zip file in a temp directory
2. sudo cp bbc.so /usr/local/lib/scriba/
3. sudo cp bbc.bas /usr/local/include/scriba/
4. sudo cp libbbc.so /usr/lib/

SDL_gfx
1. unzip the gfx.zip file in a temp directory
2. sudo cp gfx.so /usr/local/lib/scriba/
3. sudo cp gfx.bas /usr/local/include/scriba/

Use the ScriptBasic example code posted in this thread to test your installation.
« Last Edit: April 06, 2019, 03:30:23 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #125 on: April 01, 2019, 02:37:21 PM »
I just ordered a Raspberry Pi Sense HAT board. I want to get a GPIO extension module going with ScriptBasic The Sense HAT emulator that comes with the RPi uses Python as its scripting langauage. It would be cool to be able to script the emulator with SB.

Essentials Sense HAT
« Last Edit: April 06, 2019, 03:30:39 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #126 on: April 01, 2019, 03:22:12 PM »
I was able to find the xml syntax definition file for gEdit that BaCon uses. Since BaCon is so close to SB syntax, using it as a template should make creating a SB version easy.

Code: XML
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!--
3.
4. Author: Peter van Eerten <peter@basic-converter.org>
5. Copyright (C) 2010 Peter van Eerten <peter@basic-converter.org>
6.
7. Language: BaCon (BAsic CONverter)
8. Last Change: December 22, 2018
9.
10. This library is free software; you can redistribute it and/or
11. modify it under the terms of the GNU Library General Public
13. version 2 of the License, or (at your option) any later version.
14.
15. This library is distributed in the hope that it will be useful,
16. but WITHOUT ANY WARRANTY; without even the implied warranty of
17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18. Library General Public License for more details.
19.
20. You should have received a copy of the GNU Library General Public
21. License along with this library; if not, write to the
22. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23. Boston, MA 02111-1307, USA.
24.
25.    =========================
26.    INSTALLATION INSTRUCTIONS
27.    =========================
28.
29. (1) Create language directory for GtkSourceView:
30.
31.    mkdir -p ~/.local/share/gtksourceview-2.0/language-specs/
32.
33. (2) Copy this file to the new directory:
34.
35.    cp bacon.lang ~/.local/share/gtksourceview-2.0/language-specs/
36.
37.
38. Now start gedit, Anjuta, Scribes etc and the BaCon sourcecode has
39.   syntax highlighting!
40. -->
41.
42. <language id="bacon" _name="BaCon" version="2.0" _section="Sources">
43.
45.     <property name="mimetypes">text/x-bac</property>
46.     <property name="globs">*.bac</property>
47.     <property name="line-comment-start">'</property>
49.
50. <styles>
51.     <style id="comment"             _name="Comment"             map-to="def:comment"/>
52.     <style id="shebang"             _name="Shebang"             map-to="def:shebang"/>
53.     <style id="function"            _name="Function"            map-to="def:function"/>
54.     <style id="string"              _name="String"              map-to="def:string"/>
55.     <style id="keyword"             _name="Keyword"             map-to="def:keyword"/>
56.     <style id="special-constant"    _name="Special constant"    map-to="def:special-constant"/>
57.     <style id="boolean"             _name="Boolean value"       map-to="def:boolean"/>
58.     <style id="type"                _name="Data Type"           map-to="def:type"/>
59.     <style id="operator"            _name="Operator"            map-to="def:operator"/>
60.     <style id="wrapperfunc"         _name="Wrapper"             map-to="def:preprocessor"/>
61. </styles>
62.
63. <!-- <default-regex-options case-sensitive="false"/> -->
64.
65. <definitions>
66.
67.     <context id="line-comment" style-ref="comment" end-at-line-end="true">
68.         <start>'</start>
69.             <include>
70.                 <context ref="def:in-line-comment"/>
71.             </include>
72.     </context>
73.
74.     <context id="old-style-comment" style-ref="comment" end-at-line-end="true">
75.         <start>REM</start>
76.             <include>
77.                 <context ref="def:in-line-comment"/>
78.             </include>
79.     </context>
80.
81.     <context id="multiline-comment" style-ref="comment">
82.         <start>\/\*</start>
83.         <end>\*\/</end>
84.             <include>
85.                 <context ref="def:in-line-comment"/>
86.             </include>
87.     </context>
88.
89.     <context id="string" style-ref="string">
90.       <start>"</start>
91.       <end>"</end>
92.       <include>
93.         <!-- highlight escaped chars -->
94.         <context style-ref="def:special-char">
95.             <match>\\[\$`"\\]</match>
96.         </context>
97.         <!-- skip backslash where it's not an escaping char -->
98.         <context>
99.             <match>\\</match>
100.         </context>
101.         <context ref="def:line-continue"/>
102.       </include>
103.     </context>
104.
105.     <context id="function" style-ref="function">
106.         <keyword>ABS</keyword>
107.         <keyword>ACOS</keyword>
109.         <keyword>AMOUNT</keyword>
110.         <keyword>AND</keyword>
111.         <keyword>ASC</keyword>
112.         <keyword>ASIN</keyword>
113.         <keyword>ATN</keyword>
114.         <keyword>ATN2</keyword>
115.         <keyword>BETWEEN</keyword>
116.         <keyword>BIT</keyword>
118.         <keyword>BYTELEN</keyword>
119.         <keyword>CEIL</keyword>
120.         <keyword>CMDLINE</keyword>
121.         <keyword>COLUMNS</keyword>
122.         <keyword>COS</keyword>
123.         <keyword>COUNT</keyword>
124.         <keyword>DAY</keyword>
125.         <keyword>DEC</keyword>
126.         <keyword>DEG</keyword>
127.         <keyword>ENDFILE</keyword>
128.         <keyword>EQUAL</keyword>
129.         <keyword>ERROR</keyword>
130.         <keyword>EVAL</keyword>
131.         <keyword>EVEN</keyword>
132.         <keyword>EXCL</keyword>
133.         <keyword>EXP</keyword>
134.         <keyword>FILEEXISTS</keyword>
135.         <keyword>FILELEN</keyword>
136.         <keyword>FILETIME</keyword>
137.         <keyword>FILETYPE</keyword>
138.         <keyword>FLOOR</keyword>
139.         <keyword>FORK</keyword>
140.         <keyword>FP</keyword>
141.         <keyword>GETKEY</keyword>
142.         <keyword>GETX</keyword>
143.         <keyword>GETY</keyword>
144.         <keyword>HASH</keyword>
145.         <keyword>HOUR</keyword>
146.         <keyword>IIF</keyword>
147.         <keyword>INSTR</keyword>
148.         <keyword>INSTRREV</keyword>
149.         <keyword>INT</keyword>
150.         <keyword>ISASCII</keyword>
151.         <keyword>ISFALSE</keyword>
152.         <keyword>ISKEY</keyword>
153.         <keyword>ISTOKEN</keyword>
154.         <keyword>ISTRUE</keyword>
155.         <keyword>LEN</keyword>
156.         <keyword>LOG</keyword>
157.         <keyword>MAX</keyword>
158.         <keyword>MAXNUM</keyword>
159.         <keyword>MEMCHECK</keyword>
160.         <keyword>MEMORY</keyword>
161.         <keyword>MEMTELL</keyword>
162.         <keyword>MIN</keyword>
163.         <keyword>MINUTE</keyword>
164.         <keyword>MOD</keyword>
165.         <keyword>MONTH</keyword>
166.         <keyword>MYPID</keyword>
167.         <keyword>NOT</keyword>
168.         <keyword>NOW</keyword>
169.         <keyword>NRKEYS</keyword>
170.         <keyword>ODD</keyword>
171.         <keyword>OR</keyword>
172.         <keyword>PEEK</keyword>
173.         <keyword>POW</keyword>
175.         <keyword>RANDOM</keyword>
176.         <keyword>REGEX</keyword>
177.         <keyword>REAP</keyword>
178.         <keyword>RND</keyword>
179.         <keyword>ROL</keyword>
180.         <keyword>ROR</keyword>
181.         <keyword>ROUND</keyword>
182.         <keyword>ROWS</keyword>
183.         <keyword>RUN</keyword>
184.         <keyword>SEARCH</keyword>
185.         <keyword>SECOND</keyword>
186.         <keyword>SGN</keyword>
187.         <keyword>SIN</keyword>
188.         <keyword>SIZEOF</keyword>
189.         <keyword>SQR</keyword>
190.         <keyword>SUM</keyword>
191.         <keyword>SUMF</keyword>
192.         <keyword>TALLY</keyword>
193.         <keyword>TAN</keyword>
194.         <keyword>TELL</keyword>
195.         <keyword>TIMER</keyword>
196.         <keyword>TIMEVALUE</keyword>
197.         <keyword>UCS</keyword>
198.         <keyword>ULEN</keyword>
199.         <keyword>VAL</keyword>
200.         <keyword>WAIT</keyword>
201.         <keyword>WEEK</keyword>
202.         <keyword>YEAR</keyword>
203.     </context>
204.
205.     <context id="keywords" style-ref="keyword">
206.         <keyword>ALARM</keyword>
207.         <keyword>ALIAS</keyword>
208.         <keyword>APPENDING</keyword>
209.         <keyword>APPEND</keyword>
210.         <keyword>ARRAY</keyword>
211.         <keyword>AS</keyword>
212.         <keyword>ASSOC</keyword>
213.         <keyword>BACONLIB</keyword>
214.         <keyword>BACK</keyword>
215.         <keyword>BAPPEND</keyword>
216.         <keyword>BASE</keyword>
217.         <keyword>BG</keyword>
218.         <keyword>BLACK</keyword>
219.         <keyword>BLUE</keyword>
220.         <keyword>BREAK</keyword>
222.         <keyword>BSAVE</keyword>
223.         <keyword>BY</keyword>
224.         <keyword>CALL</keyword>
225.         <keyword>CASE</keyword>
226.         <keyword>CATCH</keyword>
227.         <keyword>CHANGEDIR</keyword>
228.         <keyword>CHUNK</keyword>
229.         <keyword>CLEAR</keyword>
230.         <keyword>CLOSE</keyword>
231.         <keyword>CMODE</keyword>
232.         <keyword>COLLAPSE</keyword>
233.         <keyword>COLOR</keyword>
234.         <keyword>CONST</keyword>
235.         <keyword>COMPARE</keyword>
236.         <keyword>COMPILER</keyword>
237.         <keyword>CONTINUE</keyword>
238.         <keyword>COPY</keyword>
239.         <keyword>CURRENT</keyword>
240.         <keyword>CURSOR</keyword>
241.         <keyword>CYAN</keyword>
242.         <keyword>DATA</keyword>
243.         <keyword>DECLARE</keyword>
244.         <keyword>DECR</keyword>
245.         <keyword>DEFAULT</keyword>
246.         <keyword>DEF</keyword>
247.         <keyword>DELETE</keyword>
248.         <keyword>DELIM</keyword>
249.         <keyword>DEVICE</keyword>
250.         <keyword>DIRECTORY</keyword>
251.         <keyword>DO</keyword>
252.         <keyword>DONE</keyword>
253.         <keyword>DOTIMES</keyword>
254.         <keyword>DOWN</keyword>
255.         <keyword>DOWNTO</keyword>
256.         <keyword>DQ</keyword>
257.         <keyword>ELIF</keyword>
258.         <keyword>ELSE</keyword>
259.         <keyword>ENDFORK</keyword>
260.         <keyword>ENDFUNCTION</keyword>
261.         <keyword>ENDRECORD</keyword>
262.         <keyword>ENDIF</keyword>
263.         <keyword>END</keyword>
264.         <keyword>ENDSELECT</keyword>
265.         <keyword>ENDSUB</keyword>
266.         <keyword>ENDENUM</keyword>
267.         <keyword>ENDUSEC</keyword>
268.         <keyword>ENDUSEH</keyword>
269.         <keyword>ENDWITH</keyword>
270.         <keyword>ENUM</keyword>
271.         <keyword>EPRINT</keyword>
272.         <keyword>EQ</keyword>
273.         <keyword>EXIT</keyword>
274.         <keyword>EXPLICIT</keyword>
275.         <keyword>FG</keyword>
276.         <keyword>FI</keyword>
277.         <keyword>FILE</keyword>
278.         <keyword>FN</keyword>
279.         <keyword>FOR</keyword>
280.         <keyword>FORMAT</keyword>
281.         <keyword>FORWARD</keyword>
282.         <keyword>FRAMEWORK</keyword>
283.         <keyword>FREE</keyword>
284.         <keyword>FROM</keyword>
285.         <keyword>FUNCTION</keyword>
286.         <keyword>GE</keyword>
287.         <keyword>GETBYTE</keyword>
288.         <keyword>GETFILE</keyword>
289.         <keyword>GETLINE</keyword>
290.         <keyword>GLOBAL</keyword>
291.         <keyword>GOSUB</keyword>
292.         <keyword>GOTO</keyword>
293.         <keyword>GOTOXY</keyword>
294.         <keyword>GREEN</keyword>
295.         <keyword>GT</keyword>
296.         <keyword>IF</keyword>
297.         <keyword>IMODE</keyword>
298.         <keyword>IMPORT</keyword>
299.         <keyword>IN</keyword>
300.         <keyword>INCLUDE</keyword>
301.         <keyword>INCR</keyword>
302.         <keyword>INPUT</keyword>
303.         <keyword>INTENSE</keyword>
304.         <keyword>INTERNATIONAL</keyword>
305.         <keyword>INVERSE</keyword>
306.         <keyword>IS</keyword>
307.         <keyword>ISNOT</keyword>
308.         <keyword>JOIN</keyword>
309.         <keyword>LABEL</keyword>
310.         <keyword>LDFLAGS</keyword>
311.         <keyword>LE</keyword>
312.         <keyword>LET</keyword>
313.         <keyword>LIBRARY</keyword>
314.         <keyword>LMODE</keyword>
315.         <keyword>LOCAL</keyword>
316.         <keyword>LOOKUP</keyword>
317.         <keyword>LT</keyword>
318.         <keyword>MAP</keyword>
319.         <keyword>MAGENTA</keyword>
320.         <keyword>MAKEDIR</keyword>
321.         <keyword>MEMREWIND</keyword>
322.         <keyword>MEMSTREAM</keyword>
323.         <keyword>MEMTYPE</keyword>
324.         <keyword>MONITOR</keyword>
325.         <keyword>MULTICAST</keyword>
326.         <keyword>NE</keyword>
327.         <keyword>NETWORK</keyword>
328.         <keyword>NEXT</keyword>
329.         <keyword>NORMAL</keyword>
330.         <keyword>OFF</keyword>
331.         <keyword>OFFSET</keyword>
332.         <keyword>OMODE</keyword>
333.         <keyword>ON</keyword>
334.         <keyword>OPEN</keyword>
335.         <keyword>OPTION</keyword>
336.         <keyword>OPTIONS</keyword>
337.         <keyword>OTHER</keyword>
338.         <keyword>PARSE</keyword>
339.         <keyword>POKE</keyword>
340.         <keyword>PRAGMA</keyword>
341.         <keyword>PRINT</keyword>
342.         <keyword>PROPER</keyword>
343.         <keyword>PROTO</keyword>
344.         <keyword>PULL</keyword>
345.         <keyword>PUSH</keyword>
346.         <keyword>PUTBYTE</keyword>
347.         <keyword>PUTLINE</keyword>
348.         <keyword>QUOTED</keyword>
349.         <keyword>RE</keyword>
355.         <keyword>RECORD</keyword>
356.         <keyword>RECURSIVE</keyword>
357.         <keyword>RED</keyword>
358.         <keyword>REDIM</keyword>
359.         <keyword>RELATE</keyword>
360.         <keyword>RENAME</keyword>
361.         <keyword>REPEAT</keyword>
362.         <keyword>RESET</keyword>
363.         <keyword>RESIZE</keyword>
364.         <keyword>RESTORE</keyword>
365.         <keyword>RESUME</keyword>
366.         <keyword>RETURN</keyword>
367.         <keyword>REWIND</keyword>
368.         <keyword>SAVE</keyword>
369.         <keyword>SCROLL</keyword>
370.         <keyword>SCTP</keyword>
371.         <keyword>SEED</keyword>
372.         <keyword>SEEK</keyword>
373.         <keyword>SELECT</keyword>
374.         <keyword>SEND</keyword>
375.         <keyword>SERVER</keyword>
376.         <keyword>SETENVIRON</keyword>
377.         <keyword>SETSERIAL</keyword>
378.         <keyword>SIGNAL</keyword>
379.         <keyword>SIZE</keyword>
380.         <keyword>SLEEP</keyword>
381.         <keyword>SOCKET</keyword>
382.         <keyword>SORT</keyword>
383.         <keyword>SPEED</keyword>
384.         <keyword>SPLIT</keyword>
385.         <keyword>START</keyword>
386.         <keyword>STARTPOINT</keyword>
387.         <keyword>STATIC</keyword>
388.         <keyword>STEP</keyword>
389.         <keyword>STOP</keyword>
390.         <keyword>SUB</keyword>
391.         <keyword>SWAP</keyword>
392.         <keyword>SYSTEM</keyword>
393.         <keyword>TCP</keyword>
394.         <keyword>TEXTDOMAIN</keyword>
395.         <keyword>THEN</keyword>
396.         <keyword>TO</keyword>
397.         <keyword>TRACE</keyword>
398.         <keyword>TRAP</keyword>
399.         <keyword>TYPE</keyword>
400.         <keyword>UDP</keyword>
401.         <keyword>UNTIL</keyword>
402.         <keyword>UP</keyword>
403.         <keyword>USEC</keyword>
404.         <keyword>USEH</keyword>
405.         <keyword>UTF8</keyword>
406.         <keyword>VAR</keyword>
407.         <keyword>VARTYPE</keyword>
408.         <keyword>WEND</keyword>
409.         <keyword>WHENCE</keyword>
410.         <keyword>WHILE</keyword>
411.         <keyword>WHITE</keyword>
412.         <keyword>WITH</keyword>
413.         <keyword>WRITELN</keyword>
414.         <keyword>WRITING</keyword>
415.         <keyword>YELLOW</keyword>
416.     </context>
417.
418.     <context id="strfunction" style-ref="function">
419.         <match extended="true">
420.             \bCHOP\\$|\bCHR\\$|\bCONCAT\\$|\bCURDIR\\$|\bERR\\$|\bEXEC\\$|\bEXTRACT\\$|\bFILL\\$|\bGETENVIRON\\$|\bGETPEER\\$|\bHOSTNAME\\$|\bHEX\\$|\bHOST\\$
421.                 |\bIIF\\$|\bINTL\\$|\bLCASE\\$|\bLEFT\\$|\bMAX\\$|\bMID\\$|\bMIN\\$|\bMONTH\\$|\bNNTL\\$|\bOS\\$|\bREPLACE\\$|\bREVERSE\\$|\bRIGHT\\$|\bSPC\\$|\bSTR\\$
424.                 |\bDEL\\$|\bCUT\\$|\bALIGN\\$|\bUTF8\\$|\bPROPER\\$|\bROTATE\\$|\bLAST\\$|\bFIRST\\$|\bME\\$|\bEXPLODE\\$|\bOBTAIN\\$|\bB64ENC\\$|\bB64DEC\\$|\bMERGE\\$
425.         </match>
426.     </context>
427.
428.     <context id="types" style-ref="type">
429.         <keyword>NUMBER</keyword>
430.         <keyword>STRING</keyword>
431.         <keyword>FLOATING</keyword>
432.         <keyword>FILE\*</keyword>
433.         <keyword>int</keyword>
434.         <keyword>double</keyword>
435.         <keyword>float</keyword>
436.         <keyword>long</keyword>
437.         <keyword>char</keyword>
438.         <keyword>short</keyword>
439.         <keyword>void</keyword>
440.         <keyword>signed</keyword>
441.         <keyword>unsigned</keyword>
442.         <keyword>static</keyword>
443.     </context>
444.
445.     <context id="special-constants" style-ref="special-constant">
446.         <match extended="true">
447.             \bMAXRANDOM\b|\bNL\\$|\bCR\\$|\bEL\\$|\bCL\\$|\bPI\b|\bREGLEN\b|\bRETVAL\b|\bLINENO\b|\bVERSION\\$|\bARGUMENT\\$|\bSOURCE\\$|\bNULL\b
448.         </match>
449.     </context>
450.
451.     <context id="boolean" style-ref="boolean">
452.         <keyword>FALSE</keyword>
453.         <keyword>TRUE</keyword>
454.     </context>
455.
456.     <context id="operators" style-ref="operator" extend-parent="false">
457.       <match>[-%\+/\*\=|&amp;&lt;&gt;()]</match>
458.     </context>
459.
460.     <context id="wrapper" style-ref="wrapperfunc">
461.         <keyword>INIT</keyword>
462.         <keyword>HUGOPTIONS</keyword>
463.         <keyword>PROPERTY</keyword>
464.         <keyword>GETPROPERTY</keyword>
465.         <keyword>SETPROPERTY</keyword>
466.         <keyword>QUIT</keyword>
467.         <keyword>DRAW</keyword>
468.         <keyword>HIDE</keyword>
469.         <keyword>SHOW</keyword>
470.         <keyword>WINDOW</keyword>
471.         <keyword>DISPLAY</keyword>
472.         <keyword>REGISTER</keyword>
473.         <keyword>METHOD</keyword>
474.         <keyword>TEXT</keyword>
475.         <keyword>GET</keyword>
476.         <keyword>SET</keyword>
477.         <keyword>NOTEBOOK</keyword>
478.         <keyword>BUTTON</keyword>
479.         <keyword>STOCK</keyword>
480.         <keyword>TOGGLE</keyword>
481.         <keyword>CHECK</keyword>
483.         <keyword>ENTRY</keyword>
485.         <keyword>MARK</keyword>
486.         <keyword>COMBO</keyword>
487.         <keyword>HSEPARATOR</keyword>
488.         <keyword>VSEPARATOR</keyword>
489.         <keyword>FRAME</keyword>
490.         <keyword>EDIT</keyword>
491.         <keyword>LIST</keyword>
492.         <keyword>MSGDIALOG</keyword>
493.         <keyword>FILEDIALOG</keyword>
494.         <keyword>SPIN</keyword>
495.         <keyword>IMAGE</keyword>
496.         <keyword>CANVAS</keyword>
497.         <keyword>CLIPBOARD</keyword>
498.         <keyword>PROGRESSBAR</keyword>
499.         <keyword>CALLBACK</keyword>
500.         <keyword>CALLBACKX</keyword>
501.         <keyword>MOUSE</keyword>
502.         <keyword>CIRCLE</keyword>
503.         <keyword>PIXEL</keyword>
504.         <keyword>LINE</keyword>
505.         <keyword>SQUARE</keyword>
506.         <keyword>OUT</keyword>
507.         <keyword>PICTURE</keyword>
508.         <keyword>ATTACH</keyword>
509.         <keyword>TIMEOUT</keyword>
510.         <keyword>FONT</keyword>
511.         <keyword>DISABLE</keyword>
512.         <keyword>ENABLE</keyword>
513.         <keyword>FOCUS</keyword>
514.         <keyword>UNFOCUS</keyword>
515.         <keyword>SCREENSIZE</keyword>
516.         <keyword>KEY</keyword>
517.         <keyword>RESETKEY</keyword>
518.         <keyword>WIDGET</keyword>
519.         <keyword>SYNC</keyword>
520.         <keyword>INIT</keyword>
521.         <keyword>HSLIDER</keyword>
522.         <keyword>VSLIDER</keyword>
523.         <keyword>ISPRIME</keyword>
524.         <keyword>PRECISION</keyword>
525.         <keyword>FCOMPARE</keyword>
526.         <keyword>BACKEND</keyword>
527.         <keyword>FULLSCREEN</keyword>
528.         <keyword>ARC</keyword>
529.         <keyword>TRIANGLE</keyword>
530.         <keyword>POLYGON</keyword>
531.         <keyword>QBEZIER</keyword>
532.         <keyword>CBEZIER</keyword>
533.         <keyword>PAINT</keyword>
534.         <keyword>GETINK</keyword>
535.         <keyword>FLIP</keyword>
536.         <keyword>GRID</keyword>
537.         <keyword>INK</keyword>
538.         <keyword>PEN</keyword>
539.         <keyword>ROTATION</keyword>
540.         <keyword>SCALE</keyword>
541.         <keyword>MOVE</keyword>
542.         <keyword>CLS</keyword>
543.         <keyword>WAITKEY</keyword>
544.         <keyword>PENUP</keyword>
545.         <keyword>PENDOWN</keyword>
546.         <keyword>PENXY</keyword>
547.         <keyword>PENTYPE</keyword>
548.         <keyword>TURNRIGHT</keyword>
549.         <keyword>TURN</keyword>
550.         <keyword>TURNLEFT</keyword>
551.         <keyword>RESETANGLE</keyword>
553.         <keyword>WIDTH</keyword>
554.         <keyword>HEIGHT</keyword>
555.         <keyword>REFRESH</keyword>
556.     </context>
557.
558.     <context id="wrapperstr" style-ref="wrapperfunc">
559.         <match extended="true">
561.         </match>
562.     </context>
563.
564.     <context id="bacon">
565.         <include>
566.             <context ref="def:shebang" style-ref="shebang"/>
567.             <context ref="line-comment"/>
568.             <context ref="old-style-comment"/>
569.             <context ref="multiline-comment"/>
570.             <context ref="string"/>
571.             <context ref="strfunction"/>
572.             <context ref="function"/>
573.             <context ref="keywords"/>
574.             <context ref="special-constants"/>
575.             <context ref="types"/>
576.             <context ref="boolean"/>
577.             <context ref="operators"/>
578.             <context ref="wrapper"/>
579.             <context ref="wrapperstr"/>
580.         </include>
581.     </context>
582.
583. </definitions>
584.
585. </language>
586.
« Last Edit: April 06, 2019, 03:30:56 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #127 on: April 02, 2019, 10:33:45 PM »
I found a C example of accessing the relative humidity, temperature and hPa pressure sensors. I'm not sure yet if it will work with the Sense HAT emulator or not. To me it looks like opening a file link to the /dev/i2c-1 device interface and reading and writing up to 16 bytes of data.

Writting a Sense HAT API in the language of choice (must run on RPi) might be a good code challenge.

basicsensor.c
Code: C
1. /*
2. gcc -g -O -Wall -o basicsensor basicsensor.c
3.
4. This program is based on experix, an experiment and process control interface.
5. */
6. #include <stdio.h>
7. #include <fcntl.h>
8. #include <time.h>
9. #include <unistd.h>
10. #include <sys/types.h>
11. #include <sys/stat.h>
12. #include <sys/ioctl.h>
13. #include <stdlib.h>
14. #include <asm/types.h>
15. #include "HTS221.h"   // HTS221 relative humidity and temperature sensor
16. #include "LPS25H.h"   // LPS25H MEMS 260-1260 hPa pressure sensor
17.
18. // averaging mode and output rate definitions for HTS221 and LPS25H
19. #define LPS25HifAVGP 3
20. #define LPS25HifODR 3
21. #define HTS221ifODR 3
22. #define HTS221ifAVGT 3
23. #define HTS221ifAVGH 3
24.
25. // definitions for i2c-dev
26. #define DEVPATH_I2C     "/dev/i2c-1"  // the device file
27. #define I2C_SLAVE       0x0703        // ioctl:  Use this slave address
28.
29. __s16 HTS221_T_OUT, HTS221_H_OUT;
30. __s32 LPS25H_P_OUT;
31. __s16 H0_T0_OUT = 0;
32. __s16 H1_T0_OUT = 0;
33. __s16 T0_OUT = 0;
34. __s16 T1_OUT = 0;
35. float H0_rH, H1_rH;
36. float T0_degC, T1_degC;
37. __u8 LPS25H_status, LPS25H_fifo_status, HTS221_status;
38. char i2cDp[] = DEVPATH_I2C;
39.
40. float P_LPS25H, T_HTS221, H_HTS221;
42.   P_LPS25H = (float)(LPS25H_P_OUT) / 4096.0f;
43.   T_HTS221 = T0_degC + (((float)(HTS221_T_OUT)-T0_OUT)/(T1_OUT-T0_OUT))*(T1_degC-T0_degC);
44.   H_HTS221 = H0_rH + (((float)(HTS221_H_OUT)-H0_T0_OUT)/(H1_T0_OUT-H0_T0_OUT))*(H1_rH-H0_rH);
45.
46.   printf( "P_LPS25H  mbar      T_HTS221    deg C     H_HTS221    rH\n" );
47.   printf( "0x%-8.04x%+-10.5g0x%-10.04x%+-10.5g0x%-10.04x%+-10.5g\n",
48.          (__u32)(LPS25H_P_OUT), P_LPS25H, (__u16)(HTS221_T_OUT), T_HTS221,
49.          (__u16)(HTS221_H_OUT), H_HTS221 );
50. }
51.
52. int main() {
53.   int i2c, res;
54.   unsigned char buf[16];
55.   unsigned char HTS221cal[16];
56.   __u16 T0_degC_x8;
57.   __u16 T1_degC_x8;
58.   __u8 H0_rH_x2;
59.   __u8 H1_rH_x2;
60.
61.   // open the i2c device on raspberry pi
62.   i2c = open( i2cDp, O_RDWR );
63.   if ( i2c == -1 ) {
64.     perror( "open i2c" );
65.     return 1;
66.   }
67.
68.   // discover LPS25H
69.   res = ioctl( i2c, I2C_SLAVE, LPS25H_SAD );
70.   buf[0] = LPS25H_WHO_AM_I;
71.   res = write( i2c, buf, 1 );
72.   if ( res == -1 ) {
73.     perror( "write i2c" );
74.     return 1;
75.   }
76.   res = read( i2c, buf, 1 );
77.   if ( res != 1 ) {
78.     if ( res == -1 ) perror( "read i2c" );
79.     else printf( "read i2c returns %d\n", res );
80.     return 1;
81.   }
82.   if ( buf[0] != LPS25H_who_am_i ) {
83.     printf( "expect LPS25H id = 0x%x, get 0x%x\n", LPS25H_who_am_i, buf[0] );
84.     return 1;
85.   }
86.   // Set up for temperature measurements using the LPS25H
87.   // Set LPS25H_CTRL_REG1 following usage in RTIMULibDrive11
88.   buf[0] = LPS25H_CTRL_REG1;
89.   buf[1] = LPS25H_CTRL_REG1_PD_if(1) |      // power up
90.          LPS25H_CTRL_REG1_ODR_if(LPS25HifODR) | // output data rate
91.          LPS25H_CTRL_REG1_DIFF_EN_if(0) |   // disable differential pressure
92.          LPS25H_CTRL_REG1_BDU_if(1) |       // enable block update
93.          LPS25H_CTRL_REG1_RESET_AZ_if(0) |  // do not auto-zero
94.          LPS25H_CTRL_REG1_SIM_if(0);        // SPI mode (irrelevant for i2c)
95.   res = write( i2c, buf, 2 );
96.   if ( res != 2 ) {
97.     perror( "i2c write LPS25H" );
98.     return 1;
99.   }
100.   /* Set pressure averaging modes: internal averaging numbers
101.                                        for mode = 0,  1,   2,   3
102.   LPS25HifAVGP      pressure averaging number     8, 32, 128, 512  */
103.   buf[0] = LPS25H_RES_CONF;
104.   buf[1] = LPS25H_AV_CONF_AVGP_if(LPS25HifAVGP);
105.   res = write( i2c, buf, 2 );
106.   /* Set FIFO mode. (The FIFO holds pressure data so this should not make a
107.   difference for temperature.) */
108.   buf[0] = LPS25H_FIFO_CTRL;
109.   buf[1] = LPS25H_FIFO_CTRL_F_MODE_if(6) |    // running average
110.            LPS25H_FIFO_CTRL_WTM_POINT_if(1);  // average 2 samples
111.   res = write( i2c, buf, 2 );
112.   // Set LPS25H_CTRL_REG2 following usage in RTIMULibDrive11
113.   buf[0] = LPS25H_CTRL_REG2;
114.   buf[1] = LPS25H_CTRL_REG2_BOOT_if(0) |      // no refresh registers from flash
115.            LPS25H_CTRL_REG2_FIFO_EN_if(1) |   // enable FIFO
116.            LPS25H_CTRL_REG2_WTM_EN_if(0) |    // no enable FIFO watermark
117.            LPS25H_CTRL_REG2_FIFO_MEAN_DEC_if(0) | // no enable 1 Hz ODR decim.
118.            LPS25H_CTRL_REG2_SWRESET_if(0) |   // no software reset (with BOOT=1)
119.            LPS25H_CTRL_REG2_AUTO_ZERO_if(0) | // no copy PRESS_OUT to REF_P
120.            LPS25H_CTRL_REG2_ONE_SHOT_if(0);   // no do one-shot here
121.   /* LPS25H_CTRL_REG3, LPS25H_CTRL_REG4, LPS25H_INT_CFG are irrelevant since
122.      interrupts are not being used. */
123.    // discover HTS221
124.    res = ioctl( i2c, I2C_SLAVE, HTS221_SAD );
125.    buf[0] = HTS221_WHO_AM_I;
126.    res = write( i2c, buf, 1 );
127.    if ( res != 1 ) {
128.      perror( "i2c write HTS221" );
129.      return 1;
130.    }
131.    res = read( i2c, buf, 1 );
132.    if ( res != 1 ) {
133.      if ( res == -1 ) perror( "read i2c" );
134.      else printf( "read i2c returns %d\n", res );
135.      return 1;
136.    }
137.    if ( buf[0] != HTS221_who_am_i ) {
138.      printf( "expect HTS221 id = 0x%x, get 0x%x\n", HTS221_who_am_i, buf[0] );
139.      return 1;
140.    }
141.    // Set up for temperature measurements using the HTS221
142.    // Set HTS221_CTRL_REG1 following usage in RTIMULibDrive11
143.    buf[0] = HTS221_CTRL_REG1;                                         //Drive11
144.    buf[1] = HTS221_CTRL_REG1_PD_if(1) |            // power up             1
145.             HTS221_CTRL_REG1_BDU_if(1) |           // enable block update  1
146.             HTS221_CTRL_REG1_ODR_if(HTS221ifODR);  // output data rate     3
147.    res = write( i2c, buf, 2 );
148.    /* Set temperature and humidity averaging modes: internal averaging numbers
149.                                     for mode = 0, 1,  2,  3,  4,   5,   6,   7
150.    HTS221ifAVGH  humidity averaging number     4, 8, 16, 32, 64, 128, 256, 512
151.    HTS221ifAVGT  temperature averaging number  2, 4,  8, 16, 32,  64, 128, 256 */
152.    buf[0] = HTS221_AV_CONF;                                           //Drive11
153.    buf[1] = HTS221_AV_CONF_AVGT_if(HTS221ifAVGT) |                    //   3
154.             HTS221_AV_CONF_AVGH_if(HTS221ifAVGH);                     //   3
155.    res = write( i2c, buf, 2 );
156.    /* Read the calibration registers and calculate conversion coefficients.
157.    See datasheet tables 19 and 20. */
158.    buf[0] = HTS221_CAL_H0_rH_x2 | HTS221_reg_auto;
159.    res = write( i2c, buf, 1 );
160.    res  = read( i2c, HTS221cal, 16 );
161.    if ( res != 16 ) {
162.      if ( res == -1 ) perror( "HTS221_CAL_H0_rH_x2" );
163.      else printf( "read 16 at HTS221_CAL_H0_rH_x2 returns %d\n", res );
164.    }
165.
166.    T0_degC_x8 = (((__u16)HTS221cal[5] & 0x3 ) << 8) | (__u16)HTS221cal[2];
167.    T1_degC_x8 = (((__u16)HTS221cal[5] & 0xc ) << 6) | (__u16)HTS221cal[3];
168.    T0_OUT = ((__s16*)(HTS221cal))[6];
169.    T1_OUT = ((__s16*)(HTS221cal))[7];
170.    H0_T0_OUT = ((__s16*)(HTS221cal))[3];
171.    H1_T0_OUT = ((__s16*)(HTS221cal))[5];
172.    H0_rH_x2 = ((__u8*)(HTS221cal))[0];
173.    H1_rH_x2 = ((__u8*)(HTS221cal))[1];
174.    T0_degC = (float)(T0_degC_x8) / 8.0f;
175.    T1_degC = (float)(T1_degC_x8) / 8.0f;
176.    H0_rH = (float)(H0_rH_x2) / 2.0f;
177.    H1_rH = (float)(H1_rH_x2) / 2.0f;
178.
179.    // get a LPS25H pressure sample
180.    res = ioctl( i2c, I2C_SLAVE, LPS25H_SAD );
181.    buf[0] = LPS25H_STATUS_REG;
182.    res = write( i2c, buf, 1 );
183.    res = read( i2c, buf, 2 );
184.    LPS25H_status = buf[0];
185.    buf[0] = LPS25H_FIFO_STATUS;
186.    res = write( i2c, buf, 1 );
187.    res = read( i2c, buf, 2 );
188.    LPS25H_fifo_status = buf[0];
189.    buf[0] = LPS25H_PRESS_POUT|LPS25H_reg_auto;
190.    res = write( i2c, buf, 1 );
191.    res = read( i2c, buf, 3 ); // read registers at 0x28, 0x29, 0x2a
192.    LPS25H_P_OUT = (((__s32)(buf[2])) << 16) | (((__s32)(buf[1])) << 8) | (((__s32)(buf[0])));
193.    // get a HTS221 humidity sample
194.    res = ioctl( i2c, I2C_SLAVE, HTS221_SAD );
195.    buf[0] = HTS221_STATUS_REG;
196.    res = write( i2c, buf, 1 );
197.    res = read( i2c, buf, 2 );
198.    HTS221_status = buf[0];
199.    buf[0] = HTS221_HUMIDITY_OUT | HTS221_reg_auto;
200.    res = write( i2c, buf, 1 );
201.    res = read( i2c, buf, 2 );
202.    HTS221_H_OUT = (((__s16)buf[1]) << 8) | (__s16)buf[0];
203.    buf[0] = HTS221_TEMP_OUT | HTS221_reg_auto;
204.    res = write( i2c, buf, 1 );
205.    res = read( i2c, buf, 2 );
206.    HTS221_T_OUT = (((__s16)buf[1]) << 8) | (__s16)buf[0];
207.
209.
210.    close(i2c);
211.    return 0;
212. }
213.

HTS221.h - relative humidity and temperature sensor
Code: C
1. /*
2. This file is part of experix, an experiment and process control interface.
3. Copyright (C) 2004-2016 William Bayard McConnaughey
4.
5. experix is free software; you can redistribute it and/or modify
7. the Free Software Foundation; either version 3 of the Licence, or
8. (at your option) any later version.
9.
10. experix is distributed in the hope that it will be useful,
11. but WITHOUT ANY WARRANTY; without even the implied warranty of
12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13. GNU General Public Licence for more details.
14.
15. You should have received a copy of the GNU General Public Licence
16. along with experix.  If not, see <http://www.gnu.org/licenses/>.
17.
18. Programming definitions for the HTS221 capacitive digital sensor for
19. relative humidity and temperature (STMicroelectronics NV)
20. Reference: datasheet DM00116291.pdf (October 2015), www.st.com
21. Very terse comments are provided in this file. See explanations in the
22. datasheet. This header is specialized for Raspberry Pi sense-hat usage.
23. Names used are mostly composed by prepending "HTS221_" on the register and
24. bitfield names used in the datasheet. Terms in ALL CAPS are the register
25. designations; those with lower-case letters are data values or bitfield
26. macros.
27. Registers marked with the comment "//2" are register pairs, where the
28. designation applies to the least significant byte and the high byte is
29. addressed by adding "+1". (The data sheet uses two definitions with "_L"
30. and "_H" on the end).
31. For bitfields we are using function-style definitions. The ones ending in
32. "_ef(v)" are for extracting field values from a register value, and the
33. "_if(f)" ones are for inserting fields into a register value. Example: to make
34. the HTS221_AV_CONF setting for AVGH=6 and AVGT=2, use this expression:
35. HTS221_AV_CONF_AVGT_if(2)|HTS221_AV_CONF_AVGH_if(6)
36. Field length in bits is shown by comments such as  //2b
37. Note that these macros do not check for inappropriate submitted values.
38. Note well, regarding register addresses not mentioned herein: from the
39. datasheet, "Registers marked as Reserved must not be changed. Writing to
40. those registers may cause permanent damage to the device."
41. */
42. /* Notes on Raspberry Pi sense-hat use
43. CS (pin 6) is high: I2C interface is used, with GPIO3 to SCL/SPC (pin 2) and
44. GPIO2 to SDA/SDI/SDO (pin 4). DRDY (pin 3) goes only to a test-point.
45. */
47. #define HTS221_WHO_AM_I     0x0f  // read this to get--
48. #define HTS221_who_am_i     0xbc  //   this device identifier
49.
51.
52. // humidity and temerature resolution mode:  see datasheet table 16
53. #define HTS221_AV_CONF      0x10
54. #define HTS221_AV_CONF_AVGT_if(f) ((f)<<3)      //3b temperature avg. mode
55. #define HTS221_AV_CONF_AVGH_if(f) (f)           //3b humidity avg. mode
56.
57. #define HTS221_CTRL_REG1    0x20
58. #define HTS221_CTRL_REG1_PD_if(f) ((f)<<7)      //1b 0= powerdown, 1= active
59. #define HTS221_CTRL_REG1_BDU_if(f) ((f)<<2)     //1b 0= continuous update
60. #define HTS221_CTRL_REG1_ODR_if(f) (f)          //2b output data rate
61.
62. #define HTS221_CTRL_REG2    0x21
63. #define HTS221_CTRL_REG2_BOOT_if(f) ((f)<<7)    //1b 1= reboot memory content
64. #define HTS221_CTRL_REG2_HEATER_if(f) ((f)<<1)  //1b 1= enable heater
65. #define HTS221_CTRL_REG2_ONE_SHOT_if(f) (f)     //1b 1= start for a new dataset
66.
67. // note: DRDY pin is not being used in the sense-hat
68. #define HTS221_CTRL_REG3    0x22
69. #define HTS221_CTRL_REG3_DRDY_H_L_if(f) ((f)<<7) //1b DRDY active hi/lo
70. #define HTS221_CTRL_REG3_PP_OD_if(f) ((f)<<6)    //1b push-pull / open-drain
71. #define HTS221_CTRL_REG3_DRDY_EN_if(f) ((f)<<2)  //1b enable DRDY
72.
73. #define HTS221_STATUS_REG   0x27
74. #define HTS221_STATUS_REG_H_DA_ef(v) (((v)>>1)&1) //1b new humidity avail.
75. #define HTS221_STATUS_REG_T_DA_ef(v) ((v)&1)      //1b new temperature avail.
76.
77. #define HTS221_HUMIDITY_OUT 0x28  //2  humidity data out
78. #define HTS221_TEMP_OUT     0x2a  //2  temperature data out
79.
80. /* See datasheet discussion on use of calibration registers and values.
81. DO NOT MODIFY these registers. Names are made by prepending "HTS221_CAL_"
82. onto the register names in table 19, except where that would not be usable. */
83. #define HTS221_CAL_H0_rH_x2   0x30
84. #define HTS221_CAL_H1_rH_x2   0x31
85. #define HTS221_CAL_T0_degC_x8 0x32
86. #define HTS221_CAL_T1_degC_x8 0x33
87. #define HTS221_CAL_T1_T0_msb  0x35
88. #define HTS221_CAL_H0_T0_OUT  0x36  //2
89. #define HTS221_CAL_H1_T0_OUT  0x3a  //2
90. #define HTS221_CAL_T0_OUT     0x3c  //2
91. #define HTS221_CAL_T1_OUT     0x3e  //2
92.

LPS25H.h - hPa pressure sensor
Code: C
1. /*
2. This file is part of experix, an experiment and process control interface.
3. Copyright (C) 2004-2016 William Bayard McConnaughey
4.
5. experix is free software; you can redistribute it and/or modify
7. the Free Software Foundation; either version 3 of the Licence, or
8. (at your option) any later version.
9.
10. experix is distributed in the hope that it will be useful,
11. but WITHOUT ANY WARRANTY; without even the implied warranty of
12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13. GNU General Public Licence for more details.
14.
15. You should have received a copy of the GNU General Public Licence
16. along with experix.  If not, see <http://www.gnu.org/licenses/>.
17.
18. Programming definitions for the LPS25H MEMS 260-1260 hPa pressure sensor
19. (STMicroelectronics NV)
20. Reference: datasheet DM00066332.pdf (January 2014), www.st.com
21. Very terse comments are provided in this file. See explanations in the
22. datasheet. This header is specialized for Raspberry Pi sense-hat usage.
23. Names used are mostly composed by prepending "LPS25H_" on the register and
24. bitfield names used in the datasheet. Terms in ALL CAPS are the register
25. designations; those with lower-case letters are data values or bitfield
26. macros.
27. Registers marked with the comment "//2" are register pairs, where the
28. designation applies to the least significant byte and the high byte is
29. addressed by adding "+1". Likewise, "//3" signifies a register triple. (The
30. data sheet uses two or three definitions with "_XL", "_L" and "_H" on the end).
31. For bitfields we are using function-style definitions. The ones ending in
32. "_ef(v)" are for extracting field values from a register value, and the "_if(f)"
33. ones are for inserting field values into a register value. Example: to make
34. the LPS25H_AV_CONF setting for AVGP=3 and AVGT=2, use this expression:
35. LPS25H_AV_CONF_AVGT_if(2)|LPS25H_AV_CONF_AVGP_if(3)
36. Field length in bits is shown by comments such as  //2b
37. Note that these macros do not check for inappropriate submitted values.
38. Note well, regarding register addresses not mentioned herein: from the
39. datasheet, "Registers marked as Reserved must not be changed. The Writing to
40. those registers may cause permanent damages to the device. The content of the
41. registers that are loaded at boot should not be changed. They contain the
42. factory calibration values. Their content is automatically restored when the
43. device is powered-up."
44. */
45. /* Notes on Raspberry Pi sense-hat use
46. CS (pin 6) is high: I2C interface is used, with GPIO3 to SCL/SPC (pin 2) and
47. GPIO2 to SDA/SDI/SDO (pin 4). INT1 (pin 7) goes only to a test-point. SDO/SA0
48. (pin 5) is grounded, so LSB of device address is 0.
49. */
51. #define LPS25H_WHO_AM_I     0x0f  // read this to get--
52. #define LPS25H_who_am_i     0xbd  //   this device identifier
53.
55.
56. #define LPS25H_REF_P        0x08  //3  Reference pressure
57.
58. #define LPS25H_RES_CONF     0x10  // Pressure and temperature resolution mode
59. #define LPS25H_AV_CONF_AVGT_if(f) ((f)<<2)   //2b temperature. averaging mode
60. #define LPS25H_AV_CONF_AVGP_if(f) (f)        //2b pressure averaging mode
61.
62. #define LPS25H_CTRL_REG1    0x20
63. #define LPS25H_CTRL_REG1_PD_if(f)       ((f)<<7) //1b powerdown:  1= device active
64. #define LPS25H_CTRL_REG1_ODR_if(f)      ((f)<<4) //3b output data rate (table 18)
65. #define LPS25H_CTRL_REG1_DIFF_EN_if(f)  ((f)<<3) //1b enable diff. pressure
66. #define LPS25H_CTRL_REG1_BDU_if(f)      ((f)<<2) //1b block data update
67. #define LPS25H_CTRL_REG1_RESET_AZ_if(f) ((f)<<1) //1b pressure reset autozero
68. #define LPS25H_CTRL_REG1_SIM_if(f)      (f)      //1b SPI serial interface mode
69.
70. #define LPS25H_CTRL_REG2    0x21
71. #define LPS25H_CTRL_REG2_BOOT_if(f)     ((f)<<7)  //1b refresh registers from flash
72. #define LPS25H_CTRL_REG2_FIFO_EN_if(f)  ((f)<<6)  //1b enable FIFO
73. #define LPS25H_CTRL_REG2_WTM_EN_if(f)   ((f)<<5)  //1b enable FIFO watermark
74. #define LPS25H_CTRL_REG2_FIFO_MEAN_DEC_if(f) ((f)<<4) //1b enable 1 Hz ODR decim.
75. #define LPS25H_CTRL_REG2_SWRESET_if(f)  ((f)<<2)  //1b software reset (with BOOT=1)
76. #define LPS25H_CTRL_REG2_AUTO_ZERO_if(f) ((f)<<1) //1b copy PRESS_OUT to REF_P
77. #define LPS25H_CTRL_REG2_ONE_SHOT_if(f) (f)     //1b with ODR=0, start new conv.
78.
79. #define LPS25H_CTRL_REG3    0x22     // (sense-hat: INT1 not used)
80. #define LPS25H_CTRL_REG3_INT_H_L_if(f)  ((f)<<7)  //1b interrupt active hi/lo
81. #define LPS25H_CTRL_REG3_PP_OD_if(f)    ((f)<<6)  //1b push-pull/open-drain select
82. #define LPS25H_CTRL_REG3_INT1_S_if(f)   (f)     //2b INT pad signal (table 19)
83.
84. #define LPS25H_CTRL_REG4    0x23     // (sense-hat: INT1 not used)
85. #define LPS25H_CTRL_REG4_P1_EMPTY_if(f) ((f)<<3)  //1b Empty signal on INT1
86. #define LPS25H_CTRL_REG4_P1_WTM_if(f)   ((f)<<2)  //1b Watermark signal on INT1
87. #define LPS25H_CTRL_REG4_P1_Overun_if(f) ((f)<<1) //1b Overrun signal on INT1
88. #define LPS25H_CTRL_REG4_P1_DRDY_if(f)  (f)       //1b Data ready signal on INT1
89.
90. #define LPS25H_INT_CFG      0x24
91. #define LPS25H_INT_CFG_LIR_if(f)   ((f)<<2) //1b latch int. source into INT_SOURCE
92. #define LPS25H_INT_CFG_PL_E_if(f)  ((f)<<1) //1b enable int. on diff. press. low
93. #define LPS25H_INT_CFG_PH_E_if(f)  (f)      //1b enable int. on diff. press. high
94.
95. #define LPS25H_INT_SOURCE   0x25
96. #define LPS25H_INT_SOURCE_IA_ef(v)   (((v)>>2)&1) //1b interrupt active
97. #define LPS25H_INT_SOURCE_PL_ef(v)   (((v)>>1)&1) //1b differential pressure low
98. #define LPS25H_INT_SOURCE_PH_ef(v)   ((v)&1)      //1b differential pressure high
99.
100. #define LPS25H_STATUS_REG   0x27
101. #define LPS25H_STATUS_REG_P_OR_ef(v) (((v)>>5)&1) //1b pressure data overrun
102. #define LPS25H_STATUS_REG_T_OR_ef(v) (((v)>>4)&1) //1b temperature data overrun
103. #define LPS25H_STATUS_REG_P_DA_ef(v) (((v)>>1)&1) //1b pressure data available
104. #define LPS25H_STATUS_REG_T_DA_ef(v) ((v)&1)      //1b temperature data available
105.
106. #define LPS25H_PRESS_POUT   0x28  //3 pressure data, 3 bytes at 0x28,0x29,0x2a
107. #define LPS25H_TEMP_OUT     0x2b  //2 temperature data, 2 bytes at 0x2b,0x2c
108.
109. #define LPS25H_FIFO_CTRL    0x2e  // datasheet sec 8: FIFO operating details
110. #define LPS25H_FIFO_CTRL_F_MODE_if(f)    ((f)<<5) //3b FIFO mode, tables 20,22,23
111. #define LPS25H_FIFO_CTRL_WTM_POINT_if(f) (f)    //5b watermark select, table 21
112.
113. #define LPS25H_FIFO_STATUS  0x2f
114. #define LPS25H_FIFO_STATUS_WTM_FIFO_ef(v)   ((v)>>7)     //1b watermark status
115. #define LPS25H_FIFO_STATUS_FULL_FIFO_ef(v)  (((v)>>6)&1) //1b overrun bit status
116. #define LPS25H_FIFO_STATUS_EMPTY_FIFO_ef(v) (((v)>>5)&1) //1b empty FIFO
117. #define LPS25H_FIFO_STATUS_DIFF_POINT_ef(v) ((v)&0x1f)   //5b FIFO data level
118.
119. #define LPS25H_THS_P        0x30  //2  threshold pressure for interrupt
120. #define LPS25H_RPDS         0x39  //2  pressure offset after soldering
121.

It would seem writing a ScriptBasic Sense HAT extension module shouldn't be that hard.
« Last Edit: April 06, 2019, 03:31:08 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #128 on: April 03, 2019, 12:12:28 AM »
There doesn't seem to be a /dev/i2c-1 device on my Rpi. I have seen the term fb0 used as a reference to the Sense HAT so I used that. I had the emulator running but it seems to error out trying to read the first sensor. It might be looking for the physical Sense HAT board which hasn't arrived yet.

pi@raspberrypi:~/sbrpi/sensehat \$ ./basicsensor
expect LPS25H id = 0xbd, get 0xec
pi@raspberrypi:~/sbrpi/sensehat \$

It seems you have to enable the i2c interface via the sudo raspi-config utility and then reboot. I also returned back to the /dev/i2c-1 device reference and now I'm getting and I/O Error which looks promising since I don't have a Sense HAT board installed yet. The emulator doesn't seem to work with the i2c API. Maybe it uses pigio instead?

pi@raspberrypi:~/sbrpi/sensehat \$ ./basicsensor
write i2c: Remote I/O error
pi@raspberrypi:~/sbrpi/sensehat \$ i2cdetect -y 1
0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi:~/sbrpi/sensehat \$ i2cdump -y 1 0x68
No size specified (using byte-data access)
0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
10: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
20: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
30: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
40: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
50: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
60: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
70: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
80: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
90: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
a0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
d0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
e0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
f0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
pi@raspberrypi:~/sbrpi/sensehat \$

« Last Edit: April 06, 2019, 03:31:21 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #129 on: April 03, 2019, 11:24:42 PM »
I discovered the the /dev/i2c-1 device is a serial connection. I thought I would give SB a try connecting directly. It seems to work (no errors) but doesn't return anything either. It looks like I'm not going to make any real progress with this until my Sense HAT board arrives. I've been unable to find a way to use the Sense HAT emulator from C. The other issue is I need to emulate the ioctl() function.

Code: Script BASIC
1. OPEN "/dev/i2c-1" FOR BINARY AS #1
2. iout = INPUT(1,1)
3. PRINT iout,"\n"
4. CLOSE(1)
5.
« Last Edit: April 06, 2019, 03:31:37 PM by John »

#### AIR

• BASIC Developer
• Posts: 684
##### Re: SB Pi
« Reply #130 on: April 04, 2019, 04:22:59 PM »
I discovered the the /dev/i2c-1 device is a serial connection. I thought I would give SB a try connecting directly. It seems to work (no errors) but doesn't return anything either. It looks like I'm not going to make any real progress with this until my Sense HAT board arrives. I've been unable to find a way to use the Sense HAT emulator from C. The other issue is I need to emulate the ioctl() function.

Code: Script BASIC
1. OPEN "/dev/i2c-1" FOR BINARY AS #1
2. iout = INPUT(1,1)
3. PRINT iout,"\n"
4. CLOSE(1)
5.

You may want to check out the following: c-sense-hat

AIR.

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #131 on: April 04, 2019, 04:53:22 PM »
Awesome!

My Sense HAT is due to arrive on Saturday.

It would be great to find out if the Sense HAT emuator will work with C or not.

Here is another C reference that is helpful.

ic2-dev Info
« Last Edit: April 06, 2019, 03:32:03 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #132 on: April 04, 2019, 06:47:48 PM »
AIR,

I created a fresh Raspbery PI full NOOBS and tested your latest AppImage release. Everything seems to work great. I didn't install MySQL so that extension module wasn't in the test script set.

I've attached the ScriptBasic examples I used to test the following extension modules.

* cURL
* SQLite
* JSON
* XML
* Regex

Examples not included:

MySQL
ODBC

« Last Edit: April 06, 2019, 03:32:16 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #133 on: April 05, 2019, 07:58:12 AM »
Quote
It usually says "Illegal instruction" if you try to run a Pi3 binary on a Pi Zero.
Segfault is just a plain bug!

The Pi Zero is ARMv6, whereas the Pi3 models are ARMv8

Code compiled to run under Raspbian on a Pi Zero (or Pi 1) will run on any Pi released so far.

A RPi forum member tried to run the SB AppImage on a RPi Zero and it failed. This is news that not all RPi 's are code compatible.
« Last Edit: April 06, 2019, 03:32:29 PM by John »

#### John

• Forum Support / SB Dev
• Posts: 2745
##### Re: SB π
« Reply #134 on: April 05, 2019, 03:02:09 PM »
Good News!

My Sense HAT board arrived a day early.

I'm looking forward to get ScriptBasic working with it.
« Last Edit: April 06, 2019, 03:32:43 PM by John »