|
| 1 | +/* |
| 2 | + oled_basic.cpp |
| 3 | + Part of Grbl_ESP32 |
| 4 | +
|
| 5 | + copyright (c) 2018 - Bart Dring This file was modified for use on the ESP32 |
| 6 | + CPU. Do not use this with Grbl for atMega328P |
| 7 | +
|
| 8 | + Grbl is free software: you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU General Public License as published by |
| 10 | + the Free Software Foundation, either version 3 of the License, or |
| 11 | + (at your option) any later version. |
| 12 | +
|
| 13 | + Grbl is distributed in the hope that it will be useful, |
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + GNU General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU General Public License |
| 19 | + along with Grbl. If not, see <http://www.gnu.org/licenses/>. |
| 20 | +
|
| 21 | + -------------------------------------------------------------- |
| 22 | +
|
| 23 | + This is a minimal implentation of a display as a test project. |
| 24 | + It is designed to be used with a machine that has no easily accessible serial connection |
| 25 | + It shows basic status and connection information. |
| 26 | +
|
| 27 | + When in alarm mode it will show the current Wifi/BT paramaters and status |
| 28 | + Most machines will start in alarm mode (needs homing) |
| 29 | + If the machine is running a job from SD it will show the progress |
| 30 | + In other modes it will show state and 3 axis DROs |
| 31 | + Thats All! |
| 32 | +
|
| 33 | + Library Infor: |
| 34 | + https://github.com/ThingPulse/esp8266-oled-ssd1306 |
| 35 | +
|
| 36 | + Install to PlatformIO with this typed at the terminal |
| 37 | + platformio lib install 562 |
| 38 | +
|
| 39 | + Add this to your machine definition file |
| 40 | + #define DISPLAY_CODE_FILENAME "Custom/oled_basic.cpp" |
| 41 | +
|
| 42 | +*/ |
| 43 | + |
| 44 | +// Include the correct display library |
| 45 | + |
| 46 | +#include "SSD1306Wire.h" // legacy: #include "SSD1306.h" |
| 47 | +#include "../src/WebUI/WebSettings.h" |
| 48 | + |
| 49 | +#ifndef OLED_ADDRESS |
| 50 | +# define OLED_ADDRESS 0x3c |
| 51 | +#endif |
| 52 | + |
| 53 | +#ifndef OLED_SDA |
| 54 | +# define OLED_SDA GPIO_NUM_14 |
| 55 | +#endif |
| 56 | + |
| 57 | +#ifndef OLED_SCL |
| 58 | +# define OLED_SCL GPIO_NUM_13 |
| 59 | +#endif |
| 60 | + |
| 61 | +#ifndef OLED_GEOMETRY |
| 62 | +# define OLED_GEOMETRY GEOMETRY_128_64 |
| 63 | +#endif |
| 64 | + |
| 65 | +SSD1306Wire display(OLED_ADDRESS, OLED_SDA, OLED_SCL, OLED_GEOMETRY); |
| 66 | + |
| 67 | +static TaskHandle_t displayUpdateTaskHandle = 0; |
| 68 | + |
| 69 | +// returns the position of a machine axis |
| 70 | +// wpos =true for corrected work postion |
| 71 | +float getPosition(uint8_t axis, bool wpos = true) { |
| 72 | + float wco; // work coordinate system offset |
| 73 | + |
| 74 | + float current_position = sys_position[axis] / axis_settings[axis]->steps_per_mm->get(); |
| 75 | + |
| 76 | + if (wpos) { |
| 77 | + // Apply work coordinate offsets and tool length offset to current position. |
| 78 | + wco = gc_state.coord_system[axis] + gc_state.coord_offset[axis]; |
| 79 | + if (axis == TOOL_LENGTH_OFFSET_AXIS) { |
| 80 | + wco += gc_state.tool_length_offset; |
| 81 | + } |
| 82 | + |
| 83 | + current_position -= wco; |
| 84 | + } |
| 85 | + return current_position; |
| 86 | +} |
| 87 | + |
| 88 | +String getStateText() { |
| 89 | + String str = ""; |
| 90 | + |
| 91 | + switch (sys.state) { |
| 92 | + case State::Idle: |
| 93 | + str = "Idle"; |
| 94 | + break; |
| 95 | + case State::Cycle: |
| 96 | + str = "Run"; |
| 97 | + break; |
| 98 | + case State::Hold: |
| 99 | + if (!(sys.suspend.bit.jogCancel)) { |
| 100 | + str = "Hold:"; |
| 101 | + sys.suspend.bit.holdComplete ? str += "0" : str += "1"; // Ready to resume |
| 102 | + break; |
| 103 | + } // Continues to print jog state during jog cancel. |
| 104 | + case State::Jog: |
| 105 | + str = "Jog"; |
| 106 | + break; |
| 107 | + case State::Homing: |
| 108 | + str = "Homing"; |
| 109 | + break; |
| 110 | + case State::Alarm: |
| 111 | + str = "Alarm"; |
| 112 | + break; |
| 113 | + case State::CheckMode: |
| 114 | + str = "Check"; |
| 115 | + break; |
| 116 | + case State::SafetyDoor: |
| 117 | + str = "Door:"; |
| 118 | + if (sys.suspend.bit.initiateRestore) { |
| 119 | + str += "3"; // Restoring |
| 120 | + } else { |
| 121 | + if (sys.suspend.bit.retractComplete) { |
| 122 | + sys.suspend.bit.safetyDoorAjar ? str += "1" : str += "0"; // Door ajar |
| 123 | + // Door closed and ready to resume |
| 124 | + } else { |
| 125 | + str += "2"; // Retracting |
| 126 | + } |
| 127 | + } |
| 128 | + break; |
| 129 | + case State::Sleep: |
| 130 | + str = "Sleep"; |
| 131 | + break; |
| 132 | + } |
| 133 | + |
| 134 | + return str; |
| 135 | +} |
| 136 | + |
| 137 | +// This displays the status of the ESP32 Radios...BT, WiFi, etc |
| 138 | +void displayRadioInfo() { |
| 139 | + String radio_info = ""; |
| 140 | + |
| 141 | + const uint8_t row1 = 18; |
| 142 | + const uint8_t row2 = 30; |
| 143 | + const uint8_t row3 = 42; |
| 144 | + |
| 145 | + display.setTextAlignment(TEXT_ALIGN_LEFT); |
| 146 | + display.setFont(ArialMT_Plain_10); |
| 147 | + |
| 148 | +#ifdef ENABLE_BLUETOOTH |
| 149 | + if (WebUI::wifi_radio_mode->get() == ESP_BT) { |
| 150 | + radio_info = String("Bluetooth: ") + WebUI::bt_name->get(); |
| 151 | + display.drawString(0, row1, radio_info); |
| 152 | + radio_info = String("Status: ") + String(WebUI::SerialBT.hasClient() ? "Connected" : "Not connected"); |
| 153 | + display.drawString(0, row2, radio_info); |
| 154 | + } |
| 155 | +#endif |
| 156 | +#ifdef ENABLE_WIFI |
| 157 | + if ((WiFi.getMode() == WIFI_MODE_STA) || (WiFi.getMode() == WIFI_MODE_APSTA)) { |
| 158 | + radio_info = "STA SSID: " + WiFi.SSID(); |
| 159 | + display.drawString(0, row1, radio_info); |
| 160 | + |
| 161 | + radio_info = "IP: " + WiFi.localIP().toString(); |
| 162 | + display.drawString(0, row2, radio_info); |
| 163 | + |
| 164 | + radio_info = "Status: "; |
| 165 | + (WiFi.status() == WL_CONNECTED) ? radio_info += "Connected" : radio_info += "Not connected"; |
| 166 | + display.drawString(0, row3, radio_info); |
| 167 | + //} |
| 168 | + } else if ((WiFi.getMode() == WIFI_MODE_AP) || (WiFi.getMode() == WIFI_MODE_APSTA)) { |
| 169 | + radio_info = String("AP SSID: ") + WebUI::wifi_ap_ssid->get(); |
| 170 | + |
| 171 | + display.drawString(0, row1, radio_info); |
| 172 | + |
| 173 | + radio_info = "IP: " + WiFi.softAPIP().toString(); |
| 174 | + display.drawString(0, row2, radio_info); |
| 175 | + } |
| 176 | +#endif |
| 177 | + |
| 178 | +#ifdef WIFI_OR_BLUETOOTH |
| 179 | + if (WebUI::wifi_radio_mode->get() == ESP_RADIO_OFF) { |
| 180 | + display.drawString(0, row1, "Radio Mode: None"); |
| 181 | + } |
| 182 | +#else |
| 183 | + display.drawString(0, row1, "Wifi and Bluetooth Disabled"); |
| 184 | +#endif |
| 185 | +} |
| 186 | + |
| 187 | +void displayDRO() { |
| 188 | + char axisVal[20]; |
| 189 | + |
| 190 | + display.setFont(ArialMT_Plain_16); |
| 191 | + display.setTextAlignment(TEXT_ALIGN_LEFT); |
| 192 | + display.drawString(0, 13, String('X') + ":"); |
| 193 | + display.drawString(0, 30, "Y:"); |
| 194 | + display.drawString(0, 47, "Z:"); |
| 195 | + |
| 196 | + display.setTextAlignment(TEXT_ALIGN_RIGHT); |
| 197 | + snprintf(axisVal, 20 - 1, "%.3f", getPosition(X_AXIS, true)); |
| 198 | + display.drawString(100, 13, axisVal); |
| 199 | + |
| 200 | + snprintf(axisVal, 20 - 1, "%.3f", getPosition(Y_AXIS, true)); |
| 201 | + display.drawString(100, 30, axisVal); |
| 202 | + |
| 203 | + snprintf(axisVal, 20 - 1, "%.3f", getPosition(Z_AXIS, true)); |
| 204 | + display.drawString(100, 47, axisVal); |
| 205 | +} |
| 206 | + |
| 207 | +void displayUpdate(void* pvParameters) { |
| 208 | + TickType_t xLastWakeTime; |
| 209 | + const TickType_t xDisplayFrequency = 100; // in ticks (typically ms) |
| 210 | + xLastWakeTime = xTaskGetTickCount(); // Initialise the xLastWakeTime variable with the current time. |
| 211 | + |
| 212 | + vTaskDelay(2500); |
| 213 | + uint16_t sd_file_ticker = 0; |
| 214 | + |
| 215 | + display.init(); |
| 216 | + display.flipScreenVertically(); |
| 217 | + |
| 218 | + while (true) { |
| 219 | + display.clear(); |
| 220 | + |
| 221 | + String state_string = getStateText(); |
| 222 | + |
| 223 | + state_string.toUpperCase(); |
| 224 | + |
| 225 | + display.setTextAlignment(TEXT_ALIGN_LEFT); |
| 226 | + display.setFont(ArialMT_Plain_16); |
| 227 | + display.drawString(0, 0, state_string); |
| 228 | + |
| 229 | + if (get_sd_state(false) == SDState::BusyPrinting) { |
| 230 | + display.clear(); |
| 231 | + display.setTextAlignment(TEXT_ALIGN_LEFT); |
| 232 | + display.setFont(ArialMT_Plain_16); |
| 233 | + state_string = "SD File"; |
| 234 | + for (int i = 0; i < sd_file_ticker % 10; i++) { |
| 235 | + state_string += "."; |
| 236 | + } |
| 237 | + sd_file_ticker++; |
| 238 | + display.drawString(25, 0, state_string); |
| 239 | + |
| 240 | + int progress = sd_report_perc_complete(); |
| 241 | + // draw the progress bar |
| 242 | + display.drawProgressBar(0, 45, 120, 10, progress); |
| 243 | + |
| 244 | + // draw the percentage as String |
| 245 | + display.setFont(ArialMT_Plain_16); |
| 246 | + display.setTextAlignment(TEXT_ALIGN_CENTER); |
| 247 | + display.drawString(64, 25, String(progress) + "%"); |
| 248 | + |
| 249 | + } else if (sys.state == State::Alarm) { |
| 250 | + displayRadioInfo(); |
| 251 | + } else { |
| 252 | + displayDRO(); |
| 253 | + } |
| 254 | + display.display(); |
| 255 | + |
| 256 | + vTaskDelayUntil(&xLastWakeTime, xDisplayFrequency); |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +void display_init() { |
| 261 | + // Initialising the UI will init the display too. |
| 262 | + grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "Init Basic OLED SDA:%s SCL:%s", pinName(OLED_SDA), pinName(OLED_SCL)); |
| 263 | + |
| 264 | + display.init(); |
| 265 | + display.flipScreenVertically(); |
| 266 | + display.clear(); |
| 267 | + display.setTextAlignment(TEXT_ALIGN_CENTER); |
| 268 | + display.setFont(ArialMT_Plain_16); |
| 269 | + |
| 270 | + String mach_name = MACHINE_NAME; |
| 271 | + // remove characters from the end until the string fits |
| 272 | + while (display.getStringWidth(mach_name) > 128) { |
| 273 | + mach_name = mach_name.substring(0, mach_name.length() - 1); |
| 274 | + } |
| 275 | + |
| 276 | + display.drawString(63, 0, mach_name); |
| 277 | + display.display(); |
| 278 | + |
| 279 | + xTaskCreatePinnedToCore(displayUpdate, // task |
| 280 | + "displayUpdateTask", // name for task |
| 281 | + 4096, // size of task stack |
| 282 | + NULL, // parameters |
| 283 | + 1, // priority |
| 284 | + &displayUpdateTaskHandle, |
| 285 | + CONFIG_ARDUINO_RUNNING_CORE // must run the task on same core |
| 286 | + // core |
| 287 | + ); |
| 288 | +} |
0 commit comments