Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
developers-guide [2017/01/25 18:54]
arthur
developers-guide [2017/04/27 10:21] (current)
72.168.144.31 Fix code block syntax
Line 1: Line 1:
 {{page>​contributing}} {{page>​contributing}}
  
-This page contains or will contain stuff you need to know when coding for smoothie. ​TODO : make ​proper intro.+This page contains or will contain stuff you need to know when coding for smoothie. ​TODOmake proper intro.
  
 How to submit a pull request to the Smoothieware Github project How to submit a pull request to the Smoothieware Github project
Line 12: Line 12:
 In smoothie configuration is stored in a configuration file, it is read at startup time ( when your module is being loaded ), and anytime the configuration is reloaded ( for example when the config file is changed ) In smoothie configuration is stored in a configuration file, it is read at startup time ( when your module is being loaded ), and anytime the configuration is reloaded ( for example when the config file is changed )
  
-If you want your config values to be read only upon module loading, but not when the config is reloaded, read your config values from the //​on_module_loaded//​ callback of your module : [[https://​github.com/​arthurwolf/​Smoothie/​blob/​bee725fcd5dce2162f643dd747fb95c1cc9f4242/​src/​modules/​utils/​pausebutton/​PauseButton.cpp#​L16|example]].+If you want your config values to be read only upon module loading, but not when the config is reloaded, read your config values from the //​on_module_loaded//​ callback of your module:​ [[https://​github.com/​arthurwolf/​Smoothie/​blob/​bee725fcd5dce2162f643dd747fb95c1cc9f4242/​src/​modules/​utils/​pausebutton/​PauseButton.cpp#​L16|example]].
  
 However this is not recommended. You probably want your configuration to be also read when the system says the configuration has been updated. ​ However this is not recommended. You probably want your configuration to be also read when the system says the configuration has been updated. ​
 What you want to do then is add a //​on_config_reload//​ handler to your module. What you want to do then is add a //​on_config_reload//​ handler to your module.
 That handler is called for all modules, when Smoothie wants the configuration reloaded. That handler is called for all modules, when Smoothie wants the configuration reloaded.
-And of course you also want to call that callback from on_module_loaded+And of course you also want to call that callback from //on_module_loaded//
  
