📄 main.lst
字号:
143 * @param current_state The current state of the statemachine
144 * @return The next state. Returns @b current_state if state_machine
145 * indicated @c NO_CHANGE
146 */
147 static state_t get_next_state(state_t current_state);
148
149 /** Function that runs in a loop until all buttons are released.
150 */
151 static void wait_for_button_release(void);
152
153 /** Shows the state the state_machine is in.
154 */
155 static void show_status(state_t operation);
156
157 /** Function that initialises everything. Calls @b system_init () which is
158 * hardware dependant, and @b device_boot_msg () from @b system.c.
159 * It implementes a simple statemachine to handle the input from the user on
160 * the evaluation board. With two clicks, the user can choose between
161 * primary transmitter mode (PTX) and primary reciever mode (PRX), and between
162 * the functionality levels ShockBurst (sb), Enchanced ShockBurst,
163 * and Enhanced ShockBurst with Bidirectional data (pl).
164 */
165 void main(void)
166 {
167 1 state_t current_state = DEVICE_IDLE;
168 1
169 1 system_init(); //Hardware dependant system initialisation
170 1 device_boot_msg(); //Flashes LED's in a simple pattern
171 1
172 1 GLOBAL_INT_ENABLE(); //Ensure that all interupts are turned on
173 1
174 1 LED_ALL_OFF(); //Turn off all lights
175 1
176 1 wait_for_button_release (); //Ensure that all buttons are released
177 1
178 1 //Implemenation of a simple state machine.
179 1 while (true)
180 1 {
C51 COMPILER V8.08 MAIN 01/02/2009 11:50:56 PAGE 7
181 2 current_state = get_next_state (current_state);// Go to next state
182 2 wait_for_button_release (); // Ensure that all
183 2 // buttons are released
184 2 show_status (current_state);
185 2
186 2 switch (current_state)
187 2 {
188 3 case DEVICE_IDLE: // No operation chosen yet
189 3 break;
190 3
191 3 case DEVICE_PRX_IDLE: // In PRX mode, but still lack
192 3 break; // functionality
193 3
194 3 case DEVICE_PTX_IDLE: // In PTX mode, but still lack
195 3 break; // functionality
196 3
197 3 case DEVICE_PRX_SB: // Start as PRX in ShockBurst
198 3 radio_sb_init (address, HAL_NRF_PRX);
199 3 device_prx_mode_sb ();
200 3 break;
201 3
202 3 case DEVICE_PRX_ESB: // Start as PRX in Enhanced
203 3 radio_esb_init (address, HAL_NRF_PRX);// ShockBurst
204 3 device_prx_mode_esb ();
205 3 break;
206 3
207 3 case DEVICE_PRX_PL: //Start as PRX in Enhanced
208 3 radio_pl_init (address, HAL_NRF_PRX); //ShockBurst with ACK payload
209 3 device_prx_mode_pl ();
210 3 break;
211 3
212 3 case DEVICE_PTX_SB: //Start as PTX in ShockBurst
213 3 radio_sb_init (address, HAL_NRF_PTX);
214 3 device_ptx_mode_sb ();
215 3 break;
216 3
217 3 case DEVICE_PTX_ESB: //Start as PTX in Enhanced
218 3 radio_esb_init (address, HAL_NRF_PTX);//ShockBurst
219 3 device_ptx_mode_esb ();
220 3 break;
221 3
222 3 case DEVICE_PTX_PL: // Start as PTX in Enhanced
223 3 radio_pl_init (address, HAL_NRF_PTX); // ShockBurst with ACK payload
224 3 device_ptx_mode_pl ();
225 3 break;
226 3
227 3 default: // If in an illegal state, set to
228 3 current_state = DEVICE_IDLE; // default state (DEVICE_IDLE)
229 3 break;
230 3 }
231 2 }
232 1 }
233
234 static state_t get_next_state (state_t current_state)
235 {
236 1 state_t next_state = NO_CHANGE;
237 1
238 1 if (B1_PRESSED()) // Swap state according to state_machine
239 1 { // array with button input and
240 2 // current_state as input
241 2 next_state = state_machine[current_state][0];
242 2 }
C51 COMPILER V8.08 MAIN 01/02/2009 11:50:56 PAGE 8
243 1 else if (B2_PRESSED())
244 1 {
245 2 next_state = state_machine[current_state][1];
246 2 }
247 1 else if (B3_PRESSED())
248 1 {
249 2 next_state = state_machine[current_state][2];
250 2 }
251 1
252 1 if (next_state == NO_CHANGE) // If no statechange should occur, return
253 1 { // previous state
254 2 next_state = current_state;
255 2 }
256 1 else // As it takes some time for the button to
257 1 { // stabalise as pressed, give it a short
258 2 delay_10ms(); // delay to stabalise
259 2 }
260 1
261 1 return next_state;
262 1 }
263
264 static void wait_for_button_release (void)
265 {
266 1 while (B1_PRESSED() || B2_PRESSED() || B3_PRESSED()) // Wait until all
267 1 ; // buttons are released
268 1 delay_10ms(); // Delay to stabalise
269 1 }
270
271 static void show_status (state_t operation)
272 {
273 1 uint16_t time;
274 1 LED_ALL_OFF();
275 1
276 1 if (show_state[operation][0] == ON)
277 1 {
278 2 LED1_ON();
279 2 }
280 1 if (show_state[operation][1] == ON)
281 1 {
282 2 LED2_ON();
283 2 }
284 1 if (show_state[operation][2] == ON)
285 1 {
286 2 LED3_ON();
287 2 }
288 1
289 1 // If there is to be a delay where LED's are shown, but no input is
290 1 // accepted, delay for the period indicated in show_state[operation][4]
291 1 if (show_state[operation][4] > 0)
292 1 {
293 2 time = (uint16_t)(show_state[operation][4] * 100);
294 2 start_timer(time);
295 2 wait_for_timer();
296 2 }
297 1
298 1 // If the radio goes into an operational mode, all LED's should be turned off
299 1 // before entering that mode
300 1 if (show_state[operation][3] == OFF)
301 1 {
302 2 LED_ALL_OFF();
303 2 }
304 1 }
C51 COMPILER V8.08 MAIN 01/02/2009 11:50:56 PAGE 9
C51 COMPILATION COMPLETE. 0 WARNING(S), 180 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -