My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

DGUSDisplay.cpp 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /* DGUS implementation written by coldtobi in 2019 for Marlin */
  23. #include "../../../../inc/MarlinConfigPre.h"
  24. #if ENABLED(DGUS_LCD)
  25. #include "DGUSDisplay.h"
  26. #include "DGUSVPVariable.h"
  27. #include "DGUSDisplayDefinition.h"
  28. #include "../../ui_api.h"
  29. #include "../../../../Marlin.h"
  30. #include "../../../../module/temperature.h"
  31. #include "../../../../module/motion.h"
  32. #include "../../../../gcode/queue.h"
  33. #include "../../../../module/planner.h"
  34. #include "../../../../sd/cardreader.h"
  35. #include "../../../../libs/duration_t.h"
  36. #include "../../../../module/printcounter.h"
  37. // Preamble... 2 Bytes, usually 0x5A 0xA5, but configurable
  38. constexpr uint8_t DGUS_HEADER1 = 0x5A;
  39. constexpr uint8_t DGUS_HEADER2 = 0xA5;
  40. constexpr uint8_t DGUS_CMD_WRITEVAR = 0x82;
  41. constexpr uint8_t DGUS_CMD_READVAR = 0x83;
  42. #if ENABLED(DEBUG_DGUSLCD)
  43. bool dguslcd_local_debug; // = false;
  44. #endif
  45. uint16_t DGUSScreenVariableHandler::ConfirmVP;
  46. #if ENABLED(SDSUPPORT)
  47. int16_t DGUSScreenVariableHandler::top_file = 0;
  48. int16_t DGUSScreenVariableHandler::file_to_print = 0;
  49. static ExtUI::FileList filelist;
  50. #endif
  51. void (*DGUSScreenVariableHandler::confirm_action_cb)() = nullptr;
  52. //DGUSScreenVariableHandler ScreenHandler;
  53. DGUSLCD_Screens DGUSScreenVariableHandler::current_screen;
  54. DGUSLCD_Screens DGUSScreenVariableHandler::past_screens[NUM_PAST_SCREENS];
  55. uint8_t DGUSScreenVariableHandler::update_ptr;
  56. uint16_t DGUSScreenVariableHandler::skipVP;
  57. bool DGUSScreenVariableHandler::ScreenComplete;
  58. //DGUSDisplay dgusdisplay;
  59. rx_datagram_state_t DGUSDisplay::rx_datagram_state = DGUS_IDLE;
  60. uint8_t DGUSDisplay::rx_datagram_len = 0;
  61. bool DGUSDisplay::Initialized = false;
  62. bool DGUSDisplay::no_reentrance = false;
  63. #if DGUS_RX_BUFFER_SIZE > 256
  64. typedef uint16_t r_ring_buffer_pos_t;
  65. #else
  66. typedef uint8_t r_ring_buffer_pos_t;
  67. #endif
  68. #if DGUS_TX_BUFFER_SIZE > 256
  69. typedef uint16_t t_ring_buffer_pos_t;
  70. #else
  71. typedef uint8_t t_ring_buffer_pos_t;
  72. #endif
  73. class DGUSSerial {
  74. public:
  75. DGUSSerial();
  76. ~DGUSSerial();
  77. r_ring_buffer_pos_t available();
  78. t_ring_buffer_pos_t GetTxBufferFree();
  79. void write(const uint8_t c);
  80. int read();
  81. // ISR for Rx
  82. void store_rxd_char();
  83. // ISR for Tx (UDRE vector)
  84. void tx_udr_empty_irq();
  85. inline volatile bool is_rx_overrun() {
  86. return dgus_rx_overrun;
  87. }
  88. inline void reset_rx_overun() {
  89. dgus_rx_overrun = false;
  90. }
  91. private:
  92. r_ring_buffer_pos_t atomic_read_rx_head();
  93. void atomic_set_rx_tail(r_ring_buffer_pos_t value);
  94. r_ring_buffer_pos_t atomic_read_rx_tail();
  95. volatile bool dgus_rx_overrun = false;
  96. struct ring_buffer_r {
  97. volatile r_ring_buffer_pos_t head, tail;
  98. unsigned char buffer[DGUS_RX_BUFFER_SIZE];
  99. } rx_buffer = { 0, 0, { 0 } };
  100. struct ring_buffer_t {
  101. volatile t_ring_buffer_pos_t head, tail;
  102. unsigned char buffer[DGUS_TX_BUFFER_SIZE];
  103. } tx_buffer = { 0, 0, { 0 } };
  104. #if DGUS_RX_BUFFER_SIZE > 256
  105. volatile bool rx_tail_value_not_stable = false;
  106. volatile uint16_t rx_tail_value_backup = 0;
  107. #endif
  108. };
  109. static DGUSSerial dgusserial;
  110. // endianness swap
  111. uint16_t swap16(const uint16_t value) { return (value & 0xffU) << 8U | (value >> 8U); }
  112. bool populate_VPVar(const uint16_t VP, DGUS_VP_Variable * const ramcopy) {
  113. // DEBUG_ECHOPAIR("populate_VPVar ", VP);
  114. const DGUS_VP_Variable *pvp = DGUSLCD_FindVPVar(VP);
  115. // DEBUG_ECHOLNPAIR(" pvp ", (uint16_t )pvp);
  116. if (!pvp) return false;
  117. memcpy_P(ramcopy, pvp, sizeof(DGUS_VP_Variable));
  118. return true;
  119. }
  120. void DGUSScreenVariableHandler::sendinfoscreen(const char* line1, const char* line2, const char* line3, const char* line4, bool l1inflash, bool l2inflash, bool l3inflash, bool l4inflash) {
  121. DGUS_VP_Variable ramcopy;
  122. if (populate_VPVar(VP_MSGSTR1, &ramcopy)) {
  123. ramcopy.memadr = (void*) line1;
  124. l1inflash ? DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  125. }
  126. if (populate_VPVar(VP_MSGSTR2, &ramcopy)) {
  127. ramcopy.memadr = (void*) line2;
  128. l2inflash ? DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  129. }
  130. if (populate_VPVar(VP_MSGSTR3, &ramcopy)) {
  131. ramcopy.memadr = (void*) line3;
  132. l3inflash ? DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  133. }
  134. if (populate_VPVar(VP_MSGSTR4, &ramcopy)) {
  135. ramcopy.memadr = (void*) line4;
  136. l4inflash ? DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplayPGM(ramcopy) : DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplay(ramcopy);
  137. }
  138. }
  139. void DGUSScreenVariableHandler::HandleUserConfirmationPopUp(uint16_t VP, const char* line1, const char* line2, const char* line3, const char* line4, bool l1, bool l2, bool l3, bool l4) {
  140. if (current_screen == DGUSLCD_SCREEN_CONFIRM) {
  141. // Already showing a pop up, so we need to cancel that first.
  142. PopToOldScreen();
  143. }
  144. ConfirmVP = VP;
  145. sendinfoscreen(line1, line2, line3, line4, l1, l2, l3, l4);
  146. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_CONFIRM);
  147. }
  148. void DGUSScreenVariableHandler::setstatusmessage(const char *msg) {
  149. DGUS_VP_Variable ramcopy;
  150. if (populate_VPVar(VP_M117, &ramcopy)) {
  151. ramcopy.memadr = (void*) msg;
  152. DGUSLCD_SendStringToDisplay(ramcopy);
  153. }
  154. }
  155. void DGUSScreenVariableHandler::setstatusmessagePGM(PGM_P const msg) {
  156. DGUS_VP_Variable ramcopy;
  157. if (populate_VPVar(VP_M117, &ramcopy)) {
  158. ramcopy.memadr = (void*) msg;
  159. DGUSLCD_SendStringToDisplayPGM(ramcopy);
  160. }
  161. }
  162. // Send an 8 bit or 16 bit value to the display.
  163. void DGUSScreenVariableHandler::DGUSLCD_SendWordValueToDisplay(DGUS_VP_Variable &var) {
  164. if (var.memadr) {
  165. //DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP);
  166. //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr);
  167. uint8_t *tmp = (uint8_t *) var.memadr;
  168. uint16_t data_to_send = (tmp[0] << 8);
  169. if (var.size >= 1) data_to_send |= tmp[1];
  170. dgusdisplay.WriteVariable(var.VP, data_to_send);
  171. }
  172. }
  173. // Send an uint8_t between 0 and 255 to the display, but scale to a percentage (0..100)
  174. void DGUSScreenVariableHandler::DGUSLCD_SendPercentageToDisplay(DGUS_VP_Variable &var) {
  175. if (var.memadr) {
  176. //DEBUG_ECHOPAIR(" DGUS_LCD_SendWordValueToDisplay ", var.VP);
  177. //DEBUG_ECHOLNPAIR(" data ", *(uint16_t *)var.memadr);
  178. uint16_t tmp = *(uint8_t *) var.memadr +1 ; // +1 -> avoid rounding issues for the display.
  179. tmp = map(tmp, 0, 255, 0, 100);
  180. uint16_t data_to_send = swap16(tmp);
  181. dgusdisplay.WriteVariable(var.VP, data_to_send);
  182. }
  183. }
  184. // Send the current print time to the display.
  185. // It is using a hex display for that: It expects BSD coded data in the format xxyyzz
  186. void DGUSScreenVariableHandler::DGUSLCD_SendPrintTimeToDisplay(DGUS_VP_Variable &var) {
  187. duration_t elapsed = print_job_timer.duration();
  188. uint8_t days = elapsed.day(),
  189. hours = elapsed.hour() % 24,
  190. minutes = elapsed.minute() % 60,
  191. seconds = elapsed.second() % 60;
  192. char buf[14], *p = buf; // that two extra bytes saves us some flash...
  193. if (days) { *p++ = days / 10 + '0'; *p++ = days % 10 + '0'; *p++ = 'd'; }
  194. *p++ = hours / 10 + '0'; *p++ = hours % 10 + '0'; *p++ = 'h';
  195. *p++ = minutes / 10 + '0'; *p++ = minutes % 10 + '0'; *p++ = 'm';
  196. *p++ = seconds / 10 + '0'; *p++ = seconds % 10 + '0'; *p++ = 's';
  197. *p = '\0';
  198. dgusdisplay.WriteVariable(VP_PrintTime, buf, var.size, true);
  199. }
  200. // Send an uint8_t between 0 and 100 to a variable scale to 0..255
  201. void DGUSScreenVariableHandler::DGUSLCD_PercentageToUint8(DGUS_VP_Variable &var, void *val_ptr) {
  202. if (var.memadr) {
  203. uint16_t value = swap16(*(uint16_t*)val_ptr);
  204. *(uint8_t*)var.memadr = map(constrain(value, 0, 100), 0, 100, 0, 255);
  205. }
  206. }
  207. // Sends a (RAM located) string to the DGUS Display
  208. // (Note: The DGUS Display does not clear after the \0, you have to
  209. // overwrite the remainings with spaces.// var.size has the display buffer size!
  210. void DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplay(DGUS_VP_Variable &var) {
  211. char *tmp = (char*) var.memadr;
  212. dgusdisplay.WriteVariable(var.VP, tmp, var.size, true);
  213. }
  214. // Sends a (flash located) string to the DGUS Display
  215. // (Note: The DGUS Display does not clear after the \0, you have to
  216. // overwrite the remainings with spaces.// var.size has the display buffer size!
  217. void DGUSScreenVariableHandler::DGUSLCD_SendStringToDisplayPGM(DGUS_VP_Variable &var) {
  218. char *tmp = (char*) var.memadr;
  219. dgusdisplay.WriteVariablePGM(var.VP, tmp, var.size, true);
  220. }
  221. #if ENABLED(SDSUPPORT)
  222. void DGUSScreenVariableHandler::ScreenChangeHookIfSD(DGUS_VP_Variable &var, void *val_ptr) {
  223. // default action executed when there is a SD card, but not printing
  224. if (ExtUI::isMediaInserted() && !ExtUI::isPrintingFromMedia()) {
  225. ScreenChangeHook(var, val_ptr);
  226. dgusdisplay.RequestScreen(current_screen);
  227. return;
  228. }
  229. // if we are printing, we jump to two screens after the requested one.
  230. // This should host e.g a print pause / print abort / print resume dialog.
  231. // This concept allows to recycle this hook for other file
  232. if (ExtUI::isPrintingFromMedia() && !card.flag.abort_sd_printing) {
  233. GotoScreen(DGUSLCD_SCREEN_SDPRINTMANIPULATION);
  234. return;
  235. }
  236. // Don't let the user in the dark why there is no reaction.
  237. if (!ExtUI::isMediaInserted()) {
  238. setstatusmessagePGM(GET_TEXT(MSG_NO_MEDIA));
  239. return;
  240. }
  241. if (card.flag.abort_sd_printing) {
  242. setstatusmessagePGM(GET_TEXT(MSG_MEDIA_ABORTING));
  243. return;
  244. }
  245. }
  246. void DGUSScreenVariableHandler::DGUSLCD_SD_ScrollFilelist(DGUS_VP_Variable& var, void *val_ptr) {
  247. auto old_top = top_file;
  248. int16_t scroll = (int16_t)swap16(*(uint16_t*)val_ptr);
  249. if (scroll == 0) {
  250. if (!filelist.isAtRootDir()) {
  251. filelist.upDir();
  252. top_file = 0;
  253. ForceCompleteUpdate();
  254. }
  255. }
  256. else {
  257. top_file += scroll;
  258. DEBUG_ECHOPAIR("new topfile calculated:", top_file);
  259. if (top_file < 0) {
  260. top_file = 0;
  261. DEBUG_ECHOLNPGM("Top of filelist reached");
  262. }
  263. else {
  264. int16_t max_top = filelist.count() - DGUS_SD_FILESPERSCREEN;
  265. NOLESS(max_top, 0);
  266. NOMORE(top_file, max_top);
  267. }
  268. DEBUG_ECHOPAIR("new topfile adjusted:", top_file);
  269. }
  270. if (old_top != top_file) ForceCompleteUpdate();
  271. }
  272. void DGUSScreenVariableHandler::DGUSLCD_SD_FileSelected(DGUS_VP_Variable &var, void *val_ptr) {
  273. uint16_t touched_nr = (int16_t)swap16(*(uint16_t*)val_ptr) + top_file;
  274. if (touched_nr > filelist.count()) return;
  275. if (!filelist.seek(touched_nr)) return;
  276. if (filelist.isDir()) {
  277. filelist.changeDir(filelist.filename());
  278. top_file = 0;
  279. ForceCompleteUpdate();
  280. return;
  281. }
  282. // Setup Confirmation screen
  283. file_to_print = touched_nr;
  284. HandleUserConfirmationPopUp(VP_SD_FileSelectConfirm, nullptr, PSTR("Print file"), filelist.filename(), PSTR("from SD Card?"), true, true, false, true);
  285. }
  286. void DGUSScreenVariableHandler::DGUSLCD_SD_StartPrint(DGUS_VP_Variable &var, void *val_ptr) {
  287. if (!filelist.seek(file_to_print)) return;
  288. ExtUI::printFile(filelist.filename());
  289. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_STATUS);
  290. }
  291. void DGUSScreenVariableHandler::DGUSLCD_SD_ResumePauseAbort(DGUS_VP_Variable &var, void *val_ptr) {
  292. if (!ExtUI::isPrintingFromMedia()) return; // avoid race condition when user stays in this menu and printer finishes.
  293. switch (swap16(*(uint16_t*)val_ptr)) {
  294. case 0: // Resume
  295. if (ExtUI::isPrintingFromMediaPaused()) ExtUI::resumePrint();
  296. break;
  297. case 1: // Pause
  298. if (!ExtUI::isPrintingFromMediaPaused()) ExtUI::pausePrint();
  299. break;
  300. case 2: // Abort
  301. ScreenHandler.HandleUserConfirmationPopUp(VP_SD_AbortPrintConfirmed, nullptr, PSTR("Abort printing"), filelist.filename(), PSTR("?"), true, true, false, true);
  302. break;
  303. }
  304. }
  305. void DGUSScreenVariableHandler::DGUSLCD_SD_ReallyAbort(DGUS_VP_Variable &var, void *val_ptr) {
  306. ExtUI::stopPrint();
  307. GotoScreen(DGUSLCD_SCREEN_MAIN);
  308. }
  309. void DGUSScreenVariableHandler::DGUSLCD_SD_SendFilename(DGUS_VP_Variable& var) {
  310. uint16_t target_line = (var.VP - VP_SD_FileName0) / VP_SD_FileName_LEN;
  311. if (target_line > DGUS_SD_FILESPERSCREEN) return;
  312. char tmpfilename[VP_SD_FileName_LEN + 1] = "";
  313. var.memadr = (void*)tmpfilename;
  314. if (filelist.seek(top_file + target_line))
  315. snprintf_P(tmpfilename, VP_SD_FileName_LEN, PSTR("%s%c"), filelist.filename(), filelist.isDir() ? '/' : 0);
  316. DGUSLCD_SendStringToDisplay(var);
  317. }
  318. void DGUSScreenVariableHandler::SDCardInserted() {
  319. top_file = 0;
  320. auto cs = ScreenHandler.getCurrentScreen();
  321. if (cs == DGUSLCD_SCREEN_MAIN || cs == DGUSLCD_SCREEN_STATUS)
  322. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_SDFILELIST);
  323. }
  324. void DGUSScreenVariableHandler::SDCardRemoved() {
  325. if (current_screen == DGUSLCD_SCREEN_SDFILELIST
  326. || (current_screen == DGUSLCD_SCREEN_CONFIRM && (ConfirmVP == VP_SD_AbortPrintConfirmed || ConfirmVP == VP_SD_FileSelectConfirm))
  327. || current_screen == DGUSLCD_SCREEN_SDPRINTMANIPULATION
  328. ) ScreenHandler.GotoScreen(DGUSLCD_SCREEN_MAIN);
  329. }
  330. void DGUSScreenVariableHandler::SDCardError() {
  331. DGUSScreenVariableHandler::SDCardRemoved();
  332. ScreenHandler.sendinfoscreen(PSTR("NOTICE"), nullptr, PSTR("SD card error"), nullptr, true, true, true, true);
  333. ScreenHandler.SetupConfirmAction(nullptr);
  334. ScreenHandler.GotoScreen(DGUSLCD_SCREEN_POPUP);
  335. }
  336. #endif // SDSUPPORT
  337. void DGUSScreenVariableHandler::ScreenConfirmedOK(DGUS_VP_Variable &var, void *val_ptr) {
  338. DGUS_VP_Variable ramcopy;
  339. if (!populate_VPVar(ConfirmVP, &ramcopy)) return;
  340. if (ramcopy.set_by_display_handler) ramcopy.set_by_display_handler(ramcopy, val_ptr);
  341. }
  342. const uint16_t* DGUSLCD_FindScreenVPMapList(uint8_t screen) {
  343. const uint16_t *ret;
  344. const struct VPMapping *map = VPMap;
  345. while (ret = (uint16_t*) pgm_read_word(&(map->VPList))) {
  346. if (pgm_read_byte(&(map->screen)) == screen) return ret;
  347. map++;
  348. }
  349. return nullptr;
  350. }
  351. const DGUS_VP_Variable* DGUSLCD_FindVPVar(const uint16_t vp) {
  352. const DGUS_VP_Variable *ret = ListOfVP;
  353. do {
  354. const uint16_t vpcheck = pgm_read_word(&(ret->VP));
  355. if (vpcheck == 0) break;
  356. if (vpcheck == vp) return ret;
  357. ++ret;
  358. } while (1);
  359. DEBUG_ECHOLNPAIR("FindVPVar NOT FOUND ", vp);
  360. return nullptr;
  361. }
  362. void DGUSScreenVariableHandler::ScreenChangeHookIfIdle(DGUS_VP_Variable &var, void *val_ptr) {
  363. if (!ExtUI::isPrinting()) {
  364. ScreenChangeHook(var, val_ptr);
  365. dgusdisplay.RequestScreen(current_screen);
  366. }
  367. }
  368. void DGUSScreenVariableHandler::ScreenChangeHook(DGUS_VP_Variable &var, void *val_ptr) {
  369. uint8_t *tmp = (uint8_t*)val_ptr;
  370. // The keycode in target is coded as <from-frame><to-frame>, so 0x0100A means
  371. // from screen 1 (main) to 10 (temperature). DGUSLCD_SCREEN_POPUP is special,
  372. // meaning "return to previous screen"
  373. DGUSLCD_Screens target = (DGUSLCD_Screens)tmp[1];
  374. if (target == DGUSLCD_SCREEN_POPUP) {
  375. // special handling for popup is to return to previous menu
  376. if (current_screen == DGUSLCD_SCREEN_POPUP && confirm_action_cb) confirm_action_cb();
  377. PopToOldScreen();
  378. return;
  379. }
  380. UpdateNewScreen(target);
  381. #ifdef DEBUG_DGUSLCD
  382. if (!DGUSLCD_FindScreenVPMapList(target)) DEBUG_ECHOLNPAIR("WARNING: No screen Mapping found for ", x);
  383. #endif
  384. }
  385. void DGUSScreenVariableHandler::HandleAllHeatersOff(DGUS_VP_Variable &var, void *val_ptr) {
  386. thermalManager.disable_all_heaters();
  387. ScreenHandler.ForceCompleteUpdate(); // hint to send all data.
  388. }
  389. void DGUSScreenVariableHandler::HandleTemperatureChanged(DGUS_VP_Variable &var, void *val_ptr) {
  390. uint16_t newvalue = swap16(*(uint16_t*)val_ptr);
  391. uint16_t acceptedvalue;
  392. switch (var.VP) {
  393. default: return;
  394. #if HOTENDS >= 1
  395. case VP_T_E0_Set:
  396. thermalManager.setTargetHotend(newvalue, 0);
  397. acceptedvalue = thermalManager.temp_hotend[0].target;
  398. break;
  399. #endif
  400. #if HOTENDS >= 2
  401. case VP_T_E1_Set:
  402. thermalManager.setTargetHotend(newvalue, 1);
  403. acceptedvalue = thermalManager.temp_hotend[1].target;
  404. break;
  405. #endif
  406. #if HAS_HEATED_BED
  407. case VP_T_Bed_Set:
  408. thermalManager.setTargetBed(newvalue);
  409. acceptedvalue = thermalManager.temp_bed.target;
  410. break;
  411. #endif
  412. }
  413. // reply to display the new value to update the view if the new value was rejected by the Thermal Manager.
  414. if (newvalue != acceptedvalue && var.send_to_display_handler) var.send_to_display_handler(var);
  415. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  416. }
  417. void DGUSScreenVariableHandler::HandleFlowRateChanged(DGUS_VP_Variable &var, void *val_ptr) {
  418. #if EXTRUDERS
  419. uint16_t newvalue = swap16(*(uint16_t*)val_ptr);
  420. uint8_t target_extruder;
  421. switch (var.VP) {
  422. default: return;
  423. #if (HOTENDS >= 1)
  424. case VP_Flowrate_E0: target_extruder = 0; break;
  425. #endif
  426. #if (HOTENDS >= 2)
  427. case VP_Flowrate_E1: target_extruder = 1; break;
  428. #endif
  429. }
  430. planner.flow_percentage[target_extruder] = newvalue;
  431. planner.refresh_e_factor(target_extruder);
  432. ScreenHandler.skipVP = var.VP; // don't overwrite value the next update time as the display might autoincrement in parallel
  433. #else
  434. UNUSED(var); UNUSED(val_ptr);
  435. #endif
  436. }
  437. void DGUSScreenVariableHandler::HandleManualExtrude(DGUS_VP_Variable &var, void *val_ptr) {
  438. DEBUG_ECHOLNPGM("HandleManualMove");
  439. int16_t movevalue = swap16(*(uint16_t*)val_ptr);
  440. float target = movevalue * 0.01f;
  441. ExtUI::extruder_t target_extruder;
  442. switch (var.VP) {
  443. #if HOTENDS >= 1
  444. case VP_MOVE_E0: target_extruder = ExtUI::extruder_t::E0; break;
  445. #endif
  446. #if HOTENDS >= 2
  447. case VP_MOVE_E1: target_extruder = ExtUI::extruder_t::E1; break
  448. #endif
  449. default: return;
  450. }
  451. target += ExtUI::getAxisPosition_mm(target_extruder);
  452. ExtUI::setAxisPosition_mm(target, target_extruder);
  453. skipVP = var.VP;
  454. }
  455. void DGUSScreenVariableHandler::HandleManualMove(DGUS_VP_Variable &var, void *val_ptr) {
  456. DEBUG_ECHOLNPGM("HandleManualMove");
  457. int16_t movevalue = swap16(*(uint16_t*)val_ptr);
  458. char axiscode;
  459. unsigned int speed = 1500; //FIXME: get default feedrate for manual moves, dont hardcode.
  460. switch (var.VP) {
  461. case VP_MOVE_X:
  462. axiscode = 'X';
  463. if (!ExtUI::canMove(ExtUI::axis_t::X)) goto cannotmove;
  464. break;
  465. case VP_MOVE_Y:
  466. axiscode = 'Y';
  467. if (!ExtUI::canMove(ExtUI::axis_t::Y)) goto cannotmove;
  468. break;
  469. case VP_MOVE_Z:
  470. axiscode = 'Z';
  471. speed = 300; // default to 5mm/s
  472. if (!ExtUI::canMove(ExtUI::axis_t::Z)) goto cannotmove;
  473. break;
  474. case VP_HOME_ALL: // only used for homing
  475. axiscode = '\0';
  476. movevalue = 0; // ignore value sent from display, this VP is _ONLY_ for homing.
  477. break;
  478. default: return;
  479. }
  480. if (!movevalue) {
  481. // homing
  482. DEBUG_ECHOPAIR(" homing ", axiscode);
  483. char buf[6] = "G28 X";
  484. buf[4] = axiscode;
  485. //DEBUG_ECHOPAIR(" ", buf);
  486. while (!enqueue_and_echo_command(buf)) idle();
  487. //DEBUG_ECHOLNPGM(" ✓");
  488. ScreenHandler.ForceCompleteUpdate();
  489. return;
  490. }
  491. else {
  492. //movement
  493. DEBUG_ECHOPAIR(" move ", axiscode);
  494. bool old_relative_mode = relative_mode;
  495. if (!relative_mode) {
  496. //DEBUG_ECHOPGM(" G91");
  497. while (!enqueue_and_echo_command("G91")) idle();
  498. //DEBUG_ECHOPGM(" ✓ ");
  499. }
  500. char buf[32]; // G1 X9999.99 F12345
  501. unsigned int backup_speed = MMS_TO_MMM(feedrate_mm_s);
  502. char sign[]="\0";
  503. int16_t value = movevalue / 100;
  504. if (movevalue < 0) { value = -value; sign[0] = '-'; }
  505. int16_t fraction = ABS(movevalue) % 100;
  506. snprintf_P(buf, 32, PSTR("G0 %c%s%d.%02d F%d"), axiscode, sign, value, fraction, speed);
  507. //DEBUG_ECHOPAIR(" ", buf);
  508. while (!enqueue_and_echo_command(buf)) idle();
  509. //DEBUG_ECHOLNPGM(" ✓ ");
  510. if (backup_speed != speed) {
  511. snprintf_P(buf, 32, PSTR("G0 F%d"), backup_speed);
  512. while (!enqueue_and_echo_command(buf)) idle();
  513. //DEBUG_ECHOPAIR(" ", buf);
  514. }
  515. //while (!enqueue_and_echo_command(buf)) idle();
  516. //DEBUG_ECHOLNPGM(" ✓ ");
  517. if (!old_relative_mode) {
  518. //DEBUG_ECHOPGM("G90");
  519. while (!enqueue_and_echo_command("G90")) idle();
  520. //DEBUG_ECHOPGM(" ✓ ");
  521. }
  522. }
  523. ScreenHandler.ForceCompleteUpdate();
  524. DEBUG_ECHOLNPGM("manmv done.");
  525. return;
  526. cannotmove:
  527. DEBUG_ECHOLNPAIR(" cannot move ", axiscode);
  528. return;
  529. }
  530. void DGUSScreenVariableHandler::UpdateNewScreen(DGUSLCD_Screens newscreen, bool popup) {
  531. DEBUG_ECHOLNPAIR("SetNewScreen: ", newscreen);
  532. if (!popup) {
  533. memmove(&past_screens[1], &past_screens[0], sizeof(past_screens) - 1);
  534. past_screens[0] = current_screen;
  535. }
  536. current_screen = newscreen;
  537. skipVP = 0;
  538. ForceCompleteUpdate();
  539. }
  540. void DGUSScreenVariableHandler::PopToOldScreen() {
  541. DEBUG_ECHOLNPAIR("PopToOldScreen s=", past_screens[0]);
  542. GotoScreen(past_screens[0], true);
  543. memmove(&past_screens[0], &past_screens[1], sizeof(past_screens) - 1);
  544. past_screens[sizeof(past_screens) - 1] = DGUSLCD_SCREEN_MAIN;
  545. }
  546. void DGUSScreenVariableHandler::UpdateScreenVPData() {
  547. DEBUG_ECHOPAIR(" UpdateScreenVPData Screen: ", current_screen);
  548. const uint16_t *VPList = DGUSLCD_FindScreenVPMapList(current_screen);
  549. if (!VPList) {
  550. DEBUG_ECHOLNPAIR(" NO SCREEN FOR: ", current_screen);
  551. ScreenComplete = true;
  552. return; // nothing to do, likely a bug or boring screen.
  553. }
  554. // Round-Robbin updating of all VPs.
  555. VPList += update_ptr;
  556. bool sent_one = false;
  557. do {
  558. uint16_t VP = pgm_read_word(VPList);
  559. DEBUG_ECHOPAIR(" VP: ", VP);
  560. if (!VP) {
  561. update_ptr = 0;
  562. DEBUG_ECHOLNPGM(" UpdateScreenVPData done");
  563. ScreenComplete = true;
  564. return; // Screen completed.
  565. }
  566. if (VP == skipVP) {
  567. skipVP = 0;
  568. continue;
  569. }
  570. DGUS_VP_Variable rcpy;
  571. if (populate_VPVar(VP, &rcpy)) {
  572. uint8_t expected_tx = 6 + rcpy.size; // expected overhead is 6 bytes + payload.
  573. // Send the VP to the display, but try to avoid overruning the Tx Buffer.
  574. // But send at least one VP, to avoid getting stalled.
  575. if (rcpy.send_to_display_handler && (!sent_one || expected_tx <= dgusdisplay.GetFreeTxBuffer())) {
  576. //DEBUG_ECHOPAIR(" calling handler for ", rcpy.VP);
  577. sent_one = true;
  578. rcpy.send_to_display_handler(rcpy);
  579. }
  580. else {
  581. //auto x=dgusdisplay.GetFreeTxBuffer();
  582. //DEBUG_ECHOLNPAIR(" tx almost full: ", x);
  583. //DEBUG_ECHOPAIR(" update_ptr ", update_ptr);
  584. ScreenComplete = false;
  585. return; // please call again!
  586. }
  587. }
  588. } while (++update_ptr, ++VPList, true);
  589. }
  590. void DGUSDisplay::loop() {
  591. // protection against recursion… ProcessRx() might call indirectly idle() when trying to injecting gcode commands if the queue is full.
  592. if (!no_reentrance) {
  593. no_reentrance = true;
  594. ProcessRx();
  595. no_reentrance = false;
  596. }
  597. }
  598. void DGUSDisplay::InitDisplay() {
  599. RequestScreen(
  600. #if ENABLED(SHOW_BOOTSCREEN)
  601. DGUSLCD_SCREEN_BOOT
  602. #else
  603. DGUSLCD_SCREEN_MAIN
  604. #endif
  605. );
  606. }
  607. void DGUSDisplay::WriteVariable(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) {
  608. const char* myvalues = static_cast<const char*>(values);
  609. bool strend = myvalues ? false : true;
  610. WriteHeader(adr, DGUS_CMD_WRITEVAR, valueslen);
  611. while (valueslen--) {
  612. char x;
  613. if (!strend) x = *myvalues++;
  614. if ((isstr && !x) || strend) {
  615. strend = true;
  616. x = ' ';
  617. }
  618. dgusserial.write(x);
  619. }
  620. }
  621. void DGUSDisplay::WriteVariablePGM(uint16_t adr, const void* values, uint8_t valueslen, bool isstr) {
  622. const char* myvalues = static_cast<const char*>(values);
  623. bool strend = myvalues ? false : true;
  624. WriteHeader(adr, DGUS_CMD_WRITEVAR, valueslen);
  625. while (valueslen--) {
  626. char x;
  627. if (!strend) x = pgm_read_byte(myvalues++);
  628. if ((isstr && !x) || strend) {
  629. strend = true;
  630. x = ' ';
  631. }
  632. dgusserial.write(x);
  633. }
  634. }
  635. void DGUSScreenVariableHandler::GotoScreen(DGUSLCD_Screens screen, bool ispopup) {
  636. dgusdisplay.RequestScreen(screen);
  637. UpdateNewScreen(screen, ispopup);
  638. }
  639. bool DGUSScreenVariableHandler::loop() {
  640. dgusdisplay.loop();
  641. const millis_t ms = millis();
  642. static millis_t next_event_ms = 0;
  643. if (!IsScreenComplete() || ELAPSED(ms, next_event_ms)) {
  644. next_event_ms = ms + DGUS_UPDATE_INTERVAL_MS;
  645. UpdateScreenVPData();
  646. }
  647. #if ENABLED(SHOW_BOOTSCREEN)
  648. static bool booted = false;
  649. if (!booted && ELAPSED(ms, BOOTSCREEN_TIMEOUT)) {
  650. booted = true;
  651. GotoScreen(DGUSLCD_SCREEN_MAIN);
  652. }
  653. #endif
  654. return IsScreenComplete();
  655. }
  656. void DGUSDisplay::RequestScreen(DGUSLCD_Screens screen) {
  657. DEBUG_ECHOLNPAIR("GotoScreen ", screen);
  658. const unsigned char gotoscreen[] = { 0x5A, 0x01, (unsigned char) (screen >> 8U), (unsigned char) (screen & 0xFFU) };
  659. WriteVariable(0x84, gotoscreen, sizeof(gotoscreen));
  660. }
  661. void DGUSDisplay::ProcessRx() {
  662. if (!dgusserial.available() && dgusserial.is_rx_overrun()) {
  663. // if we've got an overrun, but reset the flag only when we've emptied the buffer
  664. // We want to extract as many as valid datagrams possible...
  665. DEBUG_ECHOPGM("OVFL");
  666. rx_datagram_state = DGUS_IDLE;
  667. dgusserial.reset_rx_overun();
  668. }
  669. uint8_t receivedbyte;
  670. while (dgusserial.available()) {
  671. switch (rx_datagram_state) {
  672. case DGUS_IDLE: // Waiting for the first header byte
  673. receivedbyte = dgusserial.read();
  674. //DEBUG_ECHOPAIR("< ",x);
  675. if (DGUS_HEADER1 == receivedbyte) rx_datagram_state = DGUS_HEADER1_SEEN;
  676. break;
  677. case DGUS_HEADER1_SEEN: // Waiting for the second header byte
  678. receivedbyte = dgusserial.read();
  679. //DEBUG_ECHOPAIR(" ",x);
  680. rx_datagram_state = (DGUS_HEADER2 == receivedbyte) ? DGUS_HEADER2_SEEN : DGUS_IDLE;
  681. break;
  682. case DGUS_HEADER2_SEEN: // Waiting for the length byte
  683. rx_datagram_len = dgusserial.read();
  684. DEBUG_ECHOPAIR(" (", rx_datagram_len);
  685. DEBUG_ECHOPGM(") ");
  686. // Telegram min len is 3 (command and one word of payload)
  687. rx_datagram_state = WITHIN(rx_datagram_len, 3, DGUS_RX_BUFFER_SIZE) ? DGUS_WAIT_TELEGRAM : DGUS_IDLE;
  688. break;
  689. case DGUS_WAIT_TELEGRAM: // wait for complete datagram to arrive.
  690. if (dgusserial.available() < rx_datagram_len) return;
  691. Initialized = true; // We've talked to it, so we defined it as initialized.
  692. uint8_t command = dgusserial.read();
  693. DEBUG_ECHOPAIR("# ", command);
  694. uint8_t readlen = rx_datagram_len - 1; // command is part of len.
  695. unsigned char tmp[rx_datagram_len - 1];
  696. unsigned char *ptmp = tmp;
  697. while (readlen--) {
  698. receivedbyte = dgusserial.read();
  699. DEBUG_ECHOPAIR(" ", receivedbyte);
  700. *ptmp++ = receivedbyte;
  701. }
  702. DEBUG_ECHOPGM(" # ");
  703. // mostly we'll get this: 5A A5 03 82 4F 4B -- ACK on 0x82, so discard it.
  704. if (command == DGUS_CMD_WRITEVAR && 'O' == tmp[0] && 'K' == tmp[1]) {
  705. DEBUG_ECHOLNPGM(">");
  706. rx_datagram_state = DGUS_IDLE;
  707. break;
  708. }
  709. /* AutoUpload, (and answer to) Command 0x83 :
  710. | tmp[0 1 2 3 4 ... ]
  711. | Example 5A A5 06 83 20 01 01 78 01 ……
  712. | / / | | \ / | \ \
  713. | Header | | | | \_____\_ DATA (Words!)
  714. | DatagramLen / VPAdr |
  715. | Command DataLen (in Words) */
  716. if (command == DGUS_CMD_READVAR) {
  717. const uint16_t vp = tmp[0] << 8 | tmp[1];
  718. const uint8_t dlen = tmp[2] << 1; // Convert to Bytes. (Display works with words)
  719. //DEBUG_ECHOPAIR(" vp=", vp, " dlen=", dlen);
  720. DGUS_VP_Variable ramcopy;
  721. if (populate_VPVar(vp, &ramcopy)) {
  722. if (!(dlen == ramcopy.size || (dlen == 2 && ramcopy.size == 1)))
  723. DEBUG_ECHOLNPGM("SIZE MISMATCH");
  724. else if (ramcopy.set_by_display_handler) {
  725. ramcopy.set_by_display_handler(ramcopy, &tmp[3]);
  726. }
  727. else
  728. DEBUG_ECHOLNPGM(" VPVar found, no handler.");
  729. }
  730. else
  731. DEBUG_ECHOLNPAIR(" VPVar not found:", vp);
  732. rx_datagram_state = DGUS_IDLE;
  733. break;
  734. }
  735. // discard what we do not understand.
  736. rx_datagram_state = DGUS_IDLE;
  737. }
  738. }
  739. }
  740. size_t DGUSDisplay::GetFreeTxBuffer() { return dgusserial.GetTxBufferFree(); }
  741. void DGUSDisplay::WriteHeader(uint16_t adr, uint8_t cmd, uint8_t payloadlen) {
  742. dgusserial.write(DGUS_HEADER1);
  743. dgusserial.write(DGUS_HEADER2);
  744. dgusserial.write(payloadlen + 3);
  745. dgusserial.write(cmd);
  746. dgusserial.write(adr >> 8);
  747. dgusserial.write(adr & 0xFF);
  748. }
  749. void DGUSDisplay::WritePGM(const char str[], uint8_t len) {
  750. while (len--) dgusserial.write(pgm_read_byte(str++));
  751. }
  752. // Serial implementation stolen from MarlinSerial.cpp -- but functinality reduced to our use case
  753. // (no XON/XOFF, no Emergency Parser, no error statistics, no support to send from interrupts ...)
  754. // Define all UART registers
  755. #define _TNAME(X,Y,Z) X##Y##Z
  756. #define TNAME(X,Y,Z) _TNAME(X,Y,Z)
  757. #define DGUS_SERIAL_RX_VECT TNAME(USART,DGUS_SER_PORT,_RX_vect)
  758. #define DGUS_SERIAL_UDRE_VECT TNAME(USART,DGUS_SER_PORT,_UDRE_vect)
  759. #define DGUS_UCSRxA TNAME(UCSR,DGUS_SER_PORT,A)
  760. #define DGUS_UCSRxB TNAME(UCSR,DGUS_SER_PORT,B)
  761. #define DGUS_UCSRxC TNAME(UCSR,DGUS_SER_PORT,C)
  762. #define DGUS_UBRRxH TNAME(UBRR,DGUS_SER_PORT,H)
  763. #define DGUS_UBRRxL TNAME(UBRR,DGUS_SER_PORT,L)
  764. #define DGUS_UDRx TNAME(UDR,DGUS_SER_PORT,)
  765. #define U2Xx TNAME(U2X,DGUS_SER_PORT,)
  766. #define RXENx TNAME(RXEN,DGUS_SER_PORT,)
  767. #define TXENx TNAME(TXEN,DGUS_SER_PORT,)
  768. #define TXCx TNAME(TXC,DGUS_SER_PORT,)
  769. #define RXCIEx TNAME(RXCIE,DGUS_SER_PORT,)
  770. #define UDRIEx TNAME(UDRIE,DGUS_SER_PORT,)
  771. #define UDREx TNAME(UDRE,DGUS_SER_PORT,)
  772. // A SW memory barrier, to ensure GCC does not overoptimize loops
  773. #define sw_barrier() asm volatile("": : :"memory");
  774. DGUSSerial::DGUSSerial() {
  775. // Initialize UART
  776. DGUS_UCSRxA = 1 << U2Xx;
  777. const uint16_t baud_setting = (F_CPU / 4 / DGUS_BAUDRATE - 1) / 2;
  778. DGUS_UBRRxH = baud_setting >> 8;
  779. DGUS_UBRRxL = baud_setting;
  780. DGUS_UCSRxC = 0x06;
  781. DGUS_UCSRxB = 1 << RXCIEx | 1 << TXENx | 1 << RXENx; // Enable TX,RX and the RX interrupts.
  782. }
  783. DGUSSerial::~DGUSSerial() { DGUS_UCSRxB = 0; }
  784. // "Atomically" read the RX head index value without disabling interrupts:
  785. // This MUST be called with RX interrupts enabled, and CAN'T be called
  786. // from the RX ISR itself!
  787. FORCE_INLINE r_ring_buffer_pos_t DGUSSerial::atomic_read_rx_head() {
  788. #if RX_BUFFER_SIZE > 256
  789. // Keep reading until 2 consecutive reads return the same value,
  790. // meaning there was no update in-between caused by an interrupt.
  791. // This works because serial RX interrupts happen at a slower rate
  792. // than successive reads of a variable, so 2 consecutive reads with
  793. // the same value means no interrupt updated it.
  794. r_ring_buffer_pos_t vold, vnew = rx_buffer.head;
  795. sw_barrier();
  796. do {
  797. vold = vnew;
  798. vnew = rx_buffer.head;
  799. sw_barrier();
  800. } while (vold != vnew);
  801. return vnew;
  802. #else
  803. // With an 8bit index, reads are always atomic. No need for special handling
  804. return rx_buffer.head;
  805. #endif
  806. }
  807. // Set RX tail index, taking into account the RX ISR could interrupt
  808. // the write to this variable in the middle - So a backup strategy
  809. // is used to ensure reads of the correct values.
  810. // -Must NOT be called from the RX ISR -
  811. FORCE_INLINE void DGUSSerial::atomic_set_rx_tail(r_ring_buffer_pos_t value) {
  812. #if RX_BUFFER_SIZE > 256
  813. // Store the new value in the backup
  814. rx_tail_value_backup = value;
  815. sw_barrier();
  816. // Flag we are about to change the true value
  817. rx_tail_value_not_stable = true;
  818. sw_barrier();
  819. // Store the new value
  820. rx_buffer.tail = value;
  821. sw_barrier();
  822. // Signal the new value is completely stored into the value
  823. rx_tail_value_not_stable = false;
  824. sw_barrier();
  825. #else
  826. rx_buffer.tail = value;
  827. #endif
  828. }
  829. // Get the RX tail index, taking into account the read could be
  830. // interrupting in the middle of the update of that index value
  831. // -Called from the RX ISR -
  832. FORCE_INLINE r_ring_buffer_pos_t DGUSSerial::atomic_read_rx_tail() {
  833. #if RX_BUFFER_SIZE > 256
  834. // If the true index is being modified, return the backup value
  835. if (rx_tail_value_not_stable) return rx_tail_value_backup;
  836. #endif
  837. // The true index is stable, return it
  838. return rx_buffer.tail;
  839. }
  840. // (called with RX interrupts disabled)
  841. FORCE_INLINE void DGUSSerial::store_rxd_char() {
  842. // Get the tail - Nothing can alter its value while this ISR is executing, but there's
  843. // a chance that this ISR interrupted the main process while it was updating the index.
  844. // The backup mechanism ensures the correct value is always returned.
  845. const r_ring_buffer_pos_t t = atomic_read_rx_tail();
  846. // Get the head pointer - This ISR is the only one that modifies its value, so it's safe to read here
  847. r_ring_buffer_pos_t h = rx_buffer.head;
  848. // Get the next element
  849. r_ring_buffer_pos_t i = (r_ring_buffer_pos_t) (h + 1) & (r_ring_buffer_pos_t) (DGUS_RX_BUFFER_SIZE - 1);
  850. // Read the character from the USART
  851. uint8_t c = DGUS_UDRx;
  852. // If the character is to be stored at the index just before the tail
  853. // (such that the head would advance to the current tail), the RX FIFO is
  854. // full, so don't write the character or advance the head.
  855. if (i != t) {
  856. rx_buffer.buffer[h] = c;
  857. h = i;
  858. }
  859. else
  860. dgus_rx_overrun = true;
  861. // Store the new head value - The main loop will retry until the value is stable
  862. rx_buffer.head = h;
  863. }
  864. // (called with TX irqs disabled)
  865. FORCE_INLINE void DGUSSerial::tx_udr_empty_irq() {
  866. // Read positions
  867. uint8_t t = tx_buffer.tail;
  868. const uint8_t h = tx_buffer.head;
  869. // If nothing to transmit, just disable TX interrupts. This could
  870. // happen as the result of the non atomicity of the disabling of RX
  871. // interrupts that could end reenabling TX interrupts as a side effect.
  872. if (h == t) {
  873. CBI(DGUS_UCSRxB, UDRIEx); // (Non-atomic, could be reenabled by the main program, but eventually this will succeed)
  874. return;
  875. }
  876. // There is something to TX, Send the next byte
  877. const uint8_t c = tx_buffer.buffer[t];
  878. t = (t + 1) & (DGUS_TX_BUFFER_SIZE - 1);
  879. DGUS_UDRx = c;
  880. tx_buffer.tail = t;
  881. // Clear the TXC bit (by writing a one to its bit location).
  882. // Ensures flush() won't return until the bytes are actually written/
  883. SBI(DGUS_UCSRxA, TXCx);
  884. // Disable interrupts if there is nothing to transmit following this byte
  885. if (h == t) CBI(DGUS_UCSRxB, UDRIEx);
  886. }
  887. r_ring_buffer_pos_t DGUSSerial::available() {
  888. const r_ring_buffer_pos_t h = atomic_read_rx_head(), t = rx_buffer.tail;
  889. return (r_ring_buffer_pos_t) (DGUS_RX_BUFFER_SIZE + h - t) & (DGUS_RX_BUFFER_SIZE - 1);
  890. }
  891. int DGUSSerial::read() {
  892. const r_ring_buffer_pos_t h = atomic_read_rx_head();
  893. // Read the tail. Main thread owns it, so it is safe to directly read it
  894. r_ring_buffer_pos_t t = rx_buffer.tail;
  895. // If nothing to read, return now
  896. if (h == t) return -1;
  897. // Get the next char
  898. const int v = rx_buffer.buffer[t];
  899. t = (r_ring_buffer_pos_t) (t + 1) & (DGUS_RX_BUFFER_SIZE - 1);
  900. // Advance tail - Making sure the RX ISR will always get an stable value, even
  901. // if it interrupts the writing of the value of that variable in the middle.
  902. atomic_set_rx_tail(t);
  903. return v;
  904. }
  905. void DGUSSerial::write(const uint8_t c) {
  906. // are we currently tranmitting? If not, we can just place the byte in UDR.
  907. if (!TEST(DGUS_UCSRxB, UDRIEx) && TEST(DGUS_UCSRxA, UDREx)) {
  908. DGUS_UDRx = c;
  909. SBI(DGUS_UCSRxA, TXCx);
  910. return;
  911. }
  912. const uint8_t i = (tx_buffer.head + 1) & (DGUS_TX_BUFFER_SIZE - 1);
  913. while (i == tx_buffer.tail) sw_barrier();
  914. // Store new char. head is always safe to move
  915. tx_buffer.buffer[tx_buffer.head] = c;
  916. tx_buffer.head = i;
  917. SBI(DGUS_UCSRxB, UDRIEx); // Enable Interrupts to finish off.
  918. }
  919. t_ring_buffer_pos_t DGUSSerial::GetTxBufferFree() {
  920. const t_ring_buffer_pos_t t = tx_buffer.tail, // next byte to send.
  921. h = tx_buffer.head; // next pos for queue.
  922. int ret = t - h - 1;
  923. if (ret < 0) ret += DGUS_TX_BUFFER_SIZE + 1;
  924. return ret;
  925. }
  926. ISR(DGUS_SERIAL_UDRE_VECT) { dgusserial.tx_udr_empty_irq(); }
  927. ISR(DGUS_SERIAL_RX_VECT) { dgusserial.store_rxd_char(); }
  928. #endif // DGUS_LCD