libpruio  0.2
Input/Output driver for digital/analog lines on Beagleboard hardware
 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: `fbc -w all triggers.bas`
14 
15 '/
16 
17 ' include libpruio
18 #INCLUDE ONCE "../pruio/pruio.bi"
19 ' include the convenience macros for header pins
20 #INCLUDE ONCE "../pruio/pruio_pins.bi"
21 ' include FreeBASIC grafics
22 #INCLUDE ONCE "fbgfx.bi"
23 
24 '* define the pin to use for digital trigger
25 #DEFINE PIN P8_07
26 '* define the step number to use for analog trigger
27 #DEFINE STP 11
28 
29 VAR S_W = 0 _ '*< The screen width.
30  , S_H = 0 _ '*< The srceen hight
31  , BPP = 0 _ '*< The bits per plain number.
32  , full = fb.GFX_FULLSCREEN '*< Fullscreen or windowed mode.
33 SCREENINFO S_W, S_H, BPP ' get screen resolution
34 IF LEN(COMMAND) THEN ' customized resolution required?
35  VAR p = INSTR(COMMAND, "x") _ '*< The position of the 'x' character (if any).
36  , w = VALINT(COMMAND) _ '*< The required window width.
37  , h = VALINT(MID(COMMAND, p + 1)) '*< The required window hight.
38  IF p ANDALSO w ANDALSO h THEN
39  IF w < S_W - 4 ANDALSO h < S_H - 24 THEN full = fb.GFX_WINDOWED
40  S_W = IIF(w < S_W, w, S_W) ' set maximum custom resolution
41  S_H = IIF(h < S_H, h, S_H)
42  ELSE
43  PRINT "set resolution like 640x400"
44  END
45  END IF
46 END IF
47 
48 SCREENRES S_W, S_H, BPP, 2, full ' set screen resolution
49 IF 0 = SCREENPTR THEN PRINT "no grafic available" : END
50 
51 '* The colors for the lines (= channels).
52 DIM AS UInt32 _
53  col(...) = { _
54  RGBA( 0, 0, 0, 255) _
55  , RGBA(255, 0, 0, 255) _
56  , RGBA( 0, 255, 0, 255) _
57  , RGBA( 0, 0, 255, 255) _
58  , RGBA(255, 255, 0, 255) _
59  , RGBA(255, 0, 255, 255) _
60  , RGBA( 0, 255, 255, 255) _
61  , RGBA(127, 127, 127, 255) _
62  }
63 
64 VAR io = NEW PruIo '*< Create a PruIo structure, wakeup subsystems.
65 
66 WITH *io
67  DO ' pseudo loop, just to avoid GOTOs
68  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
69 
70 
71  IF .Gpio->config(PIN, PRUIO_GPIO_IN_1) THEN _ ' configure GPIO pin
72  ?"failed setting trigger pin (" & *.Errr & ")" : EXIT DO
73 
74  IF .Adc->setStep(STP, 4, 0, 0, 0) THEN _ ' configure fast ADC step
75  ?"failed setting trigger step (" & *.Errr & ")" : EXIT DO
76 
77 ' config OK here, transfer local settings to PRU and start PRU driver
78  VAR gap = 2 _ '*< The gap between x values.
79  , samp = S_W \ gap _ '*< The number of samples to fetch.
80  , mask = (1 SHL 5) + (1 SHL 8) _ '*< Steps 5 & 8 active (AIN4, AIN7).
81  , tmr = 1e6 '*< The sampling rate (1 kHz).
82  IF .config(samp, mask, tmr) THEN _
83  ?"config failed (" & *.Errr & ")" : EXIT DO
84 
85  VAR trg = 0 '*< The current trigger value.
86  VAR trg1 = .Adc->mm_trg_pin(PIN) '*< A GPIO trigger specification.
87  IF 0 = trg1 THEN ?"trg1 spec failed (" & *.Errr & ")" : EXIT DO
88 
89  VAR trg2 = .Adc->mm_trg_ain(STP, &h8000) '*< An analog trigger specification.
90  IF 0 = trg2 THEN ?"trg2 spec failed (" & *.Errr & ")" : EXIT DO
91 
92  VAR trg3 = .Adc->mm_trg_pre(0, -&h8000, samp SHR 1) '*< A pre-trigger specification.
93  IF 0 = trg3 THEN ?"trg3 spec failed (" & *.Errr & ")" : EXIT DO
94 
95  S_W -= 1 : S_H -= 1
96  VAR lnr = IIF(S_H > 72, S_H SHR 3 - 8, 1) _ '*< The menue line number.
97  , max = .Adc->Samples - .Adc->ChAz _ '*< The max. index for samples.
98  , scale = S_H / 65520 _ '*< The factor to scale sample to screen pixels.
99  , k = 0 _ '*< The keycode of user input.
100  , fg = RGB(0, 0, 0) _ '*< The foreground color.
101  , bg = RGB(250, 250, 250) '*< The background color.
102  COLOR fg, bg
103  CLS
104  DO ' loop to handle user actions
105  LOCATE lnr, 1, 0
106  ? ' print user menu
107  ?"Choose trigger type"
108  ?" 0 = no trigger (start immediately)"
109  ?" 1 = GPIO trigger (pin P8_07 low)"
110  ?" 2 = analog trigger, AIN-4 > 0.9 V"
111  ?" 3 = analog pre-trigger, any AIN < 0.9 V"
112  DO : SLEEP 1000, 0 : k = ASC(INKEY()) : LOOP UNTIL k ' get key
113 
114  CLS
115  SELECT CASE AS CONST k ' re-act on user keystrokes
116  CASE ASC("0") : trg = 0 : ?"starting immediately ...";
117  CASE ASC("1") : trg = trg1 : ?"waiting for GPIO trigger (pin P8_07 low) ...";
118  CASE ASC("2") : trg = trg2 : ?"waiting for analog trigger (AIN-4 > 0.9 V) ...";
119  CIRCLE (0, S_H SHR 1), 5, RGB(200, 200, 200), , , 1, F
120  CASE ASC("3") : trg = trg3 : ?"waiting for analog pre-trigger (any AIN < 0.9 V) ..." ;
121  CIRCLE (S_W SHR 1, S_H SHR 1), 5, RGB(200, 200, 200), , , 1, F
122  CASE ELSE : EXIT DO
123  END SELECT
124 
125  IF .mm_start(trg) THEN ?"mm_start failed (" & *.Errr & ")" : CONTINUE DO
126 
127  line (0, 0)-STEP (S_W, 7), bg, BF
128  FOR c AS INTEGER = 0 TO .Adc->ChAz - 1 ' draw graph
129  VAR i = c + .Adc->ChAz _ '*< The samples index.
130  , x = gap '*< The x position at the screen.
131  LINE (0, S_H - CUINT(.Adc->Value[c] * scale)) _
132  - (x, S_H - CUINT(.Adc->Value[i] * scale)), col(c)
133  DO
134  i += .Adc->ChAz : IF i >= max THEN EXIT DO
135  x += gap
136  LINE - (x, S_H - CUINT(.Adc->Value[i] * scale)), col(c)
137  LOOP
138  NEXT
139  LOOP
140  LOOP UNTIL 1
141  IF .Errr THEN ?"press any key to quit" : SLEEP
142 END WITH
143 
144 DELETE io
145 
146 '' help Doxygen to dokument the main code
147 '&/** The main function. */
148 '&int main() {PruIo::PruIo(); GpioUdt::config(); AdcUdt::setStep(); PruIo::config(); AdcUdt::mm_trg_pin(); AdcUdt::mm_trg_ain(); AdcUdt::mm_trg_pre(); PruIo::~PruIo();}
149