Frästisch mit elektronischer Höhenverstellung mittels Schrittmotoren.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Display.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. 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. drawDistanceValue(distanceValue);
  120. drawDistanceUnit(distanceUnit);
  121. drawMode(this->mode);
  122. break;
  123. case NULLING:
  124. case NULLING_TLS:
  125. redrawFrame();
  126. drawStatusText(STATUS_TXT_NULLING);
  127. drawBitmap(37 + (SCREEN_WIDTH - 37 - imageSide) / 2, (SCREEN_HEIGHT - imageSide) / 2, imageSide, imageSide, epd_bitmap_Nulling);
  128. break;
  129. case DIVING:
  130. redrawFrame();
  131. drawStatusText(STATUS_TXT_DIVING);
  132. drawBitmap((37 - imageSide_R) / 2, 47, imageSide_D, imageSide_D, epd_bitmap_Dive);
  133. drawDistanceValue(distanceValue);
  134. drawDistanceUnit(distanceUnit);
  135. break;
  136. default:
  137. break;
  138. }
  139. display();
  140. }
  141. void Display::drawConfigText(String txt) {
  142. char *s = &txt[0];
  143. int16_t w = 0, h = 0;
  144. u8g2_gfx.setFont(u8g2_font_helvB12_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  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 + h / 2 - 1); // start writing at this position
  148. u8g2_gfx.println(F(s));
  149. }
  150. void Display::drawConfigOption(String txt) {
  151. char *s = &txt[0];
  152. int16_t w = 0, h = 0;
  153. u8g2_gfx.setFont(u8g2_font_helvB12_tf);
  154. w = u8g2_gfx.getUTF8Width(s);
  155. h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent();
  156. u8g2_gfx.setCursor((SCREEN_WIDTH + 37 - w) / 2, SCREEN_HEIGHT / 4 * 3 + h / 2 - 1);
  157. u8g2_gfx.println(F(s));
  158. }
  159. const String& Display::getConfigOption() const {
  160. return configOption;
  161. }
  162. void Display::setConfigOption(const String &configOption) {
  163. if (!this->configOption.equals(configOption)) {
  164. this->configOption = configOption;
  165. setRefreshScreen();
  166. }
  167. }
  168. const String& Display::getConfigText() const {
  169. return configText;
  170. }
  171. void Display::setConfigText(const String &configText) {
  172. if (!this->configText.equals(configText)) {
  173. this->configText = configText;
  174. setRefreshScreen();
  175. }
  176. }
  177. void Display::setWlsConnected(bool wlsConnected) {
  178. if (this->wlsConnected != wlsConnected) {
  179. this->wlsConnected = wlsConnected;
  180. setRefreshScreen();
  181. }
  182. }
  183. void Display::setDistanceUnit(const String &distanceUnit) {
  184. if (this->distanceUnit != distanceUnit) {
  185. this->distanceUnit = distanceUnit;
  186. setRefreshScreen();
  187. }
  188. }
  189. void Display::setDistanceValue(const float &distanceValue) {
  190. if (this->distanceValue != distanceValue) {
  191. this->distanceValue = distanceValue;
  192. setRefreshScreen();
  193. }
  194. }
  195. void Display::setMode(const ValueMode &mode) {
  196. if (!this->mode.equals(mode)) {
  197. this->mode = mode;
  198. setRefreshScreen();
  199. }
  200. }
  201. void Display::drawWLSStatus() {
  202. String txt = wlsConnected ? "WLS" : "";
  203. char *s = &txt[0];
  204. int16_t w = 0;
  205. u8g2_gfx.setFont(u8g2_font_helvR08_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  206. w = u8g2_gfx.getUTF8Width(s);
  207. u8g2_gfx.setCursor((37 - w) / 2, 27);
  208. u8g2_gfx.println(F(s));
  209. }
  210. void Display::drawMode(ValueMode mode) {
  211. String txt = mode.toString();
  212. char *s = &txt[0];
  213. int16_t w = 0;
  214. u8g2_gfx.setFont(u8g2_font_helvB08_tf);
  215. w = u8g2_gfx.getUTF8Width(s);
  216. u8g2_gfx.setCursor((SCREEN_WIDTH - w - 6), 63 - 6);
  217. u8g2_gfx.println(F(s));
  218. }
  219. void Display::drawDistanceValue(float distance) {
  220. String txt = String(distance, 2);
  221. char *s = &txt[0];
  222. int16_t w = 0, h = 0;
  223. u8g2_gfx.setFont(u8g2_font_logisoso22_tf);
  224. w = u8g2_gfx.getUTF8Width(s);
  225. h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent();
  226. u8g2_gfx.setCursor((SCREEN_WIDTH - w - 6), h);
  227. u8g2_gfx.println(F(s));
  228. }
  229. void Display::drawDistanceUnit(String unit) {
  230. String txt = unit;
  231. char *s = &txt[0];
  232. int16_t w = 0;
  233. u8g2_gfx.setFont(u8g2_font_helvB12_tf);
  234. w = u8g2_gfx.getUTF8Width(s);
  235. u8g2_gfx.setCursor((SCREEN_WIDTH - w - 6), 41);
  236. u8g2_gfx.println(F(s));
  237. }
  238. /********************************
  239. ** Private methods
  240. *******************************/
  241. void Display::drawStatusText(String txt) {
  242. char *s = &txt[0];
  243. int16_t w = 0;
  244. u8g2_gfx.setFont(u8g2_font_helvR08_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  245. w = u8g2_gfx.getUTF8Width(s);
  246. u8g2_gfx.setCursor((37 - w) / 2, 12);
  247. u8g2_gfx.println(F(s));
  248. }
  249. void Display::drawBitmap(int x, int y, int w, int h, const uint8_t bitmap[]) {
  250. ssd1306.drawBitmap(x, y, bitmap, w, h, SSD1306_WHITE);
  251. }
  252. void Display::redrawFrame() {
  253. ssd1306.clearDisplay();
  254. ssd1306.drawRoundRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 3, SSD1306_WHITE);
  255. ssd1306.drawLine(37, 0, 37, SCREEN_HEIGHT - 1, SSD1306_WHITE);
  256. ssd1306.drawLine(0, 15, 37, 15, SSD1306_WHITE);
  257. drawWLSStatus();
  258. }
  259. void Display::rotateAndDrawRotationBitmap() {
  260. if (millis() - starttime >= 125) {
  261. int xOffset = 0;
  262. int yOffset = 0;
  263. switch (angle) {
  264. case 0:
  265. xOffset = imageSide_R / 2;
  266. yOffset = 0;
  267. break;
  268. case 1:
  269. xOffset = imageSide_R / 2;
  270. yOffset = imageSide_R / 4;
  271. break;
  272. case 2:
  273. xOffset = imageSide_R / 2;
  274. yOffset = imageSide_R / 2;
  275. break;
  276. case 3:
  277. xOffset = imageSide_R / 4;
  278. yOffset = imageSide_R / 2;
  279. break;
  280. case 4:
  281. xOffset = 0;
  282. yOffset = imageSide_R / 2;
  283. break;
  284. case 5:
  285. xOffset = 0;
  286. yOffset = imageSide_R / 4;
  287. break;
  288. case 6:
  289. xOffset = 0;
  290. yOffset = 0;
  291. break;
  292. case 7:
  293. xOffset = imageSide_R / 4;
  294. yOffset = 0;
  295. break;
  296. default:
  297. break;
  298. }
  299. ssd1306.fillRect((37 - imageSide_R) / 2 + xOffset, 30 + yOffset, imageSide_R / 2 + 1, imageSide_R / 2 + 1, SSD1306_BLACK);
  300. ssd1306.display();
  301. drawBitmap((37 - imageSide_R) / 2, 30, imageSide_R, imageSide_R, epd_rotate_bitmap_allArray[angle]);
  302. angle = (angle + 1) % 8;
  303. starttime = millis();
  304. setRefreshScreen();
  305. display();
  306. }
  307. }