Frästisch mit elektronischer Höhenverstellung mittels Schrittmotoren.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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