Scott Penrose

Teensy Console Input

Scott is an expert software developer with over 30 years experience, specialising in education, automation and remote data.

Combining Teensy Console and Serial and Teensy EEPROM Config allows you to write a powerful configuration system that can be changed via serial console(s).

#!C

// ... (see data setup in console and EEPROM config)

void setup() {
...
}

void loop() {
	processConsole();  // Process your command inputs
        ... do other stuff ...
}

// TODO - you should use define for your serial, e.g. #define bluetooth Serial2
void processConsole() {
	// Support for multiple console inputs - e.g. Serial and Serial2
	while (Serial.available() || Serial2.available()) {
		if (Serial.available())
			doCommand(&Serial);
		if (Serial2.available())
			doCommand(&Serial2);
	}
}

void doCommand(Stream *local) {
	long d;		// Integer In
	String s;	// String In

	char in = local->read();

	switch (in) {
		case '\r':
		case '\n':
				break;
		case 'h':	   // Help
			showHelp();
			break;
		case 'T': // SET TIME
			d = local->parseInt();
			if (d > 10000) {
				Teensy3Clock.set(d);
				setTime(d);
				consolePrint(F("NOTE: Time has been updated - UTC\r\n"));
			}
			else {
				consolePrint(F("ERROR: Invalid time update - UTC\r\n"));
			}
			break;
		case 'b':	// Brightness -255..255
			d = local->parseInt();
			if ( (d >= -255) && (d <= 255) ) {
				config.displayBright = d;
				writeConfig();
				displayBright();
			}
			break;
	}
}

// Show help INCLUDING existing values
void showHelp() {
	consolePrint(F("# HELP...\r\n"));
	consolePrint(F("#  T{nn}        Set time.        Current=%u\r\n"), now());
	consolePrint(F("#  b{-255..255} Set brightness.  Current=%d\r\n"), config.displayBright);
}

  • Teensy
  • Serial