State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
brass_phoenix
Date:
Thu Nov 01 09:10:21 2018 +0000
Revision:
25:cc81f2120eda
Parent:
24:e1092f95c82b
Child:
42:cafa56da9ed2
+ Arrows now correct.; * Flipped code location of homing.;

Who changed what in which revision?

UserRevisionLine numberNew 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 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 3:4b19b6cf6cc7 56
brass_phoenix 3:4b19b6cf6cc7 57 // Returns a direct handle to the screen.
brass_phoenix 3:4b19b6cf6cc7 58 // Be carefull while using the handle.
brass_phoenix 6:bfc6e68774f5 59 Adafruit_SSD1306_I2c* get_screen_handle() {
brass_phoenix 6:bfc6e68774f5 60 return &oled;
brass_phoenix 3:4b19b6cf6cc7 61 }
brass_phoenix 3:4b19b6cf6cc7 62
brass_phoenix 15:f65b4566193e 63 void display_up_down_arrow(bool is_up) {
brass_phoenix 25:cc81f2120eda 64 int size_x = 20;
brass_phoenix 25:cc81f2120eda 65 int size_y = 10;
brass_phoenix 25:cc81f2120eda 66 int x_start = oled.width() - size_x;
brass_phoenix 25:cc81f2120eda 67 int x_end = x_start + size_x;
brass_phoenix 25:cc81f2120eda 68 int y_start = oled.height() - size_y;
brass_phoenix 25:cc81f2120eda 69 int y_end = y_start + size_y;
brass_phoenix 25:cc81f2120eda 70
brass_phoenix 25:cc81f2120eda 71 oled.fillRect(x_start, y_start, size_x, size_y, BLACK);
brass_phoenix 25:cc81f2120eda 72
brass_phoenix 25:cc81f2120eda 73 if (is_up) {
brass_phoenix 25:cc81f2120eda 74 oled.fillTriangle(x_start, y_end, x_end, y_end, x_start + size_x/2, y_start, WHITE);
brass_phoenix 25:cc81f2120eda 75 } else {
brass_phoenix 25:cc81f2120eda 76 oled.fillTriangle(x_start + size_x/2, y_end, x_start, y_start, x_end, y_start, WHITE);
brass_phoenix 25:cc81f2120eda 77 }
brass_phoenix 15:f65b4566193e 78
brass_phoenix 15:f65b4566193e 79 display();
brass_phoenix 15:f65b4566193e 80 }
brass_phoenix 15:f65b4566193e 81
brass_phoenix 25:cc81f2120eda 82 void display_left_right_arrow(bool is_right) {
brass_phoenix 25:cc81f2120eda 83 int size_x = 20;
brass_phoenix 25:cc81f2120eda 84 int size_y = 10;
brass_phoenix 25:cc81f2120eda 85 int x_start = oled.width() - size_x * 2;
brass_phoenix 25:cc81f2120eda 86 int x_end = x_start + size_x;
brass_phoenix 25:cc81f2120eda 87 int y_start = oled.height() - size_y;
brass_phoenix 25:cc81f2120eda 88 int y_end = y_start + size_y;
brass_phoenix 25:cc81f2120eda 89
brass_phoenix 25:cc81f2120eda 90 oled.fillRect(x_start, y_start, size_x, size_y, BLACK);
brass_phoenix 25:cc81f2120eda 91
brass_phoenix 25:cc81f2120eda 92 if (is_right) {
brass_phoenix 25:cc81f2120eda 93 oled.fillTriangle(x_start, y_end, x_end, y_start + size_y/2, x_start, y_start, WHITE);
brass_phoenix 25:cc81f2120eda 94 } else {
brass_phoenix 25:cc81f2120eda 95 oled.fillTriangle(x_end, y_start, x_start, y_start + size_y/2, x_end, y_end, WHITE);
brass_phoenix 25:cc81f2120eda 96 }
brass_phoenix 15:f65b4566193e 97
brass_phoenix 15:f65b4566193e 98 display();
brass_phoenix 15:f65b4566193e 99 }
brass_phoenix 15:f65b4566193e 100
brass_phoenix 3:4b19b6cf6cc7 101 };