Mercurial > hg > beaglert
comparison examples/stepper/render.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/stepper/render.cpp@8d80eda512cd |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
1 /* | |
2 * render.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 | |
9 #include <BeagleRT.h> | |
10 #include <Utilities.h> | |
11 | |
12 const int kStepLengthSlow = 1000; | |
13 const int kStepLengthFast = 500; | |
14 | |
15 int gStepLengthSamples = kStepLengthSlow; | |
16 | |
17 const int gPinA1 = P8_27; | |
18 const int gPinA2 = P8_28; | |
19 const int gPinB1 = P8_29; | |
20 const int gPinB2 = P8_30; | |
21 const int gPinServo = P9_16; | |
22 | |
23 int gStepCounter = 0; | |
24 int gPhase = 0; | |
25 | |
26 int gServoCounter = 0; | |
27 | |
28 | |
29 enum { | |
30 kStateMoveRight1 = 0, | |
31 kStateMoveLeft1, | |
32 kStateMoveRight2, | |
33 kStateMoveLeft2, | |
34 kStateMoveRight3, | |
35 kStateMoveLeft3, | |
36 kStateSpin, | |
37 kStateMax | |
38 }; | |
39 | |
40 int gState = 0; | |
41 int gStateCounter = 0; | |
42 | |
43 // setup() is called once before the audio rendering starts. | |
44 // Use it to perform any initialisation and allocation which is dependent | |
45 // on the period size or sample rate. | |
46 // | |
47 // userData holds an opaque pointer to a data structure that was passed | |
48 // in from the call to initAudio(). | |
49 // | |
50 // Return true on success; returning false halts the program. | |
51 | |
52 bool setup(BeagleRTContext *context, void *userData) | |
53 { | |
54 // This project makes the assumption that the audio and digital | |
55 // sample rates are the same. But check it to be sure! | |
56 if(context->audioFrames != context->digitalFrames) { | |
57 rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n"); | |
58 return false; | |
59 } | |
60 | |
61 pinModeFrame(context, 0, gPinA1, OUTPUT); | |
62 pinModeFrame(context, 0, gPinA2, OUTPUT); | |
63 pinModeFrame(context, 0, gPinB1, OUTPUT); | |
64 pinModeFrame(context, 0, gPinB2, OUTPUT); | |
65 pinModeFrame(context, 0, gPinServo, OUTPUT); | |
66 | |
67 return true; | |
68 } | |
69 | |
70 // render() is called regularly at the highest priority by the audio engine. | |
71 // Input and output are given from the audio hardware and the other | |
72 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | |
73 // will be 0. | |
74 | |
75 void render(BeagleRTContext *context, void *userData) | |
76 { | |
77 for(unsigned int n = 0; n < context->audioFrames; n++) { | |
78 if(gPhase == 0 || gPhase == 1) { | |
79 digitalWriteFrameOnce(context, n, gPinB1, HIGH); | |
80 digitalWriteFrameOnce(context, n, gPinB2, LOW); | |
81 } | |
82 else { | |
83 digitalWriteFrameOnce(context, n, gPinB1, LOW); | |
84 digitalWriteFrameOnce(context, n, gPinB2, HIGH); | |
85 } | |
86 | |
87 if(gPhase == 1 || gPhase == 2) { | |
88 digitalWriteFrameOnce(context, n, gPinA1, HIGH); | |
89 digitalWriteFrameOnce(context, n, gPinA2, LOW); | |
90 } | |
91 else { | |
92 digitalWriteFrameOnce(context, n, gPinA1, LOW); | |
93 digitalWriteFrameOnce(context, n, gPinA2, HIGH); | |
94 } | |
95 | |
96 if(--gServoCounter > 0) | |
97 digitalWriteFrameOnce(context, n, gPinServo, HIGH); | |
98 else | |
99 digitalWriteFrameOnce(context, n, gPinServo, LOW); | |
100 | |
101 if(++gStepCounter >= gStepLengthSamples) { | |
102 gStateCounter++; | |
103 | |
104 switch(gState) { | |
105 case kStateMoveRight1: | |
106 case kStateMoveRight2: | |
107 case kStateMoveRight3: | |
108 gPhase = (gPhase + 1) & 3; | |
109 break; | |
110 case kStateMoveLeft1: | |
111 case kStateMoveLeft2: | |
112 case kStateMoveLeft3: | |
113 gPhase = (gPhase + 3) & 3; | |
114 break; | |
115 case kStateSpin: | |
116 gPhase = (gPhase + 1) & 3; | |
117 break; | |
118 } | |
119 | |
120 if(gState == kStateSpin) { | |
121 if(gStateCounter >= 48) { | |
122 gStateCounter = 0; | |
123 gState = 0; | |
124 gStepLengthSamples = kStepLengthSlow; | |
125 } | |
126 } | |
127 else { | |
128 if(gStateCounter >= 16) { | |
129 gStateCounter = 0; | |
130 gState++; | |
131 if(gState & 1) | |
132 gServoCounter = 120; | |
133 else | |
134 gServoCounter = 80; | |
135 if(gState == kStateSpin) | |
136 gStepLengthSamples = kStepLengthFast; | |
137 } | |
138 } | |
139 | |
140 gStepCounter = 0; | |
141 } | |
142 } | |
143 } | |
144 | |
145 // cleanup() is called once at the end, after the audio has stopped. | |
146 // Release any resources that were allocated in setup(). | |
147 | |
148 void cleanup(BeagleRTContext *context, void *userData) | |
149 { | |
150 | |
151 } |