State machine
Dependencies: mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter
Screen.h@15:f65b4566193e, 2018-10-31 (annotated)
- Committer:
- brass_phoenix
- Date:
- Wed Oct 31 11:05:47 2018 +0000
- Revision:
- 15:f65b4566193e
- Parent:
- 6:bfc6e68774f5
- Child:
- 17:1fe276ff17db
+ Should be displaying arrows in normal operation.
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: |
brass_phoenix | 3:4b19b6cf6cc7 | 17 | I2C i2c; |
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 | |
brass_phoenix | 3:4b19b6cf6cc7 | 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 | 3:4b19b6cf6cc7 | 34 | display(); |
brass_phoenix | 3:4b19b6cf6cc7 | 35 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 36 | |
brass_phoenix | 3:4b19b6cf6cc7 | 37 | void display() { |
brass_phoenix | 3:4b19b6cf6cc7 | 38 | // Apparently the display only updates every 2 "display" calls. |
brass_phoenix | 3:4b19b6cf6cc7 | 39 | // This is a bug in the library. |
brass_phoenix | 3:4b19b6cf6cc7 | 40 | oled.display(); |
brass_phoenix | 3:4b19b6cf6cc7 | 41 | oled.display(); |
brass_phoenix | 3:4b19b6cf6cc7 | 42 | } |
brass_phoenix | 4:5a44ab7e94b3 | 43 | |
brass_phoenix | 5:2632dfc8454c | 44 | // State names can be a maximum of 17 characters long. |
brass_phoenix | 4:5a44ab7e94b3 | 45 | void display_state_name(string name) { |
brass_phoenix | 4:5a44ab7e94b3 | 46 | // Clear a previous title. |
brass_phoenix | 4:5a44ab7e94b3 | 47 | oled.setTextCursor(0, 0); |
brass_phoenix | 5:2632dfc8454c | 48 | oled.printf(" "); |
brass_phoenix | 4:5a44ab7e94b3 | 49 | oled.setTextCursor(0, 0); |
brass_phoenix | 4:5a44ab7e94b3 | 50 | oled.printf("- "); |
brass_phoenix | 4:5a44ab7e94b3 | 51 | oled.printf(name.c_str()); |
brass_phoenix | 6:bfc6e68774f5 | 52 | oled.printf(" -\r\n"); |
brass_phoenix | 4:5a44ab7e94b3 | 53 | display(); |
brass_phoenix | 4:5a44ab7e94b3 | 54 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 55 | |
brass_phoenix | 3:4b19b6cf6cc7 | 56 | // Returns a direct handle to the screen. |
brass_phoenix | 3:4b19b6cf6cc7 | 57 | // Be carefull while using the handle. |
brass_phoenix | 6:bfc6e68774f5 | 58 | Adafruit_SSD1306_I2c* get_screen_handle() { |
brass_phoenix | 6:bfc6e68774f5 | 59 | return &oled; |
brass_phoenix | 3:4b19b6cf6cc7 | 60 | } |
brass_phoenix | 3:4b19b6cf6cc7 | 61 | |
brass_phoenix | 15:f65b4566193e | 62 | void display_up_down_arrow(bool is_up) { |
brass_phoenix | 15:f65b4566193e | 63 | int size = 40; |
brass_phoenix | 15:f65b4566193e | 64 | int x_start = oled.width() - size; |
brass_phoenix | 15:f65b4566193e | 65 | int x_end = x_start + size; |
brass_phoenix | 15:f65b4566193e | 66 | int y_start = oled.height() - size; |
brass_phoenix | 15:f65b4566193e | 67 | int y_end = y_start + size; |
brass_phoenix | 15:f65b4566193e | 68 | oled.fillRect(x_start, y_start, size, size, BLACK); |
brass_phoenix | 15:f65b4566193e | 69 | oled.fillTriangle(x_start, y_start, x_end, y_start, x_start + size/2, y_end, WHITE); |
brass_phoenix | 15:f65b4566193e | 70 | |
brass_phoenix | 15:f65b4566193e | 71 | display(); |
brass_phoenix | 15:f65b4566193e | 72 | } |
brass_phoenix | 15:f65b4566193e | 73 | |
brass_phoenix | 15:f65b4566193e | 74 | void display_left_right_arrow(bool is_right) { |
brass_phoenix | 15:f65b4566193e | 75 | int size = 40; |
brass_phoenix | 15:f65b4566193e | 76 | int x_start = oled.width() - (size * 2); |
brass_phoenix | 15:f65b4566193e | 77 | int x_end = x_start + size; |
brass_phoenix | 15:f65b4566193e | 78 | int y_start = oled.height() - size; |
brass_phoenix | 15:f65b4566193e | 79 | int y_end = y_start + size; |
brass_phoenix | 15:f65b4566193e | 80 | oled.fillRect(x_start, y_start, size, size, BLACK); |
brass_phoenix | 15:f65b4566193e | 81 | oled.fillTriangle(x_start, y_start, x_end, y_start, x_start + size/2, y_end, WHITE); |
brass_phoenix | 15:f65b4566193e | 82 | |
brass_phoenix | 15:f65b4566193e | 83 | display(); |
brass_phoenix | 15:f65b4566193e | 84 | } |
brass_phoenix | 15:f65b4566193e | 85 | |
brass_phoenix | 3:4b19b6cf6cc7 | 86 | }; |