-[[code type="​Cpp"​]]+<code cpp>
 void MyModule::​on_module_loaded() { void MyModule::​on_module_loaded() {
     // Settings     // Settings
Line 39: Line 39:
 </​code>​ </​code>​
  
-Now in our code if we want to read it we'll do something like that : +Now in our code if we want to read it we'll do something like that :
  
-[[code type="​Cpp"​]]+<code cpp>
 void MyModule::​on_config_reload(void* argument){ void MyModule::​on_config_reload(void* argument){
  // Config does not actually work like that, this is just an hypothetical example to explain, Don't do it like this.  // Config does not actually work like that, this is just an hypothetical example to explain, Don't do it like this.
Line 51: Line 51:
 If we do that for all config options, it's going to end up using quite a lot of flash storage space. If we do that for all config options, it's going to end up using quite a lot of flash storage space.
  
-The solution is [[http://​en.wikipedia.org/​wiki/​Checksum|Checksums]] : we ​don't store the actual string, but a checksum of that string we can compare to the checksums of the configuration lines in the config file and still know if they correspond to our string.+The solution is [[http://​en.wikipedia.org/​wiki/​Checksum|Checksums]]:​ we don't store the actual string, but a checksum of that string we can compare to the checksums of the configuration lines in the config file and still know if they correspond to our string.
 That way we don't actually store the string. ​ That way we don't actually store the string. ​
  
 An online tool to compute those when you need can be found [[http://​www.dioda.ro/​tst/​tstfletcher.php|here]]. An online tool to compute those when you need can be found [[http://​www.dioda.ro/​tst/​tstfletcher.php|here]].
-The actual code for the checksum calculation is in [[https://​github.com/​arthurwolf/​Smoothie/​blob/​edge/​src/​libs/​utils.cpp|src/​libs/​utils.cpp]] : +The actual code for the checksum calculation is in [[https://​github.com/​arthurwolf/​Smoothie/​blob/​edge/​src/​libs/​utils.cpp|src/​libs/​utils.cpp]] :
  
-[[code type="​Cpp"​]]+<code cpp>
 uint16_t get_checksum(string to_check){ uint16_t get_checksum(string to_check){
    // From: http://​en.wikipedia.org/​wiki/​Fletcher%27s_checksum    // From: http://​en.wikipedia.org/​wiki/​Fletcher%27s_checksum
Line 70: Line 70:
 </​code>​ </​code>​
  
-So, now our code will look more like : +So, now our code will look more like :
  
-[[code type="​Cpp"​]]+<code cpp>
  
 #define maximum_death_star_hourly_power_consumption_checksum CHECKSUM("​maximum_death_star_hourly_power_consumption"​) ​ #define maximum_death_star_hourly_power_consumption_checksum CHECKSUM("​maximum_death_star_hourly_power_consumption"​) ​
Line 94: Line 94:
 This is not very useful. It's what you get when you do :  This is not very useful. It's what you get when you do : 
  
-[[code type="​Cpp"​]]+<code cpp>
  this->​kernel->​config->​value(whatever_checksum);​  this->​kernel->​config->​value(whatever_checksum);​
 </​code>​ </​code>​
Line 104: Line 104:
 If your value is a string, you can access it using the as_string() method : If your value is a string, you can access it using the as_string() method :
  
-[[code type="​Cpp"​]]+<code cpp>
  this->​whatever_option = this->​kernel->​config->​value(whatever_checksum)->​as_string();​  this->​whatever_option = this->​kernel->​config->​value(whatever_checksum)->​as_string();​
 </​code>​ </​code>​
Line 115: Line 115:
 That's easy, just converts your string to a double :  That's easy, just converts your string to a double : 
  
-[[code type="​Cpp"​]]+<code cpp>
  double trouble = this->​kernel->​config->​value(whatever_checksum)->​as_number();​  double trouble = this->​kernel->​config->​value(whatever_checksum)->​as_number();​
 </​code>​ </​code>​
  
-Note : if you provided a default number, and the option is not found in the config file, that default will be returned. See bellow.+Note: if you provided a default number, and the option is not found in the config file, that default will be returned. See bellow.
  
 ==== Boolean ==== Boolean
Line 125: Line 125:
 This is convenient for example for stuff like :  This is convenient for example for stuff like : 
  
-[[code type="​Cpp"​]]+<code cpp>
 void Laser::​on_module_loaded() { void Laser::​on_module_loaded() {
     if( !this->​kernel->​config->​value( laser_module_enable_checksum )->​by_default(false)->​as_bool() ){ return; }      if( !this->​kernel->​config->​value( laser_module_enable_checksum )->​by_default(false)->​as_bool() ){ return; } 
 </​code>​ </​code>​
  
-This prevents the module from being loaded if the laser_module_enable config option is not present ( defaults to false ) or set to false explicitely.+This prevents the module from being loaded if the laser_module_enable config option is not present ( defaults to false ) or set to false explicitly.
  
 "​true"​ and "​1"​ are valid true values, anything else is false. "​true"​ and "​1"​ are valid true values, anything else is false.
Line 140: Line 140:
 They are generated from the result of as_string() ( making default values as strings possible ). They are generated from the result of as_string() ( making default values as strings possible ).
  
-[[code type="​Cpp"​]]+<code cpp>
  this->​step_pin ​                   = this->​kernel->​config->​value(extruder_step_pin_checksum ​         )->​by_default("​1.22"​ )->​as_pin()->​as_output();​  this->​step_pin ​                   = this->​kernel->​config->​value(extruder_step_pin_checksum ​         )->​by_default("​1.22"​ )->​as_pin()->​as_output();​
 </​code>​ </​code>​
Line 151: Line 151:
 Doubles are used as defaults only by the as_number() and as_bool() methods. Doubles are used as defaults only by the as_number() and as_bool() methods.
  
-[[code type="​Cpp"​]]+<code cpp>
  this->​acceleration ​             = this->​kernel->​config->​value(acceleration_checksum ​                   )->​by_default(1)->​as_number();​  this->​acceleration ​             = this->​kernel->​config->​value(acceleration_checksum ​                   )->​by_default(1)->​as_number();​
  this->​step_pin ​                   = this->​kernel->​config->​value(extruder_step_pin_checksum ​         )->​by_default("​1.22"​ )->​as_pin()->​as_output();​  this->​step_pin ​                   = this->​kernel->​config->​value(extruder_step_pin_checksum ​         )->​by_default("​1.22"​ )->​as_pin()->​as_output();​