コード例 #1
0
ファイル: mainwindow.cpp プロジェクト: kosme/GrblHoming
void MainWindow::preProcessFile(QString filepath)
{
    QFile file(filepath);
    if (file.open(QFile::ReadOnly))
    {
        posList.clear();

        float totalLineCount = 0;
        QTextStream code(&file);
        while ((code.atEnd() == false))
        {
            totalLineCount++;
            code.readLine();
        }
        if (totalLineCount == 0)
            totalLineCount = 1;

        code.seek(0);

        double x = 0;
        double y = 0;
        double i = 0;
        double j = 0;
        bool arc = false;
        bool cw = false;
        bool mm = true;
        int index = 0;
        int g = 0;

        bool zeroInsert = false;
        do
        {
            QString strline = code.readLine();

            index++;

            GCode::trimToEnd(strline, '(');
            GCode::trimToEnd(strline, ';');
            GCode::trimToEnd(strline, '%');

            strline = strline.trimmed();

            if (strline.size() == 0)
            {}//ignore comments
            else
            {
                strline = strline.toUpper();
                strline.replace("M6", "M06");
                strline.replace(QRegExp("([A-Z])"), " \\1");
                strline.replace(QRegExp("\\s+"), " ");
                //if (strline.contains("G", Qt::CaseInsensitive))
                {
                    if (processGCode(strline, x, y, i, j, arc, cw, mm, g))
                    {
                        if (!zeroInsert)
                        {
                            // insert 0,0 position
                            posList.append(PosItem(0, 0, 0, 0, false, false, mm, 0));
                            zeroInsert = true;
                        }
                        posList.append(PosItem(x, y, i, j, arc, cw, mm, index));

                        //printf("Got G command:%s (%f,%f)\n", strline.toLocal8Bit().constData(), x, y);
                    }
                }
            }
        } while (code.atEnd() == false);

        file.close();

        emit setItems(posList);
    }
    else
        printf("Can't open file\n");
}
コード例 #2
0
ファイル: gcode.c プロジェクト: Sagittarii/stepper
void parseCode(const char* const data)
{
    // Get Letter and Number
    char letter = '\0';
    float value = 0;

    cmd_param param;
    memset(&param, 0, sizeof(param)); // Init to zero structure (float at 0 should be 0.0 value).


    char* chrPtr = &data[0];

//    puts("Start parsing\n");
    while ( (*chrPtr != '\0') && (chrPtr < (data + strlen(data))) ) // While not at the end of the string
    {
//        if(*chrPtr == ' ')
//        {
////            puts("Space, continue\n");
//            chrPtr++;
//            continue;
//        }

        if( ( (*chrPtr >= 'A')
              && (*chrPtr <= 'Z') )
                || (*chrPtr == '*') ) // Is a Valid GCode letter
        {
//            puts("Valid letter found\n");
            letter = *chrPtr;

            chrPtr++; // Go to next character. Should be a number or a space

            if (*chrPtr != '\0') // If it is not end of string
            {
//                puts("Value found\n");
                value = strtof(chrPtr, &chrPtr);

                // affect value to the parameter list
                switch(letter)
                {
                    case 'E': // E = Precision feedrate for threading on lathes
                        param.e = value;
                        param.e_set = true;
                        break;
                    case 'F': // F = Defines feed rate
                        param.f = value;
                        param.f_set = true;
                        break;
                    case 'G': //
                        param.g = value;
                        param.g_set = true;
                        break;

                    case 'I': // I = Defines arc center in X axis for G02 or G03 arc commands.
                        param.i = value;
                        param.i_set = true;
                        break;
                    case 'J': // J = Defines arc center in Y axis for G02 or G03 arc commands.
                        param.j = value;
                        param.j_set = true;
                        break;
                    case 'K': // K = Defines arc center in Z axis for G02 or G03 arc commands.
                        param.k = value;
                        param.k_set = true;
                        break;
                    case 'M': //
                        param.m = value;
                        param.m_set = true;
                        break;
                    case 'N': //
                        param.n = value;
                        param.n_set = true;
                        break;
                    case '*': //
                        param.checksum = value;
                        break;

                    case 'P': // P = Serves as parameter address for various G and M codes
                        param.p = value;
                        param.p_set = true;
                        break;

                    case 'S': // S = Defines speed, either spindle speed or surface speed depending on mode
                        param.s = value;
                        param.s_set = true;
                        break;
                    case 'T': //
                        param.t = value;
                        param.t_set = true;
                        break;

                    case 'X': // X = Absolute or incremental position of X axis.
                        param.x = value;
                        param.x_set = true;
                        break;
                    case 'Y': // Y = Absolute or incremental position of Y axis
                        param.y = value;
                        param.y_set = true;
                        break;
                    case 'Z': // Z = Absolute or incremental position of Z axis
                        param.z = value;
                        param.z_set = true;
                        break;
                    default:
                        //
                        break;
                }
//                break; /// \todo Debug line...

            }
//            puts("Next char\n");
        }
        else
        {
            chrPtr++;
        }
    }

//    puts("Finished parsing\n");

    //Dump the parameters read:
//    printf("e=%f\n", param.e);
//    printf("f=%f\n", param.f);
//    printf("g=%f\n", param.g);
//    printf("i=%f\n", param.i);
//    printf("j=%f\n", param.j);
//    printf("k=%f\n", param.k);
//    printf("m=%f\n", param.m);
//    printf("p=%f\n", param.p);
//    printf("s=%f\n", param.s);
//    printf("t=%f\n", param.t);
//    printf("x=%f\n", param.x);
//    printf("y=%f\n", param.y);
//    printf("z=%f\n", param.z);


    if (param.g_set)
    {
        processGCode(param);
    }
    else if (param.m_set)
    {
        processMCode(param);
    }
    else if (param.t_set)
    {
        processTCode(param);
    }

}