State machine
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
Screen.h@50:5d2176b93a65, 2018-11-05 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Mon Nov 05 10:14:31 2018 +0000
- Revision:
- 50:5d2176b93a65
- Parent:
- 42:cafa56da9ed2
With new comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brass_phoenix | 3:4b19b6cf6cc7 | 1 | #pragma once |
brass_phoenix | 3:4b19b6cf6cc7 | 2 | |
brass_phoenix | 3:4b19b6cf6cc7 | 3 | #include "mbed.h" |
brass_phoenix | 4:5a44ab7e94b3 | 4 | #include <string> |
brass_phoenix | 3:4b19b6cf6cc7 | 5 | |
brass_phoenix | 3:4b19b6cf6cc7 | 6 | // Uncomment this in the adafruit config.h to turn off the builtin splash |
brass_phoenix | 3:4b19b6cf6cc7 | 7 | //#define NO_SPLASH_ADAFRUIT |
brass_phoenix | 3:4b19b6cf6cc7 | 8 | // Uncomment this in the adafruit config.h to enable all functionality |
brass_phoenix | 3:4b19b6cf6cc7 | 9 | //#define GFX_WANT_ABSTRACTS |
brass_phoenix | 3:4b19b6cf6cc7 | 10 | |
brass_phoenix | 3:4b19b6cf6cc7 | 11 | #include "Adafruit_SSD1306.h" |
brass_phoenix | 3:4b19b6cf6cc7 | 12 | |
brass_phoenix | 3:4b19b6cf6cc7 | 13 | |
brass_phoenix | 3:4b19b6cf6cc7 | 14 | class Screen |
brass_phoenix | 3:4b19b6cf6cc7 | 15 | { |
brass_phoenix | 3:4b19b6cf6cc7 | 16 | private: |
MAHCSnijders | 50:5d2176b93a65 | 17 | I2C i2c; // Communicates to the OLED screen |
brass_phoenix | 3:4b19b6cf6cc7 | 18 | Adafruit_SSD1306_I2c oled; |
brass_phoenix | 3:4b19b6cf6cc7 | 19 | public: |
brass_phoenix | 3:4b19b6cf6cc7 | 20 | // The screen we use doesn't have a reset pin, but it needs one anyway. |
brass_phoenix | 3:4b19b6cf6cc7 | 21 | // Use a pin we don't use. |
brass_phoenix | 3:4b19b6cf6cc7 | 22 | Screen(PinName sda, PinName scl, PinName reset): |
brass_phoenix | 3:4b19b6cf6cc7 | 23 | i2c(sda, scl), |
brass_phoenix | 3:4b19b6cf6cc7 | 24 | oled(i2c, reset) { |
brass_phoenix | 3:4b19b6cf6cc7 | 25 | |
MAHCSnijders | 50:5d2176b93a65 | 26 | // According to the spec the max bitrate for the SSD1308 is 400 kbit/s |
brass_phoenix | 3:4b19b6cf6cc7 | 27 | i2c.frequency(400000); |
brass_phoenix | 3:4b19b6cf6cc7 | 28 | |
brass_phoenix | 4:5a44ab7e94b3 | 29 | clear_display(); |
brass_phoenix | 4:5a44ab7e94b3 | 30 | } |
brass_phoenix | 4:5a44ab7e94b3 | 31 | |
brass_phoenix | 4:5a44ab7e94b3 | 32 | void clear_display() { |
brass_phoenix | 3:4b19b6cf6cc7 | 33 | oled.clearDisplay(); |
brass_phoenix | 17:1fe276ff17db | 34 | oled.setTextCursor(0, 0); |
brass_phoenix | 3:4b19b6cf6cc7 | 35 | display(); |
brass_phoenix | 3:4b19b6cf6cc7 | 36 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 37 | |
brass_phoenix | 3:4b19b6cf6cc7 | 38 | void display() { |
brass_phoenix | 3:4b19b6cf6cc7 | 39 | // Apparently the display only updates every 2 "display" calls. |
brass_phoenix | 3:4b19b6cf6cc7 | 40 | // This is a bug in the library. |
brass_phoenix | 3:4b19b6cf6cc7 | 41 | oled.display(); |
brass_phoenix | 3:4b19b6cf6cc7 | 42 | oled.display(); |
brass_phoenix | 3:4b19b6cf6cc7 | 43 | } |
brass_phoenix | 4:5a44ab7e94b3 | 44 | |
brass_phoenix | 5:2632dfc8454c | 45 | // State names can be a maximum of 17 characters long. |
brass_phoenix | 4:5a44ab7e94b3 | 46 | void display_state_name(string name) { |
brass_phoenix | 4:5a44ab7e94b3 | 47 | // Clear a previous title. |
brass_phoenix | 4:5a44ab7e94b3 | 48 | oled.setTextCursor(0, 0); |
brass_phoenix | 5:2632dfc8454c | 49 | oled.printf(" "); |
brass_phoenix | 4:5a44ab7e94b3 | 50 | oled.setTextCursor(0, 0); |
brass_phoenix | 4:5a44ab7e94b3 | 51 | oled.printf("- "); |
brass_phoenix | 4:5a44ab7e94b3 | 52 | oled.printf(name.c_str()); |
brass_phoenix | 6:bfc6e68774f5 | 53 | oled.printf(" -\r\n"); |
brass_phoenix | 4:5a44ab7e94b3 | 54 | display(); |
brass_phoenix | 4:5a44ab7e94b3 | 55 | } |
brass_phoenix | 42:cafa56da9ed2 | 56 | |
brass_phoenix | 42:cafa56da9ed2 | 57 | void display_emg_state(bool left_tensed, bool right_tensed) { |
brass_phoenix | 42:cafa56da9ed2 | 58 | oled.setTextCursor(0, 8); |
brass_phoenix | 42:cafa56da9ed2 | 59 | if (left_tensed) { |
brass_phoenix | 42:cafa56da9ed2 | 60 | oled.printf("Left "); |
brass_phoenix | 42:cafa56da9ed2 | 61 | } else { |
brass_phoenix | 42:cafa56da9ed2 | 62 | oled.printf(" "); |
brass_phoenix | 42:cafa56da9ed2 | 63 | } |
brass_phoenix | 42:cafa56da9ed2 | 64 | if (right_tensed) { |
brass_phoenix | 42:cafa56da9ed2 | 65 | oled.printf("Right"); |
brass_phoenix | 42:cafa56da9ed2 | 66 | } else { |
brass_phoenix | 42:cafa56da9ed2 | 67 | oled.printf(" "); |
brass_phoenix | 42:cafa56da9ed2 | 68 | } |
brass_phoenix | 42:cafa56da9ed2 | 69 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 70 | |
brass_phoenix | 3:4b19b6cf6cc7 | 71 | // Returns a direct handle to the screen. |
brass_phoenix | 3:4b19b6cf6cc7 | 72 | // Be carefull while using the handle. |
brass_phoenix | 6:bfc6e68774f5 | 73 | Adafruit_SSD1306_I2c* get_screen_handle() { |
brass_phoenix | 6:bfc6e68774f5 | 74 | return &oled; |
brass_phoenix | 3:4b19b6cf6cc7 | 75 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 76 | |
brass_phoenix | 15:f65b4566193e | 77 | void display_up_down_arrow(bool is_up) { |
brass_phoenix | 25:cc81f2120eda | 78 | int size_x = 20; |
brass_phoenix | 25:cc81f2120eda | 79 | int size_y = 10; |
brass_phoenix | 25:cc81f2120eda | 80 | int x_start = oled.width() - size_x; |
brass_phoenix | 25:cc81f2120eda | 81 | int x_end = x_start + size_x; |
brass_phoenix | 25:cc81f2120eda | 82 | int y_start = oled.height() - size_y; |
brass_phoenix | 25:cc81f2120eda | 83 | int y_end = y_start + size_y; |
brass_phoenix | 25:cc81f2120eda | 84 | |
brass_phoenix | 25:cc81f2120eda | 85 | oled.fillRect(x_start, y_start, size_x, size_y, BLACK); |
brass_phoenix | 25:cc81f2120eda | 86 | |
brass_phoenix | 25:cc81f2120eda | 87 | if (is_up) { |
brass_phoenix | 25:cc81f2120eda | 88 | oled.fillTriangle(x_start, y_end, x_end, y_end, x_start + size_x/2, y_start, WHITE); |
brass_phoenix | 25:cc81f2120eda | 89 | } else { |
brass_phoenix | 25:cc81f2120eda | 90 | oled.fillTriangle(x_start + size_x/2, y_end, x_start, y_start, x_end, y_start, WHITE); |
brass_phoenix | 25:cc81f2120eda | 91 | } |
brass_phoenix | 15:f65b4566193e | 92 | |
brass_phoenix | 15:f65b4566193e | 93 | display(); |
brass_phoenix | 15:f65b4566193e | 94 | } |
brass_phoenix | 15:f65b4566193e | 95 | |
brass_phoenix | 25:cc81f2120eda | 96 | void display_left_right_arrow(bool is_right) { |
brass_phoenix | 25:cc81f2120eda | 97 | int size_x = 20; |
brass_phoenix | 25:cc81f2120eda | 98 | int size_y = 10; |
brass_phoenix | 25:cc81f2120eda | 99 | int x_start = oled.width() - size_x * 2; |
brass_phoenix | 25:cc81f2120eda | 100 | int x_end = x_start + size_x; |
brass_phoenix | 25:cc81f2120eda | 101 | int y_start = oled.height() - size_y; |
brass_phoenix | 25:cc81f2120eda | 102 | int y_end = y_start + size_y; |
brass_phoenix | 25:cc81f2120eda | 103 | |
MAHCSnijders | 50:5d2176b93a65 | 104 | oled.fillRect(x_start, y_start, size_x, size_y, BLACK); // Deletes triangle in order to make a new one |
brass_phoenix | 25:cc81f2120eda | 105 | |
brass_phoenix | 25:cc81f2120eda | 106 | if (is_right) { |
brass_phoenix | 25:cc81f2120eda | 107 | oled.fillTriangle(x_start, y_end, x_end, y_start + size_y/2, x_start, y_start, WHITE); |
brass_phoenix | 25:cc81f2120eda | 108 | } else { |
brass_phoenix | 25:cc81f2120eda | 109 | oled.fillTriangle(x_end, y_start, x_start, y_start + size_y/2, x_end, y_end, WHITE); |
brass_phoenix | 25:cc81f2120eda | 110 | } |
brass_phoenix | 15:f65b4566193e | 111 | |
brass_phoenix | 15:f65b4566193e | 112 | display(); |
brass_phoenix | 15:f65b4566193e | 113 | } |
brass_phoenix | 15:f65b4566193e | 114 | |
brass_phoenix | 3:4b19b6cf6cc7 | 115 | }; |