Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit ae9023e

Browse files
committed
improved M117 and M226
M117 now deals better with checksums on the end of the line and blank messages (reset to nothing). M226 now handles cases with no P or S value, waits for button to click and release.
1 parent 4f11bf5 commit ae9023e

File tree

2 files changed

+345
-302
lines changed

2 files changed

+345
-302
lines changed

Makelangelo-firmware.ino

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,20 @@ float parseNumber(char code, float val) {
489489
}
490490

491491

492+
/**
493+
* @return 1 if the character is found in the serial buffer, 0 if it is not found.
494+
*/
495+
char hasGCode(char code) {
496+
char *ptr = serialBuffer; // start at the beginning of buffer
497+
while ((long)ptr > 1 && (*ptr) && (long)ptr < (long)serialBuffer + sofar) { // walk to the end
498+
if (*ptr == code) { // if you find code on your walk,
499+
return 1; // found
500+
}
501+
ptr = strchr(ptr, ' ') + 1; // take a step from here to the letter after the next space
502+
}
503+
return 0; // not found
504+
}
505+
492506
/**
493507
* G4 [Snn] [Pnn]
494508
* Wait S milliseconds and P seconds.
@@ -575,10 +589,11 @@ void parseToolOffset(int toolID) {
575589
set_tool_offset(toolID,offset);
576590
}
577591

592+
578593
/**
579594
* @return 1 if CRC ok or not present, 0 if CRC check fails.
580595
*/
581-
char checkCRCisOK() {
596+
char checkLineNumberAndCRCisOK() {
582597
// is there a line number?
583598
long cmd = parseNumber('N', -1);
584599
if (cmd != -1 && serialBuffer[0] == 'N') { // line number must appear first on the line
@@ -590,11 +605,20 @@ char checkCRCisOK() {
590605
}
591606

592607
// is there a checksum?
593-
if (strchr(serialBuffer, '*') != 0) {
608+
int i;
609+
for(i=strlen(serialBuffer)-1;i>=0;--i) {
610+
if(serialBuffer[i]=='*') {
611+
break;
612+
}
613+
}
614+
615+
if(i>=0) {
594616
// yes. is it valid?
595617
char checksum = 0;
596-
int c = 0;
597-
while (serialBuffer[c] != '*' && c < MAX_BUF) checksum ^= serialBuffer[c++];
618+
int c;
619+
for(c=0;c<i;++c) {
620+
checksum ^= serialBuffer[c];
621+
}
598622
c++; // skip *
599623
int against = strtod(serialBuffer + c, NULL);
600624
if ( checksum != against ) {
@@ -608,6 +632,9 @@ char checkCRCisOK() {
608632
return 0;
609633
}
610634

635+
// remove checksum
636+
serialBuffer[i]=0;
637+
611638
line_number++;
612639
}
613640

@@ -623,13 +650,20 @@ void parseMessage() {
623650
#ifdef HAS_LCD
624651
int i,j=0;
625652
for(i=0;i<strlen(serialBuffer);++i) {
626-
if(serialBuffer[i]==' ') {
627-
++j;
628-
if(j==2) break;
653+
if((serialBuffer[i]=='M'||serialBuffer[i]=='m')&&
654+
serialBuffer[i+1]=='1'&&
655+
serialBuffer[i+2]=='1'&&
656+
serialBuffer[i+3]=='7') {
657+
i+=4;
658+
//Serial.print("Found M117:");
659+
//Serial.println(serialBuffer+i);
660+
break;
629661
}
630662
}
663+
631664

632-
if(i==strlen(serialBuffer)) {
665+
if(i>=strlen(serialBuffer)) {
666+
//Serial.println("No message.");
633667
// no message
634668
lcd_message[0]=0;
635669
lcd_message[LCD_WIDTH + 1]=0;
@@ -638,17 +672,19 @@ void parseMessage() {
638672

639673
// preserve message for display
640674
int top = min(LCD_MESSAGE_LENGTH,MAX_BUF);
641-
//Serial.print("top "); Serial.println(top);
642-
//Serial.print(">>");
643675

644-
char *buf = serialBuffer[i];
676+
char *buf = serialBuffer+i;
677+
while(*buf==' ') ++buf; // eat whitespace
678+
679+
//Serial.print("message found:");
645680
i=j=0;
646681
while(isPrintable(*buf) && *buf!='\r' && *buf!='\n' && i<LCD_MESSAGE_LENGTH-1) {
647682
lcd_message[i]=*buf;
648-
//Serial.print(i); Serial.print(j); Serial.print('\t'); Serial.println(*buf);
683+
//Serial.print(*buf);
649684
++i;
650685
++j;
651686
if((j%LCD_WIDTH)==0) {
687+
//Serial.println();
652688
lcd_message[i]=0;
653689
++i;
654690
}
@@ -657,7 +693,7 @@ void parseMessage() {
657693
while(i<LCD_MESSAGE_LENGTH) {
658694
lcd_message[i++]=0;
659695
}
660-
//Serial.print("<<END\n");
696+
//Serial.println();
661697
#endif
662698
}
663699

@@ -668,16 +704,22 @@ void parseMessage() {
668704
* Command is ignored if there is no LCD panel (and no button to press)
669705
*/
670706
void pauseForUserInput() {
707+
wait_for_empty_segment_buffer();
708+
if(hasGCode('P') || hasGCode('S')) {
709+
int pin = parseNumber('P', BTN_ENC);
710+
int newState = parseNumber('S', 0);
711+
671712
#ifdef HAS_LCD
672-
int pin = parseNumber('P', BTN_ENC);
673-
int newState = parseNumber('S', 0);
674-
newState = (newState==1)?HIGH:LOW;
675-
676-
while(digitalRead(pin)!=newState) {
677-
SD_check();
678-
LCD_update();
679-
}
713+
newState = (newState==1)?HIGH:LOW;
714+
while(digitalRead(pin)!=newState);
680715
#endif
716+
} else {
717+
#ifdef HAS_LCD
718+
// does not have P or S. Wait for click and release of button.
719+
while(digitalRead(BTN_ENC)!=LOW);
720+
while(digitalRead(BTN_ENC)!=HIGH);
721+
#endif
722+
}
681723
}
682724

683725

@@ -702,7 +744,7 @@ void parseBeep() {
702744
*/
703745
void processCommand() {
704746
if (serialBuffer[0] == ';') return; // blank lines
705-
if(!checkCRCisOK()) return; // message garbled
747+
if(!checkLineNumberAndCRCisOK()) return; // message garbled
706748

707749
if (!strncmp(serialBuffer, "UID", 3)) {
708750
robot_uid = atoi(strchr(serialBuffer, ' ') + 1);

0 commit comments

Comments
 (0)