<p><a href="https://3dprinting.stackexchange.com/questions/3586/adding-custom-m-codes-to-marlin">Adding custom M Codes to Marlin</a> doesn't work for Marlin 2.0</p>

<p>How would one go about adding custom G codes or M Codes to Marlin 2.0? The Marlin_main.cpp file does not exist. </p>

<p>In general for Marlin 2.0, things are organized better, but split into more files. </p>

[{'answer_id': 13706, 'author': '0scar', 'author_id': 5740, 'author_profile': 'https://3dprinting.stackexchange.com/users/5740', 'pm_score': 2, 'selected': False, 'text': '<p>The code in 2.0.x is similar to the old branch 1.1.x, G-code is parsed in <a href="https://github.com/MarlinFirmware/Marlin/blob/2.0.x/Marlin/src/gcode/gcode.cpp" rel="nofollow noreferrer"><code>gcode.cpp</code></a>, specifically in <code>process_parsed_command</code>:</p>\n\n<pre><code>void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/)\n</code></pre>\n\n<p>In the case statement the codes read from the G-code files are parsed (interpreted) and the appropriate method is called (e.g. <code>G28()</code> calls <a href="https://github.com/MarlinFirmware/Marlin/blob/2.0.x/Marlin/src/gcode/calibrate/G28.cpp" rel="nofollow noreferrer"><code>void GcodeSuite::G28()</code></a>)</p>\n\n<p>If you want to create your own codes, it could be an idea to start there. Also think of using a different letter and/or codes in the 10,000 range so that it will not collide with new implemented G-codes.</p>\n'}, {'answer_id': 13719, 'author': 'K Mmmm', 'author_id': 10369, 'author_profile': 'https://3dprinting.stackexchange.com/users/10369', 'pm_score': 1, 'selected': True, 'text': '<ol start="0">\n<li>Choose a code in the &gt;10,000 in case new codes are added. But in this example I will choose 13</li>\n<li>Navigate to \'src\' folder of Marlin</li>\n<li>Edit the file <code>gcode.cpp</code> around line 223 to have a new unused number. For example, this will create a  new G code function for the label <code>G13</code>.</li>\n</ol>\n<pre><code>  ...\n  // Handle a known G, M, or T\n  switch (parser.command_letter) {\n    case \'G\': switch (parser.codenum) {\n      case 13: G13(); break;\n      case 0: case 1: G0_G1(  \n  ...\n</code></pre>\n<ol start="3">\n<li><p>On line 375 of <code>gcode.h</code> add:  <code> static void G13();</code> to declare it.</p>\n</li>\n<li><p>In my case i was reading values from an analog system. So I went to <code>src/temperatures</code> and copied <code>M105.cpp</code> to be <code>G13.cpp</code>. Then inside the file I replaced <code>GcodeSuite::M105</code> to be <code>GcodeSuite::G13</code>. I am using this to take in the weight of something using a [scale][1] but for now I just want to test functionality so here is my test function:</p>\n</li>\n</ol>\n<pre><code>\nvoid GcodeSuite::G13() {\n\n  SERIAL_ECHOPGM(MSG_OK);\n  SERIAL_ECHOLNPGM(&quot;here is where weights are broadcast&quot;);\n\n}\n</code></pre>\n<p>And again this is the only part I changed in my new copy of M105.cpp (a new file named G13.cpp). There is still more stuff in the file than just these few lines.</p>\n<ol start="5">\n<li><p>Upload to board</p>\n</li>\n<li><p>When going to octoprint and typing in <code>G13</code> I get:</p>\n</li>\n</ol>\n<pre><code>\nSend: G13\nRecv: okhere is where weights are broadcast\n</code></pre>\n<p>A bit more work can be done to make it look nice, but this was the hard part.\n[1]: <a href="https://www.instructables.com/id/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/" rel="nofollow noreferrer">https://www.instructables.com/id/Arduino-Bathroom-Scale-With-50-Kg-Load-Cells-and-H/</a></p>\n'}]
['https://3dprinting.stackexchange.com/questions/13705', 'https://3dprinting.stackexchange.com', 'https://3dprinting.stackexchange.com/users/10369/']