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.

EEPROMwrite.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #ifndef __EEPROMH
  2. #define __EEPROMH
  3. #include "Marlin.h"
  4. #include "planner.h"
  5. #include "temperature.h"
  6. #include <EEPROM.h>
  7. template <class T> int EEPROM_writeAnything(int &ee, const T& value)
  8. {
  9. const byte* p = (const byte*)(const void*)&value;
  10. int i;
  11. for (i = 0; i < (int)sizeof(value); i++)
  12. EEPROM.write(ee++, *p++);
  13. return i;
  14. }
  15. template <class T> int EEPROM_readAnything(int &ee, T& value)
  16. {
  17. byte* p = (byte*)(void*)&value;
  18. int i;
  19. for (i = 0; i < (int)sizeof(value); i++)
  20. *p++ = EEPROM.read(ee++);
  21. return i;
  22. }
  23. //======================================================================================
  24. #include <avr/pgmspace.h>
  25. void serialprintPGM(const char *str)
  26. {
  27. char ch=pgm_read_byte(str);
  28. while(ch)
  29. {
  30. Serial.print(ch);
  31. ch=pgm_read_byte(++str);
  32. }
  33. }
  34. #define SerialprintPGM(x) serialprintPGM(PSTR(x))
  35. #define EEPROM_OFFSET 100
  36. // IMPORTANT: Whenever there are changes made to the variables stored in EEPROM
  37. // in the functions below, also increment the version number. This makes sure that
  38. // the default values are used whenever there is a change to the data, to prevent
  39. // wrong data being written to the variables.
  40. // ALSO: always make sure the variables in the Store and retrieve sections are in the same order.
  41. #define EEPROM_VERSION "V04"
  42. void StoreSettings()
  43. {
  44. char ver[4]= "000";
  45. int i=EEPROM_OFFSET;
  46. EEPROM_writeAnything(i,ver); // invalidate data first
  47. EEPROM_writeAnything(i,axis_steps_per_unit);
  48. EEPROM_writeAnything(i,max_feedrate);
  49. EEPROM_writeAnything(i,max_acceleration_units_per_sq_second);
  50. EEPROM_writeAnything(i,acceleration);
  51. EEPROM_writeAnything(i,retract_acceleration);
  52. EEPROM_writeAnything(i,minimumfeedrate);
  53. EEPROM_writeAnything(i,mintravelfeedrate);
  54. EEPROM_writeAnything(i,minsegmenttime);
  55. EEPROM_writeAnything(i,max_xy_jerk);
  56. EEPROM_writeAnything(i,max_z_jerk);
  57. #ifdef PIDTEMP
  58. EEPROM_writeAnything(i,Kp);
  59. EEPROM_writeAnything(i,Ki);
  60. EEPROM_writeAnything(i,Kd);
  61. #else
  62. EEPROM_writeAnything(i,3000);
  63. EEPROM_writeAnything(i,0);
  64. EEPROM_writeAnything(i,0);
  65. #endif
  66. char ver2[4]=EEPROM_VERSION;
  67. i=EEPROM_OFFSET;
  68. EEPROM_writeAnything(i,ver2); // validate data
  69. SerialprintPGM("echo: Settings Stored\n");
  70. }
  71. void RetrieveSettings(bool def=false)
  72. { // if def=true, the default values will be used
  73. int i=EEPROM_OFFSET;
  74. char stored_ver[4];
  75. char ver[4]=EEPROM_VERSION;
  76. EEPROM_readAnything(i,stored_ver); //read stored version
  77. // SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << stored_ver << "]");
  78. if ((!def)&&(strncmp(ver,stored_ver,3)==0))
  79. { // version number match
  80. EEPROM_readAnything(i,axis_steps_per_unit);
  81. EEPROM_readAnything(i,max_feedrate);
  82. EEPROM_readAnything(i,max_acceleration_units_per_sq_second);
  83. EEPROM_readAnything(i,acceleration);
  84. EEPROM_readAnything(i,retract_acceleration);
  85. EEPROM_readAnything(i,minimumfeedrate);
  86. EEPROM_readAnything(i,mintravelfeedrate);
  87. EEPROM_readAnything(i,minsegmenttime);
  88. EEPROM_readAnything(i,max_xy_jerk);
  89. EEPROM_readAnything(i,max_z_jerk);
  90. #ifndef PIDTEMP
  91. float Kp,Ki,Kd;
  92. #endif
  93. EEPROM_readAnything(i,Kp);
  94. EEPROM_readAnything(i,Ki);
  95. EEPROM_readAnything(i,Kd);
  96. SerialprintPGM("echo: Stored settings retreived:\n");
  97. }
  98. else
  99. {
  100. float tmp1[]=DEFAULT_AXIS_STEPS_PER_UNIT;
  101. float tmp2[]=DEFAULT_MAX_FEEDRATE;
  102. long tmp3[]=DEFAULT_MAX_ACCELERATION;
  103. for (short i=0;i<4;i++)
  104. {
  105. axis_steps_per_unit[i]=tmp1[i];
  106. max_feedrate[i]=tmp2[i];
  107. max_acceleration_units_per_sq_second[i]=tmp3[i];
  108. }
  109. acceleration=DEFAULT_ACCELERATION;
  110. retract_acceleration=DEFAULT_RETRACT_ACCELERATION;
  111. minimumfeedrate=DEFAULT_MINIMUMFEEDRATE;
  112. minsegmenttime=DEFAULT_MINSEGMENTTIME;
  113. mintravelfeedrate=DEFAULT_MINTRAVELFEEDRATE;
  114. max_xy_jerk=DEFAULT_XYJERK;
  115. max_z_jerk=DEFAULT_ZJERK;
  116. SerialprintPGM("echo: Using Default settings:\n");
  117. }
  118. SerialprintPGM("echo: Steps per unit:\n M92 X");
  119. Serial.print(axis_steps_per_unit[0]);
  120. SerialprintPGM(" Y");
  121. Serial.print(axis_steps_per_unit[1]);
  122. SerialprintPGM(" Z");
  123. Serial.print(axis_steps_per_unit[2]);
  124. SerialprintPGM(" E");
  125. Serial.print(axis_steps_per_unit[3]);
  126. SerialprintPGM("\nMaximum feedrates (mm/s):\n M203 X" );
  127. Serial.print(max_feedrate[0]/60);
  128. SerialprintPGM(" Y" );
  129. Serial.print(max_feedrate[1]/60 );
  130. SerialprintPGM(" Z" );
  131. Serial.print(max_feedrate[2]/60 );
  132. SerialprintPGM(" E" );
  133. Serial.print(max_feedrate[3]/60);
  134. SerialprintPGM("\nMaximum Acceleration (mm/s2):\n M201 X" );
  135. Serial.print(max_acceleration_units_per_sq_second[0] );
  136. SerialprintPGM(" Y" );
  137. Serial.print(max_acceleration_units_per_sq_second[1] );
  138. SerialprintPGM(" Z" );
  139. Serial.print(max_acceleration_units_per_sq_second[2] );
  140. SerialprintPGM(" E" );
  141. Serial.print(max_acceleration_units_per_sq_second[3]);
  142. SerialprintPGM("\necho: Acceleration: S=acceleration, T=retract acceleration\n M204 S" );
  143. Serial.print(acceleration );
  144. SerialprintPGM(" T" );
  145. Serial.print(retract_acceleration);
  146. SerialprintPGM("\necho: Advanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum xY jerk (mm/s), Z=maximum Z jerk (mm/s)");
  147. SerialprintPGM(" M205 S" );
  148. Serial.print(minimumfeedrate/60 );
  149. SerialprintPGM(" T" );
  150. Serial.print(mintravelfeedrate/60 );
  151. SerialprintPGM(" B" );
  152. Serial.print(minsegmenttime );
  153. SerialprintPGM(" X" );
  154. Serial.print(max_xy_jerk/60 );
  155. SerialprintPGM(" Z" );
  156. Serial.print(max_z_jerk/60);
  157. SerialprintPGM("\n" );
  158. #ifdef PIDTEMP
  159. SerialprintPGM("PID settings:");
  160. SerialprintPGM(" M301 P" );
  161. Serial.print(Kp );
  162. SerialprintPGM(" I" );
  163. Serial.print(Ki );
  164. SerialprintPGM(" D" );
  165. Serial.print(Kd);
  166. #endif
  167. }
  168. #endif