libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
triggers.bas
Go to the documentation of this file.
1 
15 
16 ' include libpruio
17 #INCLUDE ONCE "BBB/pruio.bi"
18 ' include the convenience macros for header pins
19 #INCLUDE ONCE "BBB/pruio_pins.bi"
20 ' include FreeBASIC grafics
21 #INCLUDE ONCE "fbgfx.bi"
22 
23 
24 #DEFINE PIN P8_07
25 
26 #DEFINE STP 11
27 
28 VAR S_W = 0 _
29  , S_H = 0 _
30  , BPP = 0 _
31  , full = fb.GFX_FULLSCREEN
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  IF w < S_W - 4 ANDALSO h < S_H - 24 THEN full = fb.GFX_WINDOWED
39  S_W = IIF(w < S_W, w, S_W) ' set maximum custom resolution
40  S_H = IIF(h < S_H, h, S_H)
41  ELSE
42  PRINT "set resolution like 640x400"
43  END
44  END IF
45 END IF
46 
47 SCREENRES S_W, S_H, BPP, 2, full ' set screen resolution
48 IF 0 = SCREENPTR THEN PRINT "no grafic available" : END
49 
50 
51 DIM AS UInt32 _
52  col(...) = { _
53  RGBA( 0, 0, 0, 255) _
54  , RGBA(255, 0, 0, 255) _
55  , RGBA( 0, 255, 0, 255) _
56  , RGBA( 0, 0, 255, 255) _
57  , RGBA(255, 255, 0, 255) _
58  , RGBA(255, 0, 255, 255) _
59  , RGBA( 0, 255, 255, 255) _
60  , RGBA(127, 127, 127, 255) _
61  }
62 
63 VAR io = NEW PruIo
64 
65 WITH *io
66  DO ' pseudo loop, just to avoid GOTOs
67  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
68 
69 
70  IF .Gpio->config(PIN, PRUIO_GPIO_IN_1) THEN _ ' configure GPIO pin
71  ?"failed setting trigger pin (" & *.Errr & ")" : EXIT DO
72 
73  IF .Adc->setStep(STP, 4, 0, 0, 0) THEN _ ' configure fast ADC step
74  ?"failed setting trigger step (" & *.Errr & ")" : EXIT DO
75 
76 ' config OK here, transfer local settings to PRU and start PRU driver
77  VAR gap = 2 _
78  , samp = S_W \ gap _
79  , mask = (1 SHL 5) + (1 SHL 8) _
80  , tmr = 1e6
81  IF .config(samp, mask, tmr) THEN _
82  ?"config failed (" & *.Errr & ")" : EXIT DO
83 
84  VAR trg = 0
85  VAR trg1 = .Adc->mm_trg_pin(PIN)
86  IF 0 = trg1 THEN ?"trg1 spec failed (" & *.Errr & ")" : EXIT DO
87 
88  VAR trg2 = .Adc->mm_trg_ain(STP, &h8000)
89  IF 0 = trg2 THEN ?"trg2 spec failed (" & *.Errr & ")" : EXIT DO
90 
91  VAR trg3 = .Adc->mm_trg_pre(0, -&h8000, samp SHR 1)
92  IF 0 = trg3 THEN ?"trg3 spec failed (" & *.Errr & ")" : EXIT DO
93 
94  S_W -= 1 : S_H -= 1
95  VAR lnr = IIF(S_H > 72, S_H SHR 3 - 8, 1) _
96  , max = .Adc->Samples - .Adc->ChAz _
97  , scale = S_H / 65520 _
98  , k = 0 _
99  , fg = RGB(0, 0, 0) _
100  , bg = RGB(250, 250, 250)
101  COLOR fg, bg
102  CLS
103  DO ' loop to handle user actions
104  LOCATE lnr, 1, 0
105  ? ' print user menu
106  ?"Choose trigger type"
107  ?" 0 = no trigger (start immediately)"
108  ?" 1 = GPIO trigger (pin P8_07 low)"
109  ?" 2 = analog trigger, AIN-4 > 0.9 V"
110  ?" 3 = analog pre-trigger, any AIN < 0.9 V"
111  DO : SLEEP 1000, 0 : k = ASC(INKEY()) : LOOP UNTIL k ' get key
112 
113  CLS
114  SELECT CASE AS CONST k ' re-act on user keystrokes
115  CASE ASC("0") : trg = 0 : ?"starting immediately ...";
116  CASE ASC("1") : trg = trg1 : ?"waiting for GPIO trigger (pin P8_07 low) ...";
117  CASE ASC("2") : trg = trg2 : ?"waiting for analog trigger (AIN-4 > 0.9 V) ...";
118  CIRCLE (0, S_H SHR 1), 5, RGB(200, 200, 200), , , 1, F
119  CASE ASC("3") : trg = trg3 : ?"waiting for analog pre-trigger (any AIN < 0.9 V) ..." ;
120  CIRCLE (S_W SHR 1, S_H SHR 1), 5, RGB(200, 200, 200), , , 1, F
121  CASE ELSE : EXIT DO
122  END SELECT
123 
124  IF .mm_start(trg) THEN ?"mm_start failed (" & *.Errr & ")" : CONTINUE DO
125 
126  LINE (0, 0)-STEP (S_W, S_H), bg, BF
127  FOR c AS INTEGER = 0 TO .Adc->ChAz - 1 ' draw graph
128  VAR i = c + .Adc->ChAz _
129  , x = gap
130  LINE (0, S_H - CUINT(.Adc->Value[c] * scale)) _
131  - (x, S_H - CUINT(.Adc->Value[i] * scale)), col(c)
132  DO
133  i += .Adc->ChAz : IF i >= max THEN EXIT DO
134  x += gap
135  LINE - (x, S_H - CUINT(.Adc->Value[i] * scale)), col(c)
136  LOOP
137  NEXT
138  LOOP
139  LOOP UNTIL 1
140  IF .Errr THEN ?"press any key to quit" : SLEEP
141 END WITH
142 
143 DELETE io
144 
145 '' help Doxygen to document the main code
146 '&/** The main function. */
147 '&int main() {PruIo::PruIo(); GpioUdt::config(); AdcUdt::setStep(); PruIo::config(); AdcUdt::mm_trg_pin(); AdcUdt::mm_trg_ain(); AdcUdt::mm_trg_pre(); PruIo::~PruIo();}
148