util.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. static 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 force_selftest_if_fw_version()
  167. {
  168. //if fw version used before flashing new firmware (fw version currently stored in eeprom) is lower then 3.1.2-RC2, function returns true to force selftest
  169. uint16_t ver_eeprom[4];
  170. uint16_t ver_with_calibration[4] = {3, 1, 2, 4}; //hardcoded 3.1.2-RC2 version
  171. bool force_selftest = false;
  172. ver_eeprom[0] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR);
  173. ver_eeprom[1] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR);
  174. ver_eeprom[2] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION);
  175. ver_eeprom[3] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR);
  176. for (uint8_t i = 0; i < 4; ++i) {
  177. if (ver_with_calibration[i] > ver_eeprom[i]) {
  178. force_selftest = true;
  179. break;
  180. }
  181. else if (ver_with_calibration[i] < ver_eeprom[i])
  182. break;
  183. }
  184. //force selftest also in case that version used before flashing new firmware was 3.2.0-RC1
  185. if ((ver_eeprom[0] == 3) && (ver_eeprom[1] == 2) && (ver_eeprom[2] == 0) && (ver_eeprom[3] == 3)) force_selftest = true;
  186. return force_selftest;
  187. }
  188. bool show_upgrade_dialog_if_version_newer(const char *version_string)
  189. {
  190. if(oCheckVersion == ClCheckVersion::_None)
  191. return false;
  192. int8_t upgrade = is_provided_version_newer(version_string);
  193. if (upgrade < 0)
  194. return false;
  195. if (upgrade) {
  196. lcd_display_message_fullscreen_P(_i("New firmware version available:"));////MSG_NEW_FIRMWARE_AVAILABLE c=20 r=2
  197. lcd_puts_at_P(0, 2, PSTR(""));
  198. for (const char *c = version_string; ! is_whitespace_or_nl_or_eol(*c); ++ c)
  199. lcd_putc(*c);
  200. lcd_puts_at_P(0, 3, _i("Please upgrade."));////MSG_NEW_FIRMWARE_PLEASE_UPGRADE c=20
  201. Sound_MakeCustom(50,1000,false);
  202. delay_keep_alive(500);
  203. Sound_MakeCustom(50,1000,false);
  204. lcd_wait_for_click_delay(30);
  205. lcd_update_enable(true);
  206. lcd_clear();
  207. lcd_update(0);
  208. }
  209. // Succeeded.
  210. return true;
  211. }
  212. void update_current_firmware_version_to_eeprom()
  213. {
  214. for (int8_t i = 0; i < FW_PRUSA3D_MAGIC_LEN; ++ i){
  215. eeprom_update_byte((uint8_t*)(EEPROM_FIRMWARE_PRUSA_MAGIC+i), pgm_read_byte(FW_PRUSA3D_MAGIC_STR+i));
  216. }
  217. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR, (uint16_t)pgm_read_word(&FW_VERSION_NR[0]));
  218. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR, (uint16_t)pgm_read_word(&FW_VERSION_NR[1]));
  219. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION, (uint16_t)pgm_read_word(&FW_VERSION_NR[2]));
  220. // See FirmwareRevisionFlavorType for the definition of firmware flavors.
  221. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR, (uint16_t)pgm_read_word(&FW_VERSION_NR[3]));
  222. }
  223. ClNozzleDiameter oNozzleDiameter=ClNozzleDiameter::_Diameter_400;
  224. ClCheckMode oCheckMode=ClCheckMode::_None;
  225. ClCheckModel oCheckModel=ClCheckModel::_None;
  226. ClCheckVersion oCheckVersion=ClCheckVersion::_None;
  227. ClCheckGcode oCheckGcode=ClCheckGcode::_None;
  228. void fCheckModeInit() {
  229. oCheckMode = (ClCheckMode)eeprom_read_byte((uint8_t *)EEPROM_CHECK_MODE);
  230. if (oCheckMode == ClCheckMode::_Undef) {
  231. oCheckMode = ClCheckMode::_Warn;
  232. eeprom_update_byte((uint8_t *)EEPROM_CHECK_MODE, (uint8_t)oCheckMode);
  233. }
  234. if (farm_mode) {
  235. oCheckMode = ClCheckMode::_Strict;
  236. eeprom_init_default_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT);
  237. }
  238. oNozzleDiameter = (ClNozzleDiameter)eeprom_read_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER);
  239. if ((oNozzleDiameter == ClNozzleDiameter::_Diameter_Undef) && !farm_mode) {
  240. oNozzleDiameter = ClNozzleDiameter::_Diameter_400;
  241. eeprom_update_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER, (uint8_t)oNozzleDiameter);
  242. eeprom_update_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT);
  243. }
  244. oCheckModel = (ClCheckModel)eeprom_read_byte((uint8_t *)EEPROM_CHECK_MODEL);
  245. if (oCheckModel == ClCheckModel::_Undef) {
  246. oCheckModel = ClCheckModel::_Warn;
  247. eeprom_update_byte((uint8_t *)EEPROM_CHECK_MODEL, (uint8_t)oCheckModel);
  248. }
  249. oCheckVersion = (ClCheckVersion)eeprom_read_byte((uint8_t *)EEPROM_CHECK_VERSION);
  250. if (oCheckVersion == ClCheckVersion::_Undef) {
  251. oCheckVersion = ClCheckVersion::_Warn;
  252. eeprom_update_byte((uint8_t *)EEPROM_CHECK_VERSION, (uint8_t)oCheckVersion);
  253. }
  254. oCheckGcode = (ClCheckGcode)eeprom_read_byte((uint8_t *)EEPROM_CHECK_GCODE);
  255. if (oCheckGcode == ClCheckGcode::_Undef) {
  256. oCheckGcode = ClCheckGcode::_Warn;
  257. eeprom_update_byte((uint8_t *)EEPROM_CHECK_GCODE, (uint8_t)oCheckGcode);
  258. }
  259. }
  260. static void render_M862_warnings(const char* warning, const char* strict, uint8_t check)
  261. {
  262. if (check == 1) { // Warning, stop print if user selects 'No'
  263. if (lcd_show_fullscreen_message_yes_no_and_wait_P(warning, true, LCD_LEFT_BUTTON_CHOICE) == LCD_MIDDLE_BUTTON_CHOICE) {
  264. lcd_print_stop();
  265. }
  266. } else if (check == 2) { // Strict, always stop print
  267. lcd_show_fullscreen_message_and_wait_P(strict);
  268. lcd_print_stop();
  269. }
  270. }
  271. void nozzle_diameter_check(uint16_t nDiameter) {
  272. uint16_t nDiameter_um;
  273. if (oCheckMode == ClCheckMode::_None)
  274. return;
  275. nDiameter_um = eeprom_read_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM);
  276. if (nDiameter == nDiameter_um)
  277. return;
  278. // SERIAL_ECHO_START;
  279. // SERIAL_ECHOLNPGM("Printer nozzle diameter differs from the G-code ...");
  280. // SERIAL_ECHOPGM("actual : ");
  281. // SERIAL_ECHOLN((float)(nDiameter_um/1000.0));
  282. // SERIAL_ECHOPGM("expected: ");
  283. // SERIAL_ECHOLN((float)(nDiameter/1000.0));
  284. render_M862_warnings(
  285. _i("Printer nozzle diameter differs from the G-code. Continue?") ////MSG_NOZZLE_DIFFERS_CONTINUE c=20 r=5
  286. ,_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
  287. ,(uint8_t)oCheckMode
  288. );
  289. if (!farm_mode) {
  290. bSettings = false; // flag ('fake parameter') for 'lcd_hw_setup_menu()' function
  291. menu_submenu(lcd_hw_setup_menu);
  292. }
  293. }
  294. void printer_model_check(uint16_t nPrinterModel, uint16_t actualPrinterModel) {
  295. if (oCheckModel == ClCheckModel::_None)
  296. return;
  297. if (nPrinterModel == actualPrinterModel)
  298. return;
  299. // SERIAL_ECHO_START;
  300. // SERIAL_ECHOLNPGM("Printer model differs from the G-code ...");
  301. // SERIAL_ECHOPGM("actual : ");
  302. // SERIAL_ECHOLN(actualPrinterModel);
  303. // SERIAL_ECHOPGM("expected: ");
  304. // SERIAL_ECHOLN(nPrinterModel);
  305. render_M862_warnings(
  306. _T(MSG_GCODE_DIFF_PRINTER_CONTINUE)
  307. ,_T(MSG_GCODE_DIFF_PRINTER_CANCELLED)
  308. ,(uint8_t)oCheckModel
  309. );
  310. }
  311. uint8_t mCompareValue(uint16_t nX, uint16_t nY) {
  312. if (nX > nY)
  313. return ((uint8_t)ClCompareValue::_Greater);
  314. if (nX < nY)
  315. return ((uint8_t)ClCompareValue::_Less);
  316. return ((uint8_t)ClCompareValue::_Equal);
  317. }
  318. void fw_version_check(const char *pVersion) {
  319. if (oCheckVersion == ClCheckVersion::_None)
  320. return;
  321. uint16_t aVersion[4];
  322. uint8_t nCompareValueResult;
  323. parse_version(pVersion, aVersion);
  324. nCompareValueResult = mCompareValue(aVersion[0], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MAJOR)) << 6;
  325. nCompareValueResult += mCompareValue(aVersion[1], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MINOR)) << 4;
  326. nCompareValueResult += mCompareValue(aVersion[2], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_REVISION)) << 2;
  327. nCompareValueResult += mCompareValue(aVersion[3], eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_FLAVOR));
  328. if (nCompareValueResult <= COMPARE_VALUE_EQUAL)
  329. return;
  330. /*
  331. SERIAL_ECHO_START;
  332. SERIAL_ECHOLNPGM("Printer FW version differs from the G-code ...");
  333. SERIAL_ECHOPGM("actual : ");
  334. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MAJOR));
  335. SERIAL_ECHO('.');
  336. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_MINOR));
  337. SERIAL_ECHO('.');
  338. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_REVISION));
  339. SERIAL_ECHO('.');
  340. SERIAL_ECHO(eeprom_read_word((uint16_t *)EEPROM_FIRMWARE_VERSION_FLAVOR));
  341. SERIAL_ECHOPGM("\nexpected: ");
  342. SERIAL_ECHO(aVersion[0]);
  343. SERIAL_ECHO('.');
  344. SERIAL_ECHO(aVersion[1]);
  345. SERIAL_ECHO('.');
  346. SERIAL_ECHO(aVersion[2]);
  347. SERIAL_ECHO('.');
  348. SERIAL_ECHOLN(aVersion[3]);
  349. */
  350. render_M862_warnings(
  351. _i("G-code sliced for a newer firmware. Continue?") ////MSG_GCODE_NEWER_FIRMWARE_CONTINUE c=20 r=5
  352. ,_i("G-code sliced for a newer firmware. Please update the firmware. Print cancelled.") ////MSG_GCODE_NEWER_FIRMWARE_CANCELLED c=20 r=8
  353. ,(uint8_t)oCheckVersion
  354. );
  355. }
  356. void gcode_level_check(uint16_t nGcodeLevel) {
  357. if (oCheckGcode == ClCheckGcode::_None)
  358. return;
  359. if (nGcodeLevel <= (uint16_t)GCODE_LEVEL)
  360. return;
  361. // SERIAL_ECHO_START;
  362. // SERIAL_ECHOLNPGM("Printer G-code level differs from the G-code ...");
  363. // SERIAL_ECHOPGM("actual : ");
  364. // SERIAL_ECHOLN(GCODE_LEVEL);
  365. // SERIAL_ECHOPGM("expected: ");
  366. // SERIAL_ECHOLN(nGcodeLevel);
  367. render_M862_warnings(
  368. _i("G-code sliced for a different level. Continue?") ////MSG_GCODE_DIFF_CONTINUE c=20 r=4
  369. ,_i("G-code sliced for a different level. Please re-slice the model again. Print cancelled.") ////MSG_GCODE_DIFF_CANCELLED c=20 r=7
  370. ,(uint8_t)oCheckGcode
  371. );
  372. }
  373. #define GCODE_DELIMITER '"'
  374. char *code_string(const char *pStr, size_t *nLength) {
  375. char* pStrBegin;
  376. char* pStrEnd;
  377. pStrBegin=strchr(pStr,GCODE_DELIMITER);
  378. if(!pStrBegin)
  379. return(NULL);
  380. pStrBegin++;
  381. pStrEnd=strchr(pStrBegin,GCODE_DELIMITER);
  382. if(!pStrEnd)
  383. return(NULL);
  384. *nLength=pStrEnd-pStrBegin;
  385. return pStrBegin;
  386. }
  387. void printer_smodel_check(const char *pStrPos, const char *actualPrinterSModel) {
  388. char* pResult;
  389. size_t nLength,nPrinterNameLength;
  390. nPrinterNameLength = strlen_P(actualPrinterSModel);
  391. pResult=code_string(pStrPos,&nLength);
  392. if(pResult != NULL && nLength == nPrinterNameLength) {
  393. // Only compare them if the lengths match
  394. if (strncmp_P(pResult, actualPrinterSModel, nLength) == 0) return;
  395. }
  396. render_M862_warnings(
  397. _T(MSG_GCODE_DIFF_PRINTER_CONTINUE)
  398. ,_T(MSG_GCODE_DIFF_PRINTER_CANCELLED)
  399. ,(uint8_t)oCheckModel
  400. );
  401. }
  402. uint16_t nPrinterType(bool bMMu) {
  403. if (bMMu) {
  404. return pgm_read_word(&_nPrinterMmuType);
  405. }
  406. else {
  407. return pgm_read_word(&_nPrinterType);
  408. }
  409. }
  410. const char *sPrinterType(bool bMMu) {
  411. if (bMMu) {
  412. return _sPrinterMmuName;
  413. }
  414. else {
  415. return _sPrinterName;
  416. }
  417. }
  418. void ip4_to_str(char* dest, uint8_t* IP)
  419. {
  420. sprintf_P(dest, PSTR("%u.%u.%u.%u"), IP[0], IP[1], IP[2], IP[3]);
  421. }