util.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #include "Configuration.h"
  2. #include "ultralcd.h"
  3. #include "menu.h"
  4. #include "sound.h"
  5. #include "language.h"
  6. #include "util.h"
  7. #include <avr/pgmspace.h>
  8. #include "Prusa_farm.h"
  9. // Allocate the version string in the program memory. Otherwise the string lands either on the stack or in the global RAM.
  10. static const char FW_VERSION_STR[] PROGMEM = FW_VERSION;
  11. const uint16_t FW_VERSION_NR[4] PROGMEM = {
  12. FW_MAJOR,
  13. FW_MINOR,
  14. FW_REVISION,
  15. #ifndef FW_FLAVOR
  16. FW_COMMIT_NR
  17. #else
  18. # if FW_DEV_VERSION == FW_VERSION_ALPHA
  19. FIRMWARE_REVISION_ALPHA + FW_FLAVERSION
  20. # elif FW_DEV_VERSION == FW_VERSION_BETA
  21. FIRMWARE_REVISION_BETA + FW_FLAVERSION
  22. # elif FW_DEV_VERSION == FW_VERSION_RC
  23. FIRMWARE_REVISION_RC + FW_FLAVERSION
  24. # elif FW_DEV_VERSION == FW_VERSION_GOLD
  25. 0
  26. # endif
  27. #endif
  28. };
  29. const char* FW_VERSION_STR_P()
  30. {
  31. return FW_VERSION_STR;
  32. }
  33. const char FW_PRUSA3D_MAGIC_STR[] PROGMEM = FW_PRUSA3D_MAGIC;
  34. const char* FW_PRUSA3D_MAGIC_STR_P()
  35. {
  36. return FW_PRUSA3D_MAGIC_STR;
  37. }
  38. const char STR_REVISION_DEV [] PROGMEM = "DEV";
  39. const char STR_REVISION_ALPHA[] PROGMEM = "ALPHA";
  40. const char STR_REVISION_BETA [] PROGMEM = "BETA";
  41. const char STR_REVISION_RC [] PROGMEM = "RC";
  42. inline bool is_whitespace_or_nl(char c)
  43. {
  44. return c == ' ' || c == '\t' || c == '\n' || c == '\r';
  45. }
  46. inline bool is_whitespace_or_nl_or_eol(char c)
  47. {
  48. return c == 0 || c == ' ' || c == '\t' || c == '\n' || c == '\r';
  49. }
  50. inline bool is_digit(char c)
  51. {
  52. return c >= '0' && c <= '9';
  53. }
  54. char const * __attribute__((noinline)) Number(char const *str, uint16_t *v){
  55. *v = 0;
  56. while(is_digit(*str)){
  57. *v *= 10;
  58. *v += *str - '0';
  59. ++str;
  60. }
  61. return str;
  62. }
  63. bool __attribute__((noinline)) Tag(const char *str, const char *tag_P, uint8_t tagSize, uint16_t tagMask, uint16_t *v){
  64. if( ! strncmp_P(str, tag_P, tagSize) ){
  65. Number(str + tagSize, v);
  66. *v |= tagMask;
  67. return true;
  68. }
  69. return false;
  70. }
  71. // Parse a major.minor.revision version number.
  72. // Return true if valid.
  73. bool parse_version(const char *str, uint16_t version[4]) {
  74. for(uint8_t i = 0; i < 2; ++i){
  75. str = Number(str, version + i);
  76. if (*str != '.')
  77. return false;
  78. ++str;
  79. }
  80. str = Number(str, version + 2);
  81. version[3] = FIRMWARE_REVISION_RELEASED;
  82. // skip everything else until eol or '-'
  83. for(;;){
  84. if(is_whitespace_or_nl_or_eol(*str)){
  85. // speculatively reached the end of line, silently ignoring anything which is not a '-'
  86. return true;
  87. }
  88. if( *str == '-'){
  89. break; // tag expected
  90. }
  91. ++str;
  92. }
  93. // SERIAL_ECHOPGM("parse_version: ");
  94. // SERIAL_ECHO(version[0]);
  95. // SERIAL_ECHO('.');
  96. // SERIAL_ECHO(version[1]);
  97. // SERIAL_ECHO('.');
  98. // SERIAL_ECHO(version[2]);
  99. // SERIAL_ECHO('.');
  100. // SERIAL_ECHOLN(version[3]);
  101. if (*str++ == '-') {
  102. switch(*str){
  103. case 'A': // expect ALPHA
  104. static_assert(sizeof(STR_REVISION_ALPHA) == 6);
  105. return Tag( str, STR_REVISION_ALPHA, sizeof(STR_REVISION_ALPHA) - 1, FIRMWARE_REVISION_ALPHA, version + 3);
  106. case 'B': // expect BETA
  107. static_assert(sizeof(STR_REVISION_BETA) == 5);
  108. return Tag( str, STR_REVISION_BETA, sizeof(STR_REVISION_BETA) - 1, FIRMWARE_REVISION_BETA, version + 3);
  109. case 'D': // expect DEV
  110. static_assert(sizeof(STR_REVISION_DEV) == 4);
  111. return Tag( str, STR_REVISION_DEV, sizeof(STR_REVISION_DEV) - 1, FIRMWARE_REVISION_DEV, version + 3);
  112. case 'R': // expect RC
  113. static_assert(sizeof(STR_REVISION_RC) == 3);
  114. return Tag( str, STR_REVISION_RC, sizeof(STR_REVISION_RC) - 1, FIRMWARE_REVISION_RC, version + 3);
  115. default: return false; // fail everything else
  116. }
  117. }
  118. // SERIAL_ECHOPGM("parse_version with tag: ");
  119. // SERIAL_ECHO(version[0]);
  120. // SERIAL_ECHO('.');
  121. // SERIAL_ECHO(version[1]);
  122. // SERIAL_ECHO('.');
  123. // SERIAL_ECHO(version[2]);
  124. // SERIAL_ECHO('.');
  125. // SERIAL_ECHOLN(version[3]);
  126. return false;
  127. }
  128. inline bool strncmp_PP(const char *p1, const char *p2, uint8_t n)
  129. {
  130. for (; n > 0; -- n, ++ p1, ++ p2) {
  131. if (pgm_read_byte(p1) >= 65 && pgm_read_byte(p1) <= 92) //p1 is upper case (p2 is always lowercase)
  132. {
  133. if ((pgm_read_byte(p1)+32) < pgm_read_byte(p2))
  134. return -1;
  135. if ((pgm_read_byte(p1)+32) > pgm_read_byte(p2))
  136. return 1;
  137. }
  138. else if (pgm_read_byte(p1) == 0) {
  139. return 0;
  140. }
  141. else { //p1 is lowercase
  142. if (pgm_read_byte(p1) < pgm_read_byte(p2))
  143. return -1;
  144. if (pgm_read_byte(p1) > pgm_read_byte(p2))
  145. return 1;
  146. }
  147. }
  148. return 0;
  149. }
  150. // 1 - yes, 0 - false, -1 - error;
  151. inline int8_t is_provided_version_newer(const char *version_string)
  152. {
  153. uint16_t ver_gcode[4];
  154. if (! parse_version(version_string, ver_gcode))
  155. return -1;
  156. for (uint8_t i = 0; i < 4; ++ i)
  157. {
  158. uint16_t v = (uint16_t)pgm_read_word(&FW_VERSION_NR[i]);
  159. if (ver_gcode[i] > v)
  160. return 1;
  161. else if (ver_gcode[i] < v)
  162. return 0;
  163. }
  164. return 0;
  165. }
  166. bool eeprom_fw_version_older_than_p(const uint16_t (&ver_req)[4])
  167. {
  168. uint16_t ver_eeprom[4];
  169. ver_eeprom[0] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR);
  170. ver_eeprom[1] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR);
  171. ver_eeprom[2] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION);
  172. ver_eeprom[3] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR);
  173. for (uint8_t i = 0; i < 4; ++i) {
  174. uint16_t v = pgm_read_word(&ver_req[i]);
  175. if (v > ver_eeprom[i])
  176. return true;
  177. else if (v < ver_eeprom[i])
  178. break;
  179. }
  180. return false;
  181. }
  182. bool show_upgrade_dialog_if_version_newer(const char *version_string)
  183. {
  184. if(oCheckVersion == ClCheckVersion::_None)
  185. return false;
  186. int8_t upgrade = is_provided_version_newer(version_string);
  187. if (upgrade < 0)
  188. return false;
  189. if (upgrade) {
  190. lcd_display_message_fullscreen_P(_i("New firmware version available:"));////MSG_NEW_FIRMWARE_AVAILABLE c=20 r=2
  191. lcd_puts_at_P(0, 2, PSTR(""));
  192. for (const char *c = version_string; ! is_whitespace_or_nl_or_eol(*c); ++ c)
  193. lcd_putc(*c);
  194. lcd_puts_at_P(0, 3, _i("Please upgrade."));////MSG_NEW_FIRMWARE_PLEASE_UPGRADE c=20
  195. Sound_MakeCustom(50,1000,false);
  196. delay_keep_alive(500);
  197. Sound_MakeCustom(50,1000,false);
  198. lcd_wait_for_click_delay(30);
  199. lcd_update_enable(true);
  200. lcd_clear();
  201. lcd_update(0);
  202. }
  203. // Succeeded.
  204. return true;
  205. }
  206. void update_current_firmware_version_to_eeprom()
  207. {
  208. for (int8_t i = 0; i < FW_PRUSA3D_MAGIC_LEN; ++ i){
  209. eeprom_update_byte((uint8_t*)(EEPROM_FIRMWARE_PRUSA_MAGIC+i), pgm_read_byte(FW_PRUSA3D_MAGIC_STR+i));
  210. }
  211. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR, (uint16_t)pgm_read_word(&FW_VERSION_NR[0]));
  212. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR, (uint16_t)pgm_read_word(&FW_VERSION_NR[1]));
  213. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION, (uint16_t)pgm_read_word(&FW_VERSION_NR[2]));
  214. // See FirmwareRevisionFlavorType for the definition of firmware flavors.
  215. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR, (uint16_t)pgm_read_word(&FW_VERSION_NR[3]));
  216. }
  217. ClNozzleDiameter oNozzleDiameter=ClNozzleDiameter::_Diameter_400;
  218. ClCheckMode oCheckMode=ClCheckMode::_None;
  219. ClCheckModel oCheckModel=ClCheckModel::_None;
  220. ClCheckVersion oCheckVersion=ClCheckVersion::_None;
  221. ClCheckGcode oCheckGcode=ClCheckGcode::_None;
  222. void fCheckModeInit() {
  223. oCheckMode = (ClCheckMode)eeprom_read_byte((uint8_t *)EEPROM_CHECK_MODE);
  224. if (oCheckMode == ClCheckMode::_Undef) {
  225. oCheckMode = ClCheckMode::_Warn;
  226. eeprom_update_byte((uint8_t *)EEPROM_CHECK_MODE, (uint8_t)oCheckMode);
  227. }
  228. if (farm_mode) {
  229. oCheckMode = ClCheckMode::_Strict;
  230. eeprom_init_default_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT);
  231. }
  232. oNozzleDiameter = (ClNozzleDiameter)eeprom_read_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER);
  233. if ((oNozzleDiameter == ClNozzleDiameter::_Diameter_Undef) && !farm_mode) {
  234. oNozzleDiameter = ClNozzleDiameter::_Diameter_400;
  235. eeprom_update_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER, (uint8_t)oNozzleDiameter);
  236. eeprom_update_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT);
  237. }
  238. oCheckModel = (ClCheckModel)eeprom_read_byte((uint8_t *)EEPROM_CHECK_MODEL);
  239. if (oCheckModel == ClCheckModel::_Undef) {
  240. oCheckModel = ClCheckModel::_Warn;
  241. eeprom_update_byte((uint8_t *)EEPROM_CHECK_MODEL, (uint8_t)oCheckModel);
  242. }
  243. oCheckVersion = (ClCheckVersion)eeprom_read_byte((uint8_t *)EEPROM_CHECK_VERSION);
  244. if (oCheckVersion == ClCheckVersion::_Undef) {
  245. oCheckVersion = ClCheckVersion::_Warn;
  246. eeprom_update_byte((uint8_t *)EEPROM_CHECK_VERSION, (uint8_t)oCheckVersion);
  247. }
  248. oCheckGcode = (ClCheckGcode)eeprom_read_byte((uint8_t *)EEPROM_CHECK_GCODE);
  249. if (oCheckGcode == ClCheckGcode::_Undef) {
  250. oCheckGcode = ClCheckGcode::_Warn;
  251. eeprom_update_byte((uint8_t *)EEPROM_CHECK_GCODE, (uint8_t)oCheckGcode);
  252. }
  253. }
  254. static void render_M862_warnings(const char* warning, const char* strict, uint8_t check)
  255. {
  256. if (check == 1) { // Warning, stop print if user selects 'No'
  257. if (lcd_show_fullscreen_message_yes_no_and_wait_P(warning, true, LCD_LEFT_BUTTON_CHOICE) == LCD_MIDDLE_BUTTON_CHOICE) {
  258. lcd_print_stop();
  259. }
  260. } else if (check == 2) { // Strict, always stop print
  261. lcd_show_fullscreen_message_and_wait_P(strict);
  262. lcd_print_stop();
  263. }
  264. }
  265. void nozzle_diameter_check(uint16_t nDiameter) {
  266. uint16_t nDiameter_um;
  267. if (oCheckMode == ClCheckMode::_None)
  268. return;
  269. nDiameter_um = eeprom_read_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM);
  270. if (nDiameter == nDiameter_um)
  271. return;
  272. // SERIAL_ECHO_START;
  273. // SERIAL_ECHOLNPGM("Printer nozzle diameter differs from the G-code ...");
  274. // SERIAL_ECHOPGM("actual : ");
  275. // SERIAL_ECHOLN((float)(nDiameter_um/1000.0));
  276. // SERIAL_ECHOPGM("expected: ");
  277. // SERIAL_ECHOLN((float)(nDiameter/1000.0));
  278. render_M862_warnings(
  279. _i("Printer nozzle diameter differs from the G-code. Continue?") ////MSG_NOZZLE_DIFFERS_CONTINUE c=20 r=5
  280. ,_i("Printer nozzle diameter differs from the G-code. Please check the value in settings. Print cancelled.") ////MSG_NOZZLE_DIFFERS_CANCELLED c=20 r=9
  281. ,(uint8_t)oCheckMode
  282. );
  283. if (!farm_mode) {
  284. bSettings = false; // flag ('fake parameter') for 'lcd_hw_setup_menu()' function
  285. menu_submenu(lcd_hw_setup_menu);
  286. }
  287. }
  288. void printer_model_check(uint16_t nPrinterModel, uint16_t actualPrinterModel) {
  289. if (oCheckModel == ClCheckModel::_None)
  290. return;
  291. if (nPrinterModel == actualPrinterModel)
  292. return;
  293. // SERIAL_ECHO_START;
  294. // SERIAL_ECHOLNPGM("Printer model differs from the G-code ...");
  295. // SERIAL_ECHOPGM("actual : ");
  296. // SERIAL_ECHOLN(actualPrinterModel);
  297. // SERIAL_ECHOPGM("expected: ");
  298. // SERIAL_ECHOLN(nPrinterModel);
  299. render_M862_warnings(
  300. _T(MSG_GCODE_DIFF_PRINTER_CONTINUE)
  301. ,_T(MSG_GCODE_DIFF_PRINTER_CANCELLED)
  302. ,(uint8_t)oCheckModel
  303. );
  304. }
  305. uint8_t mCompareValue(uint16_t nX, uint16_t nY) {
  306. if (nX > nY)
  307. return ((uint8_t)ClCompareValue::_Greater);
  308. if (nX < nY)
  309. return ((uint8_t)ClCompareValue::_Less);
  310. return ((uint8_t)ClCompareValue::_Equal);
  311. }
  312. void fw_version_check(const char *pVersion) {
  313. if (oCheckVersion == ClCheckVersion::_None)
  314. return;
  315. uint16_t aVersion[4];
  316. uint8_t nCompareValueResult;
  317. parse_version(pVersion, aVersion);
  318. nCompareValueResult = mCompareValue(aVersion[0], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MAJOR)) << 6;
  319. nCompareValueResult += mCompareValue(aVersion[1], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MINOR)) << 4;
  320. nCompareValueResult += mCompareValue(aVersion[2], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_REVISION)) << 2;
  321. nCompareValueResult += mCompareValue(aVersion[3], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_FLAVOR));
  322. if (nCompareValueResult <= COMPARE_VALUE_EQUAL)
  323. return;
  324. /*
  325. SERIAL_ECHO_START;
  326. SERIAL_ECHOLNPGM("Printer FW version differs from the G-code ...");
  327. SERIAL_ECHOPGM("actual : ");
  328. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MAJOR));
  329. SERIAL_ECHO('.');
  330. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MINOR));
  331. SERIAL_ECHO('.');
  332. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_REVISION));
  333. SERIAL_ECHO('.');
  334. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_FLAVOR));
  335. SERIAL_ECHOPGM("\nexpected: ");
  336. SERIAL_ECHO(aVersion[0]);
  337. SERIAL_ECHO('.');
  338. SERIAL_ECHO(aVersion[1]);
  339. SERIAL_ECHO('.');
  340. SERIAL_ECHO(aVersion[2]);
  341. SERIAL_ECHO('.');
  342. SERIAL_ECHOLN(aVersion[3]);
  343. */
  344. render_M862_warnings(
  345. _i("G-code sliced for a newer firmware. Continue?") ////MSG_GCODE_NEWER_FIRMWARE_CONTINUE c=20 r=5
  346. ,_i("G-code sliced for a newer firmware. Please update the firmware. Print cancelled.") ////MSG_GCODE_NEWER_FIRMWARE_CANCELLED c=20 r=8
  347. ,(uint8_t)oCheckVersion
  348. );
  349. }
  350. void gcode_level_check(uint16_t nGcodeLevel) {
  351. if (oCheckGcode == ClCheckGcode::_None)
  352. return;
  353. if (nGcodeLevel <= (uint16_t)GCODE_LEVEL)
  354. return;
  355. // SERIAL_ECHO_START;
  356. // SERIAL_ECHOLNPGM("Printer G-code level differs from the G-code ...");
  357. // SERIAL_ECHOPGM("actual : ");
  358. // SERIAL_ECHOLN(GCODE_LEVEL);
  359. // SERIAL_ECHOPGM("expected: ");
  360. // SERIAL_ECHOLN(nGcodeLevel);
  361. render_M862_warnings(
  362. _i("G-code sliced for a different level. Continue?") ////MSG_GCODE_DIFF_CONTINUE c=20 r=4
  363. ,_i("G-code sliced for a different level. Please re-slice the model again. Print cancelled.") ////MSG_GCODE_DIFF_CANCELLED c=20 r=7
  364. ,(uint8_t)oCheckGcode
  365. );
  366. }
  367. #define GCODE_DELIMITER '"'
  368. char *code_string(const char *pStr, size_t *nLength) {
  369. char* pStrBegin;
  370. char* pStrEnd;
  371. pStrBegin=strchr(pStr,GCODE_DELIMITER);
  372. if(!pStrBegin)
  373. return(NULL);
  374. pStrBegin++;
  375. pStrEnd=strchr(pStrBegin,GCODE_DELIMITER);
  376. if(!pStrEnd)
  377. return(NULL);
  378. *nLength=pStrEnd-pStrBegin;
  379. return pStrBegin;
  380. }
  381. void printer_smodel_check(const char *pStrPos, const char *actualPrinterSModel) {
  382. char* pResult;
  383. size_t nLength,nPrinterNameLength;
  384. nPrinterNameLength = strlen_P(actualPrinterSModel);
  385. pResult=code_string(pStrPos,&nLength);
  386. if(pResult != NULL && nLength == nPrinterNameLength) {
  387. // Only compare them if the lengths match
  388. if (strncmp_P(pResult, actualPrinterSModel, nLength) == 0) return;
  389. }
  390. render_M862_warnings(
  391. _T(MSG_GCODE_DIFF_PRINTER_CONTINUE)
  392. ,_T(MSG_GCODE_DIFF_PRINTER_CANCELLED)
  393. ,(uint8_t)oCheckModel
  394. );
  395. }
  396. uint16_t nPrinterType(bool bMMu) {
  397. if (bMMu) {
  398. return pgm_read_word(&_nPrinterMmuType);
  399. }
  400. else {
  401. return pgm_read_word(&_nPrinterType);
  402. }
  403. }
  404. const char *sPrinterType(bool bMMu) {
  405. if (bMMu) {
  406. return _sPrinterMmuName;
  407. }
  408. else {
  409. return _sPrinterName;
  410. }
  411. }
  412. void ip4_to_str(char* dest, uint8_t* IP)
  413. {
  414. sprintf_P(dest, PSTR("%u.%u.%u.%u"), IP[0], IP[1], IP[2], IP[3]);
  415. }
  416. bool calibration_status_get(CalibrationStatus components)
  417. {
  418. CalibrationStatus status = eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_V2);
  419. return ((status & components) == components);
  420. }
  421. void calibration_status_set(CalibrationStatus components)
  422. {
  423. CalibrationStatus status = eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_V2);
  424. status |= components;
  425. eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_V2, status);
  426. }
  427. void calibration_status_clear(CalibrationStatus components)
  428. {
  429. CalibrationStatus status = eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_V2);
  430. status &= ~components;
  431. eeprom_update_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_V2, status);
  432. }