libpruio  0.2
Input/Output driver for digital/analog lines on Beagleboard hardware
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rb_oszi.bas
Go to the documentation of this file.
1 /'* \file rb_oszi.bas
2 \brief Example: fetch ADC samples in a ring buffer and draw graf.
3 
4 This file contains an example on how to use the ring buffer mode of
5 libpruio. A fixed step mask of AIN-4 and AIN-7 get sampled and drawn as
6 a line graf to a grafic window. Unlike IO mode, the step
7 mask cannot get changed in RB mode at run-time.
8 
9 Licence: GPLv3
10 
11 Copyright 2014 by Thomas{ dOt ]Freiherr[ At ]gmx[ DoT }net
12 
13 
14 Compile by: `fbc -w all rb_oszi.bas`
15 
16 \since 0.2
17 '/
18 
19 ' include libpruio
20 #INCLUDE ONCE "../pruio/pruio.bi"
21 ' include FreeBASIC grafics
22 #INCLUDE ONCE "fbgfx.bi"
23 
24 VAR S_W = 0 _ '*< The screen width.
25  , S_H = 0 _ '*< The srceen hight
26  , BPP = 0 _ '*< The bits per plain number.
27  , full = fb.GFX_FULLSCREEN '*< Fullscreen or windowed mode.
28 SCREENINFO S_W, S_H, BPP ' get screen resolution
29 IF LEN(COMMAND) THEN ' customized resolution required?
30  VAR p = INSTR(COMMAND, "x") _ '*< The position of the 'x' character (if any).
31  , w = VALINT(COMMAND) _ '*< The required window width.
32  , h = VALINT(MID(COMMAND, p + 1)) '*< The required window hight.
33  IF p ANDALSO w ANDALSO h THEN
34  IF w < S_W - 4 ANDALSO h < S_H - 24 THEN full = fb.GFX_WINDOWED
35  S_W = IIF(w < S_W, w, S_W) ' set maximum custom resolution
36  S_H = IIF(h < S_H, h, S_H)
37  ELSE
38  PRINT "set resolution like 640x400"
39  END
40  END IF
41 END IF
42 
43 SCREENRES S_W, S_H, BPP, 2, full ' set screen resolution
44 IF 0 = SCREENPTR THEN PRINT "no grafic available" : END
45 
46 '* The colors for the lines (= channels).
47 DIM AS UInt32 _
48  col(...) = { _
49  RGBA( 0, 0, 0, 255) _
50  , RGBA(255, 0, 0, 255) _
51  , RGBA( 0, 255, 0, 255) _
52  , RGBA( 0, 0, 255, 255) _
53  , RGBA(255, 255, 0, 255) _
54  , RGBA(255, 0, 255, 255) _
55  , RGBA( 0, 255, 255, 255) _
56  , RGBA(127, 127, 127, 255) _
57  }
58 
59 '* Macro to draw a graph from the valid half of the ring buffer.
60 #MACRO DRAW_GRAF()
61  LINE (0, 0) - (S_W, S_H), RGB(250, 250, 250), BF
62  FOR c AS INTEGER = 0 TO .Adc->ChAz - 1
63  VAR i = c + .Adc->ChAz
64  LINE (0, S_H - CUINT(p[c] * scale)) - _
65  (1, S_H - CUINT(p[i] * scale)), col(c)
66  FOR x AS INTEGER = 2 TO S_W
67  i += .Adc->ChAz
68  LINE - (x, S_H - CUINT(p[i] * scale)), col(c)
69  NEXT
70  NEXT
71 #ENDMACRO
72 
73 VAR io = NEW PruIo '*< Create a PruIo structure, wakeup subsystems.
74 
75 WITH *io
76  DO
77  IF .Errr THEN ?"NEW failed: " & *.Errr : EXIT DO
78 
79  VAR samp = S_W SHL 1 '*< The number of samples to fetch (ring buffer size).
80  S_W -= 1
81  S_H -= 1
82  VAR scale = S_H / 65520 '*< The scaling factor.
83 
84  IF .config(samp, &b100100000, 4e5) THEN _ ' configure steps 5+8
85  ?"config failed: " & *.Errr : EXIT DO
86 
87  VAR half = .Adc->Samples SHR 1 _ '*< The half size of the ring buffer.
88  , p = .Adc->Value '*< A (local) pointer to the samples.
89  IF .rb_start() THEN _ ' start ring buffer mode
90  ?"rb_start failed: " & *.Errr : EXIT DO
91 
92  DO ' read ring buffer and draw graf
93  WHILE .DRam[0] < half : WEND
94  DRAW_GRAF()
95  p += half
96  SCREENSET 0, 1
97 
98  WHILE .DRam[0] > half : WEND
99  DRAW_GRAF()
100  p -= half
101  SCREENSET 1, 0
102  LOOP UNTIL LEN(INKEY()) : ?
103  LOOP UNTIL 1
104  IF .Errr THEN SLEEP
105 END WITH
106 
107 DELETE(io)
108 
109 '' help Doxygen to dokument the main code
110 '&/** The main function. */
111 '&int main() {PruIo::PruIo(); PruIo::config(); PruIo::~PruIo();}
112