Advertisements

Kenmore 158 Motor Controller: Button Command Decoder

Now that the sewing machine motor controller receives commands from the UI (or typed in on a console), it must decode them. The “parser” doesn’t amount to much, because the commands consist of exactly two characters wrapped in square brackets. For simplicity, if the format doesn’t match or the command isn’t exactly right, the decoder simply tosses it on the floor and moves on:

void ParseCmd(char *pBuff) {

	if ((CmdBuffer[0] != '[') || (CmdBuffer[3] != ']')) {
		printf("** Bad cmd format: %s\r\n",CmdBuffer);
		return;	
	}

	switch (CmdBuffer[1]) {
	case 'N':							// needle park position
		switch (CmdBuffer[2]) {
		case 'u':
			MotorDrive.ParkPosition = NS_UP;
//			ParkNeedle(NS_UP);
			break;
		case 'a':
			MotorDrive.ParkPosition = NS_NONE;
			break;
		case 'd':
			MotorDrive.ParkPosition = NS_DOWN;
//			ParkNeedle(NS_DOWN);
			break;
		default:
			printf("** Bad Needle cmd: %s\r\n",CmdBuffer);
		}
		break;
	case 'P':							// pedal mode
		switch (CmdBuffer[2]) {
		case 'r':
			MotorDrive.PedalMode = PD_RUN;
			break;
		case '1':
			MotorDrive.PedalMode = PD_SINGLE;
			break;
		case 'f':
			MotorDrive.PedalMode = PD_FOLLOW;
			break;
		default:
			printf("** Bad Pedal cmd: %s\r\n",CmdBuffer);
		}
		break;
	case 'S':							// motor speed range
		switch (CmdBuffer[2]) {
		case 'h':
			MotorDrive.SpeedRange = SPEED_HIGH;
			PedalMaxClamp = PEDALMAX;
			break;
		case 'm':
			MotorDrive.SpeedRange = SPEED_MEDIUM;
			PedalMaxClamp = (3 * PEDALMAX) / 4;
			break;
		case 'l':
			MotorDrive.SpeedRange = SPEED_LOW;
			PedalMaxClamp = PEDALMAX / 2;
			break;
		default:
			printf("** Bad Speed cmd: %s\r\n",CmdBuffer);
		}
		break;
	default:
		printf("** Bad command string: %s\r\n",CmdBuffer);
	}
	
	return;
}

So much for recursive descent parser design theory, eh?

Advertisements

,

  1. #1 by madbodger on 2015-02-06 - 09:23

    No switch(*CmdBuffer++)?

    • #2 by Ed on 2015-02-06 - 09:32

      That’s entirely too practical. [grin]

      With a total of nine possible commands with two fixed characters each, I can afford a lot of dumb, plus a dose of dead-simple error checking.

      • #3 by madbodger on 2015-02-06 - 09:45

        I wasn’t really being serious – I used to do that sort of coding, but these days I aim for readability over just conciseness. I once even had an argument parser containing “while (++argv)”. Yecch.