Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Options_Engine/OptionsEngine.cpp@23:61fa82f76808, 2019-04-18 (annotated)
- Committer:
- JamesCummins
- Date:
- Thu Apr 18 22:56:34 2019 +0000
- Revision:
- 23:61fa82f76808
- Parent:
- 22:4e305ff8a050
- Child:
- 24:c6415cc74b17
Set ball speed issue fixed by creating a single ball object in top level file and referring back to that, rather than 2 different ones which don't communicate
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 22:4e305ff8a050 | 1 | #include "OptionsEngine.h" |
JamesCummins | 22:4e305ff8a050 | 2 | |
JamesCummins | 22:4e305ff8a050 | 3 | OptionsEngine::OptionsEngine(){ |
JamesCummins | 22:4e305ff8a050 | 4 | } |
JamesCummins | 22:4e305ff8a050 | 5 | |
JamesCummins | 22:4e305ff8a050 | 6 | OptionsEngine::~OptionsEngine(){ |
JamesCummins | 22:4e305ff8a050 | 7 | } |
JamesCummins | 22:4e305ff8a050 | 8 | |
JamesCummins | 22:4e305ff8a050 | 9 | void OptionsEngine::init(){ |
JamesCummins | 22:4e305ff8a050 | 10 | _state = BRIGHTNESS; |
JamesCummins | 22:4e305ff8a050 | 11 | _brightness = 0.5; |
JamesCummins | 22:4e305ff8a050 | 12 | |
JamesCummins | 22:4e305ff8a050 | 13 | } |
JamesCummins | 22:4e305ff8a050 | 14 | |
JamesCummins | 23:61fa82f76808 | 15 | void OptionsEngine::options_menu(Gamepad &gamepad, N5110 &lcd, Ball &ball){ |
JamesCummins | 22:4e305ff8a050 | 16 | Option choice = BRIGHTNESS; |
JamesCummins | 22:4e305ff8a050 | 17 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ |
JamesCummins | 22:4e305ff8a050 | 18 | lcd.clear(); |
JamesCummins | 22:4e305ff8a050 | 19 | display_options(lcd); |
JamesCummins | 22:4e305ff8a050 | 20 | choice = option_selection(gamepad, lcd); |
JamesCummins | 22:4e305ff8a050 | 21 | lcd.refresh(); |
JamesCummins | 22:4e305ff8a050 | 22 | wait(0.2); |
JamesCummins | 22:4e305ff8a050 | 23 | } |
JamesCummins | 22:4e305ff8a050 | 24 | if(choice == BRIGHTNESS){ change_brightness(gamepad, lcd); } |
JamesCummins | 23:61fa82f76808 | 25 | if(choice == BALL_SPEED){ change_ball_speed(gamepad, lcd, ball); } |
JamesCummins | 22:4e305ff8a050 | 26 | if(choice == HIGH_SCORES){ /*view_high_scores(gamepad, lcd);*/ } |
JamesCummins | 22:4e305ff8a050 | 27 | } |
JamesCummins | 22:4e305ff8a050 | 28 | |
JamesCummins | 22:4e305ff8a050 | 29 | void OptionsEngine::display_options(N5110 &lcd){ |
JamesCummins | 22:4e305ff8a050 | 30 | lcd.printString("Options menu", 6, 0); |
JamesCummins | 22:4e305ff8a050 | 31 | lcd.printString("Brightness", 12, 2); |
JamesCummins | 22:4e305ff8a050 | 32 | lcd.printString("Ball speed", 12, 3); |
JamesCummins | 22:4e305ff8a050 | 33 | lcd.printString("High scores", 9, 4); |
JamesCummins | 22:4e305ff8a050 | 34 | } |
JamesCummins | 22:4e305ff8a050 | 35 | |
JamesCummins | 22:4e305ff8a050 | 36 | Option OptionsEngine::option_selection(Gamepad &gamepad, N5110 &lcd){ |
JamesCummins | 22:4e305ff8a050 | 37 | OptionSelection fsm[3] = { |
JamesCummins | 22:4e305ff8a050 | 38 | {2,{HIGH_SCORES, BALL_SPEED, BRIGHTNESS}}, |
JamesCummins | 22:4e305ff8a050 | 39 | {3,{BRIGHTNESS, HIGH_SCORES, BALL_SPEED}}, |
JamesCummins | 22:4e305ff8a050 | 40 | {4,{BALL_SPEED, BRIGHTNESS, HIGH_SCORES}} |
JamesCummins | 22:4e305ff8a050 | 41 | }; |
JamesCummins | 22:4e305ff8a050 | 42 | if(gamepad.get_direction() == N){ _next_state = 0; } |
JamesCummins | 22:4e305ff8a050 | 43 | else if(gamepad.get_direction() == S){ _next_state = 1; } |
JamesCummins | 22:4e305ff8a050 | 44 | else{ _next_state = 2; } |
JamesCummins | 22:4e305ff8a050 | 45 | _state = fsm[_state].next_state[_next_state]; |
JamesCummins | 22:4e305ff8a050 | 46 | lcd.printChar('>', 0, fsm[_state].output); |
JamesCummins | 22:4e305ff8a050 | 47 | lcd.printChar('<', 78, fsm[_state].output); |
JamesCummins | 22:4e305ff8a050 | 48 | return _state; |
JamesCummins | 22:4e305ff8a050 | 49 | } |
JamesCummins | 22:4e305ff8a050 | 50 | |
JamesCummins | 22:4e305ff8a050 | 51 | void OptionsEngine::change_brightness(Gamepad &gamepad, N5110 &lcd){ |
JamesCummins | 22:4e305ff8a050 | 52 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ |
JamesCummins | 22:4e305ff8a050 | 53 | lcd.clear(); |
JamesCummins | 22:4e305ff8a050 | 54 | lcd.printString("Brightness", 12, 0); |
JamesCummins | 22:4e305ff8a050 | 55 | lcd.printString("Use L and R to", 0, 3); |
JamesCummins | 22:4e305ff8a050 | 56 | lcd.printString("change", 24, 4); |
JamesCummins | 22:4e305ff8a050 | 57 | lcd.printString("A = confirm", 9, 5); |
JamesCummins | 23:61fa82f76808 | 58 | lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT); |
JamesCummins | 22:4e305ff8a050 | 59 | read_brightness_input(gamepad); |
JamesCummins | 22:4e305ff8a050 | 60 | for(int i = 0; i < _brightness*10; i ++){ |
JamesCummins | 23:61fa82f76808 | 61 | lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); |
JamesCummins | 22:4e305ff8a050 | 62 | } |
JamesCummins | 22:4e305ff8a050 | 63 | lcd.setBrightness(_brightness); |
JamesCummins | 22:4e305ff8a050 | 64 | lcd.refresh(); |
JamesCummins | 22:4e305ff8a050 | 65 | wait(0.2); |
JamesCummins | 22:4e305ff8a050 | 66 | } |
JamesCummins | 22:4e305ff8a050 | 67 | } |
JamesCummins | 22:4e305ff8a050 | 68 | |
JamesCummins | 22:4e305ff8a050 | 69 | void OptionsEngine::read_brightness_input(Gamepad &gamepad){ |
JamesCummins | 23:61fa82f76808 | 70 | if(gamepad.check_event(gamepad.L_PRESSED)){ _brightness -= 0.1f; } //Use of f to explicitly convert to a float (to fit declaration type in header file). |
JamesCummins | 22:4e305ff8a050 | 71 | if(gamepad.check_event(gamepad.R_PRESSED)){ _brightness += 0.1f; } //Otherwise 0.1 is implicitly converted to a double (giving warning messages). |
JamesCummins | 22:4e305ff8a050 | 72 | if(_brightness < 0){ _brightness = 0; } |
JamesCummins | 22:4e305ff8a050 | 73 | if(_brightness > 1){ _brightness = 1; } |
JamesCummins | 23:61fa82f76808 | 74 | /*printf("Brightness = %f\n", _brightness);*/ |
JamesCummins | 22:4e305ff8a050 | 75 | } |
JamesCummins | 23:61fa82f76808 | 76 | |
JamesCummins | 23:61fa82f76808 | 77 | void OptionsEngine::change_ball_speed(Gamepad &gamepad, N5110 &lcd, Ball &ball){ |
JamesCummins | 23:61fa82f76808 | 78 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ |
JamesCummins | 23:61fa82f76808 | 79 | lcd.clear(); |
JamesCummins | 23:61fa82f76808 | 80 | lcd.printString("Ball Speed", 12, 0); |
JamesCummins | 23:61fa82f76808 | 81 | lcd.printString("Use L and R to", 0, 3); |
JamesCummins | 23:61fa82f76808 | 82 | lcd.printString("change", 24, 4); |
JamesCummins | 23:61fa82f76808 | 83 | lcd.printString("A = confirm", 9, 5); |
JamesCummins | 23:61fa82f76808 | 84 | lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT); |
JamesCummins | 23:61fa82f76808 | 85 | read_ball_speed_input(gamepad); |
JamesCummins | 23:61fa82f76808 | 86 | for(int i = 0; i < _ball_speed; i ++){ |
JamesCummins | 23:61fa82f76808 | 87 | lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); |
JamesCummins | 23:61fa82f76808 | 88 | } |
JamesCummins | 23:61fa82f76808 | 89 | ball.set_ball_speed(_ball_speed); |
JamesCummins | 23:61fa82f76808 | 90 | lcd.refresh(); |
JamesCummins | 23:61fa82f76808 | 91 | wait(0.2); |
JamesCummins | 23:61fa82f76808 | 92 | } |
JamesCummins | 23:61fa82f76808 | 93 | } |
JamesCummins | 23:61fa82f76808 | 94 | |
JamesCummins | 23:61fa82f76808 | 95 | void OptionsEngine::read_ball_speed_input(Gamepad &gamepad){ |
JamesCummins | 23:61fa82f76808 | 96 | if(gamepad.check_event(gamepad.L_PRESSED)){ _ball_speed -= 1; } |
JamesCummins | 23:61fa82f76808 | 97 | if(gamepad.check_event(gamepad.R_PRESSED)){ _ball_speed += 1; } |
JamesCummins | 23:61fa82f76808 | 98 | if(_ball_speed < 0){ _ball_speed = 0; } |
JamesCummins | 23:61fa82f76808 | 99 | if(_ball_speed > 10){ _ball_speed = 10; } |
JamesCummins | 23:61fa82f76808 | 100 | } |