libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
pwm_adc.bas
Go to the documentation of this file.
1 
17 
18 ' include libpruio
19 #INCLUDE ONCE "BBB/pruio.bi"
20 ' include the convenience macros for header pins
21 #INCLUDE ONCE "BBB/pruio_pins.bi"
22 ' include FB grafics
23 #INCLUDE ONCE "fbgfx.bi"
24 
25 VAR S_W = 0 _
26  , S_H = 0 _
27  , BPP = 0 _
28  , full = fb.GFX_FULLSCREEN
29 SCREENINFO S_W, S_H, BPP ' get screen resolution
30 
31 IF LEN(COMMAND) THEN ' customized resolution required?
32  VAR p = INSTR(COMMAND, "x") _
33  , w = VALINT(COMMAND) _
34  , h = VALINT(MID(COMMAND, p + 1))
35  IF p ANDALSO w ANDALSO h THEN
36  IF w < S_W - 4 ANDALSO h < S_H - 24 THEN full = fb.GFX_WINDOWED
37  S_W = IIF(w < S_W, w, S_W) ' set maximum custom resolution
38  S_H = IIF(h < S_H, h, S_H)
39  ELSE
40  PRINT "set resolution like 640x400"
41  END
42  END IF
43 END IF
44 
45 SCREENRES S_W, S_H, BPP, 2, full ' set screen resolution
46 IF 0 = SCREENPTR THEN PRINT "no grafic available" : END
47 
48 
49 DIM AS UInt32 _
50  col(...) = { _
51  RGBA(255, 0, 0, 255) _
52  , RGBA( 0, 255, 0, 255) _
53  , RGBA( 0, 0, 255, 255) _
54  }
55 
56 
57 DIM AS UInt32 _
58  last(...) = { _
59  0 _
60  , 0 _
61  , 0 _
62  , 0 _
63  , 0 _
64  , 0 _
65  , 0 _
66  , 0 _
67  }
68 
69 
70 TYPE PWM_PIN
71  AS UInt8 _
72  Pin
73  AS Float_t _
74  Freq _
75  , Duty
76  AS ZSTRING PTR _
77  Nam
78 END TYPE
79 
80 
81 DIM AS PWM_PIN ch(...) = { _
82  TYPE<PWM_PIN>(P9_14, 2.5, .5, @"P9_14 (A)") _
83  , TYPE<PWM_PIN>(P9_16, 2.5, .2, @"P9_16 (B)") _
84  , TYPE<PWM_PIN>(P9_42, 2.5, .8, @"P9_42 (C)") _
85  }
86 
87 VAR io = NEW PruIo
88 
89 WITH *io
90  DO
91  IF .Errr THEN ?"NEW failed: " & *.Errr : EXIT DO
92 
93  FOR i AS INTEGER = 0 TO UBOUND(ch) ' configure PWM pins
94  IF .Pwm->setValue(ch(i).Pin, ch(i).Freq, ch(i).Duty) THEN _
95  ?"failed setting " & *ch(i).nam & " (" & *.Errr & ")" : EXIT DO
96  NEXT
97 
98  IF .config(1, &b1110) THEN _ ' configure steps 1, 2, 3 (AIN-[0,1,2])
99  ?"config failed: " & *.Errr & " --> " & .DRam[0] : SLEEP : EXIT DO
100 
101  VAR scale = S_H / 65520 _
102  , x = 0 _
103  , gap = 1 _
104  , xmax = S_W - gap - 1 _
105  , m = 0 _
106  , n = 2 _
107  , fg = RGB(0, 0, 0) _
108  , bg = RGB(250, 250, 250)
109  FOR i AS INTEGER = 0 TO UBOUND(ch) ' get start values
110  last(i) = S_H - CUINT(.Adc->Value[i + 1] * scale)
111  NEXT
112 
113  WINDOWTITLE(*ch(n).nam _
114  & ": Frequency = " & ch(n).Freq & " Hz" _
115  & ", Duty = " & (ch(n).Duty) * 100) & "%"
116  COLOR fg, bg
117  CLS
118  DO
119  VAR k = ASC(INKEY())
120  IF k THEN
121  SELECT CASE AS CONST k ' react on user keystrokes
122  CASE ASC("A"), ASC("a") : m = BITRESET(&hFF, 0): n = 256 : ch(0).Freq = ch(1).Freq 'PWMSS 1, output PWM-A (P9_14)
123  CASE ASC("B"), ASC("b") : m = BITRESET(&hFF, 1): n = 257 : ch(1).Freq = ch(0).Freq 'PWMSS 1, output PWM-B (P9_16)
124  CASE ASC("C"), ASC("c") : m = BITRESET(&hFF, 2): n = 258 'PWMSS 0, output eCAP (P9_42)
125  CASE ASC("0") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.0
126  CASE ASC("1") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.1
127  CASE ASC("2") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.2
128  CASE ASC("3") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.3
129  CASE ASC("4") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.4
130  CASE ASC("5") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.5
131  CASE ASC("6") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.6
132  CASE ASC("7") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.7
133  CASE ASC("8") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.8
134  CASE ASC("9") : m = BITRESET(&hFF, n) : ch(n).Duty = 0.9
135  CASE ASC(","), ASC(".") : m = BITRESET(&hFF, n) : ch(n).Duty = 1.0
136  CASE ASC("+") : IF ch(n).Freq < 5.0 THEN ch(n).Freq += .5
137  CASE ASC("-") : IF ch(n).Freq > 0.9 THEN ch(n).Freq -= .5
138  CASE ASC("*") : ch(n).Freq = 5.0
139  CASE ASC("/") : ch(n).Freq = .5
140  CASE 13 : m = 0 : n += 256
141  CASE ELSE : EXIT DO ' finish
142  END SELECT
143 
144  IF n > UBOUND(ch) THEN
145  n -= 256
146  ELSE
147  IF .Pwm->setValue(ch(n).Pin, ch(n).Freq, ch(n).Duty) THEN _
148  ?"failed setting PWM value (" & *.Errr & ")" : EXIT DO
149  END IF
150 
151  WINDOWTITLE(*ch(n).nam _
152  & ": Frequency = " & ch(n).Freq & " Hz" _
153  & ", Duty = " & (ch(n).Duty) * 100) & "%"
154  END IF
155 
156  LINE (x + 1, 0) - STEP (gap, S_H), bg, BF
157  FOR i AS INTEGER = 0 TO UBOUND(ch) ' draw lines
158  IF BIT(m, i) THEN CONTINUE FOR
159  VAR neu = S_H - CUINT(.Adc->Value[i + 1] * scale)
160  LINE (x, last(i)) - (x + gap, neu), col(i)
161  last(i) = neu
162  LINE (0, 0) - STEP (gap, S_H), 0, BF
163  NEXT
164  x += gap : IF x > xmax THEN x = 0
165  SLEEP 1
166  LOOP : ?
167  LOOP UNTIL 1
168  IF .Errr THEN SLEEP
169 END WITH
170 
171 DELETE(io)
172 
173 '' help Doxygen to document the main code
174 '&/** The main function. */
175 '&int main() {PruIo::PruIo(); PwmMod::setValue(); PruIo::config(); PruIo::~PruIo();}
176