libpruio  0.0
AM33xx-PRU driver for digital input / output and analog input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
triggers.bas
Go to the documentation of this file.
1 /'* \file triggers.bas
2 \brief Example: start measurements in MM mode by triggers.
3 
4 This file contains an example on how to use libpruio to measure analog
5 input and draw a graph of the sampled data. Triggering of measurement
6 can be done by different events.
7 
8 Licence: GPLv3
9 
10 Copyright 2014 by Thomas{ dOt ]Freiherr[ At ]gmx[ DoT }net
11 
12 
13 Compile by:
14 
15 fbc -w all triggers.bas
16 
17 '/
18 
19 '* include libpruio
20 #INCLUDE ONCE "../pruio/pruio.bi"
21 '* include the convenience macros for header pins
22 #INCLUDE ONCE "../pruio/pruio_pins.bi"
23 '* include FreeBASIC grafics
24 #INCLUDE ONCE "fbgfx.bi"
25 
26 '* define the pin to use for digital trigger
27 #DEFINE PIN P8_07
28 '* define the step number to use for analog trigger
29 #DEFINE STP 11
30 
31 VAR S_W = 0, S_H = 0, gap = 2, BPP = 0
32 SCREENINFO S_W, S_H, BPP ' get screen resolution
33 IF LEN(COMMAND) THEN ' customized resolution required?
34  VAR p = INSTR(COMMAND, "x") _
35  , w = VALINT(COMMAND) _
36  , h = VALINT(MID(COMMAND, p + 1))
37  IF p ANDALSO w ANDALSO h THEN
38  S_W = IIF(w < S_W, w, S_W) ' set maximum custom resolution
39  S_H = IIF(h < S_H, h, S_H)
40  ELSE
41  PRINT "failed setting resolution (specify like 640x400)" : END 1
42  END IF
43 END IF
44 
45 SCREENRES S_W, S_H, BPP, , fb.GFX_FULLSCREEN ' set screen resolution
46 IF 0 = SCREENPTR THEN PRINT "no grafic available" : END 1
47 
48 DIM AS uint32 _
49  col(...) = { _ '*< the colors for the lines (= channels)
50  RGBA(255, 255, 255, 255) _
51  , RGBA(255, 0, 0, 255) _
52  , RGBA( 0, 255, 0, 255) _
53  , RGBA( 0, 0, 255, 255) _
54  , RGBA(255, 255, 0, 255) _
55  , RGBA(255, 0, 255, 255) _
56  , RGBA( 0, 255, 255, 255) _
57  }
58 
59 VAR io = NEW PruIo()
60 
61 WITH *io
62  DO ' pseudo loop, just to avoid GOTOs
63  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
64 
65 
66  IF .gpio_set(PIN, PRUIO_IN_1) THEN _ ' configure GPIO pin
67  ?"failed setting trigger pin (" & *.Errr & ")" : EXIT DO
68 
69  IF .adc_step(STP, 4, 0, 0, 0) THEN _ ' configure fast ADC step
70  ?"failed setting trigger step (" & *.Errr & ")" : EXIT DO
71 
72 ' config OK here, transfer local settings to PRU and start PRU driver
73  VAR samp = S_W \ gap _ ' number of samples to fetch
74  , mask = (1 SHL 5) + (1 SHL 8) _ ' steps 5 & 8 active (AIN4, AIN7)
75  , tmr = 1e6 ' the sampling rate (1 kHz)
76  IF .config(samp, mask, tmr) THEN _
77  ?"config failed (" & *.Errr & ")" : EXIT DO
78 
79  VAR trg = 0
80  VAR trg1 = .mm_trg_pin(PIN) : IF 0 = trg1 THEN _
81  ?"trg1 spec failed (" & *.Errr & ")" : EXIT DO
82  VAR trg2 = .mm_trg_ain(STP, &h8000) : IF 0 = trg2 THEN _
83  ?"trg2 spec failed (" & *.Errr & ")" : EXIT DO
84  VAR trg3 = .mm_trg_pre(0, -&h8000, samp SHR 1) : IF 0 = trg3 THEN _
85  ?"trg3 spec failed (" & *.Errr & ")" : EXIT DO
86 
87  S_H -= 1
88  VAR lnr = IIF(S_H > 72, S_H SHR 3 - 8, 1) _ ' line number
89  , scale = S_H / 65520 _ ' scale sample to screen pixels
90  , k = 0 ' keycode for user input
91  DO ' loop to handle user actions
92  LOCATE lnr, 1, 0
93  ? ' print user menu
94  ?"Choose trigger type"
95  ?" 0 = no trigger (start immediately)"
96  ?" 1 = GPIO trigger (pin P8_07 low)"
97  ?" 2 = analog trigger, AIN4 > 0.9 V"
98  ?" 3 = analog pre-trigger, AIN4 < 0.9 V"
99  DO : SLEEP 1000, 0 : k = ASC(INKEY()) : LOOP UNTIL k ' get key
100 
101  SELECT CASE AS CONST k ' re-act on user keystrokes
102  CASE ASC("0") : trg = 0
103  CASE ASC("1") : trg = trg1 : ?"waiting for GPIO trigger (pin P8_07 low) ...";
104  CASE ASC("2") : trg = trg2 : ?"waiting for analog trigger (AIN4 > 0.9 V) ...";
105  CASE ASC("3") : trg = trg3 : ?"waiting for analog pre-trigger (any AIN < 0.9 V) ..." ;
106  CASE ELSE : EXIT DO
107  END SELECT
108 
109  IF .mm_start(trg) THEN ?"start failed (" & *.Errr & ")" : CONTINUE DO
110 
111  CLS
112  FOR c AS INTEGER = 0 TO .ChAz - 1
113  VAR i = c + .ChAz, x = gap
114  LINE (0, S_H - CUINT(.Value[c] * scale)) _
115  - (x, S_H - CUINT(.Value[i] * scale)), col(c)
116  DO
117  i += .ChAz : IF i >= .Samples THEN EXIT DO
118  x += gap
119  LINE - (x, S_H - CUINT(.Value[i] * scale)), col(c)
120  LOOP
121  NEXT
122  LOOP
123  LOOP UNTIL 1
124  IF .Errr THEN ?"press any key to quit" : SLEEP
125 END WITH
126 
127 DELETE io
128 
129