Frästisch mit elektronischer Höhenverstellung mittels Schrittmotoren.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Display.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Display.cpp
  3. *
  4. * Wrapper class for Adafruit OLED display.
  5. *
  6. * Created on: 28.01.2022
  7. * Author: FSmilari
  8. */
  9. #include "Display.h"
  10. // Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 48)
  11. const int epd_rotate_bitmap_allArray_LEN = 8;
  12. const static unsigned char *epd_rotate_bitmap_allArray[epd_rotate_bitmap_allArray_LEN] = {
  13. epd_bitmap_Rotate_0,
  14. epd_bitmap_Rotate_45,
  15. epd_bitmap_Rotate_90,
  16. epd_bitmap_Rotate_135,
  17. epd_bitmap_Rotate_180,
  18. epd_bitmap_Rotate_225,
  19. epd_bitmap_Rotate_270,
  20. epd_bitmap_Rotate_315
  21. };
  22. /*****************
  23. ** Constructors.
  24. ****************/
  25. Display::Display() : mode(SLOW) {
  26. ssd1306 = Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT);
  27. u8g2_gfx.begin(ssd1306);
  28. wlsConnected = false;
  29. angle = 0;
  30. starttime = millis();
  31. refreshScreen = true;
  32. distanceValue = 0;
  33. }
  34. /******************
  35. ** Public methods
  36. *****************/
  37. void Display::init() {
  38. if (!ssd1306.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, true, true)) {
  39. Serial.println(F("SSD1306 allocation failed"));
  40. for (;;)
  41. ; // Don't proceed, loop forever
  42. }
  43. }
  44. void Display::setRefreshScreen() {
  45. refreshScreen = true;
  46. }
  47. void Display::display() {
  48. if (refreshScreen) {
  49. refreshScreen = false;
  50. ssd1306.display();
  51. }
  52. }
  53. void Display::clearDisplay() {
  54. ssd1306.clearDisplay();
  55. }
  56. void Display::showBrand() {
  57. ssd1306.clearDisplay();
  58. ssd1306.drawBitmap(0, 0, epd_bitmap_SFTools_Logo, imageWidth, imageHeight, SSD1306_WHITE);
  59. char *s = &String("Frästisch N172")[0];
  60. int16_t w = 0, h = 0;
  61. u8g2_gfx.setFont(u8g2_font_helvB12_tf);
  62. w = u8g2_gfx.getUTF8Width(s);
  63. h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent();
  64. u8g2_gfx.setForegroundColor(SSD1306_WHITE);
  65. u8g2_gfx.setCursor((SCREEN_WIDTH - w) / 2, 63 - (30 - h) / 2);
  66. u8g2_gfx.println(F(s));
  67. display();
  68. }
  69. void Display::showInitialization() {
  70. ssd1306.clearDisplay();
  71. char *s = &String("Initialisieren")[0];
  72. int16_t w = 0;
  73. u8g2_gfx.setFont(u8g2_font_helvB12_tf);
  74. w = u8g2_gfx.getUTF8Width(s);
  75. u8g2_gfx.setForegroundColor(SSD1306_WHITE);
  76. u8g2_gfx.setCursor((SCREEN_WIDTH - w) / 2, 30);
  77. u8g2_gfx.println(F(s));
  78. char *g;
  79. w = u8g2_gfx.getUTF8Width(". . . . .");
  80. u8g2_gfx.setCursor((SCREEN_WIDTH - w) / 2, 50);
  81. String gauge = "";
  82. for (int i = 0; i < 6; i++) {
  83. g = &gauge[0];
  84. u8g2_gfx.print(g);
  85. setRefreshScreen();
  86. display();
  87. delay(500);
  88. if (i < 4) {
  89. gauge = ". ";
  90. g = &gauge[0];
  91. } else {
  92. gauge = ".";
  93. g = &gauge[0];
  94. }
  95. }
  96. }
  97. void Display::showFrame(Status status) {
  98. switch (status) {
  99. case MOVING_ELEVATOR:
  100. rotateAndDrawRotationBitmap();
  101. drawBitmap((37 - imageSide_R) / 2, 47, imageSide_D, imageSide_D, epd_bitmap_Dive);
  102. break;
  103. case TOOL_CHANGE:
  104. redrawFrame();
  105. drawStatusText(STATUS_TXT_TOOLCHG);
  106. drawBitmap(37 + (SCREEN_WIDTH - 37 - imageSide) / 2, (SCREEN_HEIGHT - imageSide) / 2, imageSide, imageSide, epd_bitmap_Tools);
  107. break;
  108. case CONFIGURATION:
  109. redrawFrame();
  110. ssd1306.drawLine(37, SCREEN_HEIGHT / 2 - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT / 2 - 1, SSD1306_WHITE);
  111. drawStatusText(STATUS_TXT_CFG);
  112. drawConfigText(this->configText);
  113. drawConfigOption(this->configOption);
  114. break;
  115. case IDLE:
  116. redrawFrame();
  117. ssd1306.drawLine(37, 43, SCREEN_WIDTH - 1, 43, SSD1306_WHITE);
  118. drawStatusText(STATUS_TXT_IDLE);
  119. drawMode(this->mode);
  120. break;
  121. case NULLING:
  122. case NULLING_TLS:
  123. redrawFrame();
  124. drawStatusText(STATUS_TXT_NULLING);
  125. drawBitmap(37 + (SCREEN_WIDTH - 37 - imageSide) / 2, (SCREEN_HEIGHT - imageSide) / 2, imageSide, imageSide, epd_bitmap_Nulling);
  126. break;
  127. default:
  128. break;
  129. }
  130. display();
  131. }
  132. void Display::drawConfigText(String txt) {
  133. char *s = &txt[0];
  134. int16_t w = 0, h = 0;
  135. u8g2_gfx.setFont(u8g2_font_helvB12_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  136. w = u8g2_gfx.getUTF8Width(s);
  137. h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent();
  138. u8g2_gfx.setCursor((SCREEN_WIDTH + 37 - w) / 2, SCREEN_HEIGHT / 4 + h / 2 - 1); // start writing at this position
  139. u8g2_gfx.println(F(s));
  140. }
  141. void Display::drawConfigOption(String txt) {
  142. char *s = &txt[0];
  143. int16_t w = 0, h = 0;
  144. u8g2_gfx.setFont(u8g2_font_helvB12_tf);
  145. w = u8g2_gfx.getUTF8Width(s);
  146. h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent();
  147. u8g2_gfx.setCursor((SCREEN_WIDTH + 37 - w) / 2, SCREEN_HEIGHT / 4 * 3 + h / 2 - 1);
  148. u8g2_gfx.println(F(s));
  149. }
  150. const String& Display::getConfigOption() const {
  151. return configOption;
  152. }
  153. void Display::setConfigOption(const String &configOption) {
  154. if (!this->configOption.equals(configOption)) {
  155. this->configOption = configOption;
  156. setRefreshScreen();
  157. }
  158. }
  159. const String& Display::getConfigText() const {
  160. return configText;
  161. }
  162. void Display::setConfigText(const String &configText) {
  163. if (!this->configText.equals(configText)) {
  164. this->configText = configText;
  165. setRefreshScreen();
  166. }
  167. }
  168. void Display::setWlsConnected(bool wlsConnected) {
  169. if (this->wlsConnected != wlsConnected) {
  170. this->wlsConnected = wlsConnected;
  171. setRefreshScreen();
  172. }
  173. }
  174. void Display::setDistanceUnit(const String &distanceUnit) {
  175. if (this->distanceUnit != distanceUnit) {
  176. this->distanceUnit = distanceUnit;
  177. setRefreshScreen();
  178. }
  179. }
  180. void Display::setDistanceValue(const float &distanceValue) {
  181. if (this->distanceValue != distanceValue) {
  182. this->distanceValue = distanceValue;
  183. setRefreshScreen();
  184. }
  185. }
  186. void Display::setMode(const ValueMode &mode) {
  187. if (!this->mode.equals(mode)) {
  188. Serial.println("Equal: " + this->mode.toString());
  189. this->mode = mode;
  190. setRefreshScreen();
  191. }
  192. }
  193. void Display::drawWLSStatus() {
  194. String txt = wlsConnected ? "WLS" : "";
  195. char *s = &txt[0];
  196. int16_t w = 0;
  197. u8g2_gfx.setFont(u8g2_font_helvR08_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  198. w = u8g2_gfx.getUTF8Width(s);
  199. u8g2_gfx.setCursor((37 - w) / 2, 27);
  200. u8g2_gfx.println(F(s));
  201. }
  202. void Display::drawMode(ValueMode mode) {
  203. String txt = mode.toString();
  204. char *s = &txt[0];
  205. int16_t w = 0;
  206. u8g2_gfx.setFont(u8g2_font_helvB10_tf);
  207. w = u8g2_gfx.getUTF8Width(s);
  208. u8g2_gfx.setCursor((SCREEN_WIDTH - w - 6), 63 - 4);
  209. u8g2_gfx.println(F(s));
  210. }
  211. void Display::drawDistanceValue(float distance) {
  212. }
  213. void Display::drawDistanceUnit(String unit) {
  214. }
  215. /********************************
  216. ** Private methods
  217. *******************************/
  218. void Display::drawStatusText(String txt) {
  219. char *s = &txt[0];
  220. int16_t w = 0;
  221. u8g2_gfx.setFont(u8g2_font_helvR08_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  222. w = u8g2_gfx.getUTF8Width(s);
  223. u8g2_gfx.setCursor((37 - w) / 2, 12);
  224. u8g2_gfx.println(F(s));
  225. }
  226. void Display::drawBitmap(int x, int y, int w, int h, const uint8_t bitmap[]) {
  227. ssd1306.drawBitmap(x, y, bitmap, w, h, SSD1306_WHITE);
  228. }
  229. void Display::redrawFrame() {
  230. ssd1306.clearDisplay();
  231. ssd1306.drawRoundRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 3, SSD1306_WHITE);
  232. ssd1306.drawLine(37, 0, 37, SCREEN_HEIGHT - 1, SSD1306_WHITE);
  233. ssd1306.drawLine(0, 15, 37, 15, SSD1306_WHITE);
  234. drawWLSStatus();
  235. }
  236. void Display::rotateAndDrawRotationBitmap() {
  237. if (millis() - starttime >= 125) {
  238. int xOffset = 0;
  239. int yOffset = 0;
  240. switch (angle) {
  241. case 0:
  242. xOffset = imageSide_R / 2;
  243. yOffset = 0;
  244. break;
  245. case 1:
  246. xOffset = imageSide_R / 2;
  247. yOffset = imageSide_R / 4;
  248. break;
  249. case 2:
  250. xOffset = imageSide_R / 2;
  251. yOffset = imageSide_R / 2;
  252. break;
  253. case 3:
  254. xOffset = imageSide_R / 4;
  255. yOffset = imageSide_R / 2;
  256. break;
  257. case 4:
  258. xOffset = 0;
  259. yOffset = imageSide_R / 2;
  260. break;
  261. case 5:
  262. xOffset = 0;
  263. yOffset = imageSide_R / 4;
  264. break;
  265. case 6:
  266. xOffset = 0;
  267. yOffset = 0;
  268. break;
  269. case 7:
  270. xOffset = imageSide_R / 4;
  271. yOffset = 0;
  272. break;
  273. default:
  274. break;
  275. }
  276. ssd1306.fillRect((37 - imageSide_R) / 2 + xOffset, 30 + yOffset, imageSide_R / 2 + 1, imageSide_R / 2 + 1, SSD1306_BLACK);
  277. ssd1306.display();
  278. drawBitmap((37 - imageSide_R) / 2, 30, imageSide_R, imageSide_R, epd_rotate_bitmap_allArray[angle]);
  279. angle = (angle + 1) % 8;
  280. starttime = millis();
  281. setRefreshScreen();
  282. display();
  283. }
  284. }