<commit_before>/* 
 * File:   thimble.cpp
 * This is the main file - Everything happens here.
 * 
 */

#include <avr/wdt.h>

#include "thimble.hpp"
#include "stepper.hpp"
#include "coordsystem.hpp"
#include "gcodeinterpreter.hpp"

void setup();
void loop();

String input = "";
GcodeStack gcs;
ShiftRegister shiftregister;
StepperStateMachine ssm;
SensorSystem        sensors;
CoordSystem* coord = 0;

Stepper*   stepperx;
Stepper*   steppery;
Stepper* stepperz;

void setup() {
#ifdef WATCHDOG
    wdt_disable(); // just in case
    wdt_enable(WDTO_4S);
    wdt_reset();
#endif
    
    pinMode(STATLED, OUTPUT); // status LED
    pinMode(XSTOP_PIN, INPUT);
    pinMode(YSTOP_PIN, INPUT);
    pinMode(ZSTOP_PIN, INPUT);
    input.reserve(127);

    coord = new Cartesian();
    shiftregister = ShiftRegister(SR_CLOCKPIN, SR_LATCHPIN, SR_DATAPIN, 1);
    stepperx = new XSTEPPER_TYPE(XSTEPPER_OPTIONS);
    steppery = new YSTEPPER_TYPE(YSTEPPER_OPTIONS);
    stepperz = new ZSTEPPER_TYPE(ZSTEPPER_OPTIONS);
    ssm = StepperStateMachine(&sensors, stepperx, steppery, stepperz);
    
    Serial.begin(115200);
    showMem();
    Serial.println("start");
}

void loop() {
#ifdef WATCHDOG
    wdt_reset();
#endif
    
    // read the Serial port for commands
    while(Serial.available()) {
        char in = (char) Serial.read();
        input += in;
        if(in == '\n')
            break;
    }
    
    if(input.length() > 1) {
        showMem();

        Serial.print("//");
        Serial.println(input);
        
        // parse gcode stuff
        gcs.pushAndParse(input);

        // strictly debug stuff here - may get removed later
        GcodeInstruction gci = gcs.popBuffer();
        Serial.print("//gcode instruction: ");
        Serial.println(gci.instruction);
        Serial.print("//gcode X: ");
        Serial.println(gci.argument[0]);
        Serial.print("//gcode Y: ");
        Serial.println(gci.argument[1]);
        Serial.print("//gcode Z: ");
        Serial.println(gci.argument[2]);
        Serial.print("//gcode E: ");
        Serial.println(gci.argument[3]);
        
        switch(gci.instruction) {
            case G0:
            case G1:
                moveHead(gci.argument);
                break;
            case G28:
                homeHead(gci.argument);
                break;
        }
        
        input = "";
    }
    
    int status = ssm.updateSteppers();
    status = coord->checkHome(status);
    if(status != 0) {   // hardware error - we're shutting down
        Serial.println(G_ERROR);
    }

    delay(5);
}
<commit_msg>More fixes (String.reserve corrupted ends of strings for some reason)<commit_after>/* 
 * File:   thimble.cpp
 * This is the main file - Everything happens here.
 * 
 */

#include <avr/wdt.h>

#include "thimble.hpp"
#include "stepper.hpp"
#include "coordsystem.hpp"
#include "gcodeinterpreter.hpp"

void setup();
void loop();

String input = "";
GcodeStack gcs;
ShiftRegister shiftregister;
StepperStateMachine ssm;
SensorSystem        sensors;
CoordSystem* coord = 0;

Stepper*   stepperx;
Stepper*   steppery;
Stepper* stepperz;

void setup() {
#ifdef WATCHDOG
    wdt_disable(); // just in case
    wdt_enable(WDTO_4S);
    wdt_reset();
#endif
    
    pinMode(STATLED, OUTPUT); // status LED
    pinMode(XSTOP_PIN, INPUT);
    pinMode(YSTOP_PIN, INPUT);
    pinMode(ZSTOP_PIN, INPUT);
    //input.reserve(127);

    coord = new Cartesian();
    shiftregister = ShiftRegister(SR_CLOCKPIN, SR_LATCHPIN, SR_DATAPIN, 1);
    stepperx = new XSTEPPER_TYPE(XSTEPPER_OPTIONS);
    steppery = new YSTEPPER_TYPE(YSTEPPER_OPTIONS);
    stepperz = new ZSTEPPER_TYPE(ZSTEPPER_OPTIONS);
    ssm = StepperStateMachine(&sensors, stepperx, steppery, stepperz);
    
    Serial.begin(115200);
    showMem();
    Serial.println("start");
}

void loop() {
#ifdef WATCHDOG
    wdt_reset();
#endif
    
    // read the Serial port for commands
    while(Serial.available()) {
        char in = (char) Serial.read();
        if(in == '\n')
            break;
        else
            input += in;
    }
    
    if(input.length() > 1) {
        showMem();

        Serial.print("//");
        Serial.println(input);
        
        // parse gcode stuff
        if(gcs.pushAndParse(input) != 0) {
            Serial.println(G_ERROR);
        }

        // strictly debug stuff here - may get removed later
        GcodeInstruction gci = gcs.popBuffer();
        Serial.print("//gcode instruction: ");
        Serial.println(gci.instruction);
        Serial.print("//gcode X: ");
        Serial.println(gci.argument[0]);
        Serial.print("//gcode Y: ");
        Serial.println(gci.argument[1]);
        Serial.print("//gcode Z: ");
        Serial.println(gci.argument[2]);
        Serial.print("//gcode E: ");
        Serial.println(gci.argument[3]);
        
        switch(gci.instruction) {
            case G0:
            case G1:
                moveHead(gci.argument);
                break;
            case G28:
                homeHead(gci.argument);
                break;
        }
        
        input = "";
    }
    
    int status = ssm.updateSteppers();
    status = coord->checkHome(status);
    if(status != 0) {   // hardware error - we're shutting down
        Serial.println(G_ERROR);
    }

    delay(5);
}

{'fraction_non_alphanumeric': 0.13169811320754718, 'fraction_numerical': 0.008867924528301888, 'mean_word_length': 2.2402200488997557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4684964', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1732, 'n_words': 360}