libpruio  0.0.2
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  , RGBA(127, 127, 127, 255) _
58  }
59 
60 VAR io = NEW PruIo()
61 
62 WITH *io
63  DO ' pseudo loop, just to avoid GOTOs
64  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
65 
66 
67  IF .gpio_set(PIN, PRUIO_IN_1) THEN _ ' configure GPIO pin
68  ?"failed setting trigger pin (" & *.Errr & ")" : EXIT DO
69 
70  IF .adc_step(STP, 4, 0, 0, 0) THEN _ ' configure fast ADC step
71  ?"failed setting trigger step (" & *.Errr & ")" : EXIT DO
72 
73 ' config OK here, transfer local settings to PRU and start PRU driver
74  VAR samp = S_W \ gap _ ' number of samples to fetch
75  , mask = (1 SHL 5) + (1 SHL 8) _ ' steps 5 & 8 active (AIN4, AIN7)
76  , tmr = 1e6 ' the sampling rate (1 kHz)
77  IF .config(samp, mask, tmr) THEN _
78  ?"config failed (" & *.Errr & ")" : EXIT DO
79 
80  VAR trg = 0
81  VAR trg1 = .mm_trg_pin(PIN) : IF 0 = trg1 THEN _
82  ?"trg1 spec failed (" & *.Errr & ")" : EXIT DO
83  VAR trg2 = .mm_trg_ain(STP, &h8000) : IF 0 = trg2 THEN _
84  ?"trg2 spec failed (" & *.Errr & ")" : EXIT DO
85  VAR trg3 = .mm_trg_pre(0, -&h8000, samp SHR 1) : IF 0 = trg3 THEN _
86  ?"trg3 spec failed (" & *.Errr & ")" : EXIT DO
87 
88  S_H -= 1
89  VAR lnr = IIF(S_H > 72, S_H SHR 3 - 8, 1) _ ' line number
90  , scale = S_H / 65520 _ ' scale sample to screen pixels
91  , k = 0 ' keycode for user input
92  DO ' loop to handle user actions
93  LOCATE lnr, 1, 0
94  ? ' print user menu
95  ?"Choose trigger type"
96  ?" 0 = no trigger (start immediately)"
97  ?" 1 = GPIO trigger (pin P8_07 low)"
98  ?" 2 = analog trigger, AIN4 > 0.9 V"
99  ?" 3 = analog pre-trigger, AIN4 < 0.9 V"
100  DO : SLEEP 1000, 0 : k = ASC(INKEY()) : LOOP UNTIL k ' get key
101 
102  SELECT CASE AS CONST k ' re-act on user keystrokes
103  CASE ASC("0") : trg = 0
104  CASE ASC("1") : trg = trg1 : ?"waiting for GPIO trigger (pin P8_07 low) ...";
105  CASE ASC("2") : trg = trg2 : ?"waiting for analog trigger (AIN4 > 0.9 V) ...";
106  CASE ASC("3") : trg = trg3 : ?"waiting for analog pre-trigger (any AIN < 0.9 V) ..." ;
107  CASE ELSE : EXIT DO
108  END SELECT
109 
110  IF .mm_start(trg) THEN ?"start failed (" & *.Errr & ")" : CONTINUE DO
111 
112  CLS
113  FOR c AS INTEGER = 0 TO .ChAz - 1
114  VAR i = c + .ChAz, x = gap
115  LINE (0, S_H - CUINT(.Value[c] * scale)) _
116  - (x, S_H - CUINT(.Value[i] * scale)), col(c)
117  DO
118  i += .ChAz : IF i >= .Samples THEN EXIT DO
119  x += gap
120  LINE - (x, S_H - CUINT(.Value[i] * scale)), col(c)
121  LOOP
122  NEXT
123  LOOP
124  LOOP UNTIL 1
125  IF .Errr THEN ?"press any key to quit" : SLEEP
126 END WITH
127 
128 DELETE io
129 
130