State machine

Dependencies:   mbed Adafruit_GFX BioroboticsMotorControl MODSERIAL BioroboticsEMGFilter

Committer:
brass_phoenix
Date:
Thu Nov 01 08:40:10 2018 +0000
Revision:
24:e1092f95c82b
Parent:
17:1fe276ff17db
Child:
25:cc81f2120eda
+ Homing state is back to angles.; + Down arrows on screen should be correct now.

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 15:f65b4566193e 64 int size = 40;
brass_phoenix 15:f65b4566193e 65 int x_start = oled.width() - size;
brass_phoenix 15:f65b4566193e 66 int x_end = x_start + size;
brass_phoenix 15:f65b4566193e 67 int y_start = oled.height() - size;
brass_phoenix 15:f65b4566193e 68 int y_end = y_start + size;
brass_phoenix 15:f65b4566193e 69 oled.fillRect(x_start, y_start, size, size, BLACK);
brass_phoenix 24:e1092f95c82b 70 oled.fillTriangle(x_start, y_start, x_start + size/2, y_end, x_end, y_start, WHITE);
brass_phoenix 15:f65b4566193e 71
brass_phoenix 15:f65b4566193e 72 display();
brass_phoenix 15:f65b4566193e 73 }
brass_phoenix 15:f65b4566193e 74
brass_phoenix 15:f65b4566193e 75 void display_left_right_arrow(bool is_right) {
brass_phoenix 15:f65b4566193e 76 int size = 40;
brass_phoenix 15:f65b4566193e 77 int x_start = oled.width() - (size * 2);
brass_phoenix 15:f65b4566193e 78 int x_end = x_start + size;
brass_phoenix 15:f65b4566193e 79 int y_start = oled.height() - size;
brass_phoenix 15:f65b4566193e 80 int y_end = y_start + size;
brass_phoenix 15:f65b4566193e 81 oled.fillRect(x_start, y_start, size, size, BLACK);
brass_phoenix 24:e1092f95c82b 82 oled.fillTriangle(x_start, y_start, x_start + size/2, y_end, x_end, y_start, WHITE);
brass_phoenix 15:f65b4566193e 83
brass_phoenix 15:f65b4566193e 84 display();
brass_phoenix 15:f65b4566193e 85 }
brass_phoenix 15:f65b4566193e 86
brass_phoenix 3:4b19b6cf6cc7 87 };