Free Your Model Train (FYMT) — "Heißt du etwa Rumpelstilzchen?"

Ein Programm gibt Auskunft

Das Problem

Einem Microcontrollerboard sieht man nicht an, mit welchem Programm es gerade geladen ist. Manchmal hat man jedoch das dringende Bedürfnis zu erfahren, welches Programm in welcher Version dort gerade läuft.

Eine Lösung

Man muss das Programm daher veranlassen, über sich Auskunft zu geben. Dies kann beim Arduino über die USB-Schnittstelle erfolgen.

Ein kleines Programm soll dieses Konzept beispielhaft demonstrieren.

/*
 * Copyright 2017 - Michael Stehmann 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */
 
String VERSION = "0.2";
String NAME = "InfoTest";
String LASTAUTHOR = "Michael Stehmann";
String DATE = "2017-05-04";

const unsigned int LED_PIN = 13;     //usually the pin of the
                                     //built-in led
const unsigned int BAUD_RATE = 9600;
unsigned int COUNT = 0;

void setup() {
	pinMode(LED_PIN, OUTPUT);
	Serial.begin(BAUD_RATE);
}

void printinfo() {
	Serial.print("name of the program: ");
	Serial.println(NAME);
	Serial.print("version: ");
	Serial.println(VERSION);Serial.print("last editor: ");
	Serial.println(LASTAUTHOR);
	Serial.print("date of last change: ");
	Serial.println(DATE);
	Serial.println("");
	COUNT = 1;
}

void info() {
	if (Serial.available() > 0) {
		String COMMAND = Serial.readString();
		Serial.print(COMMAND);
		if (COUNT == 0) {
			printinfo();
			Serial.println("Type 'vvv' to repeat information");
			Serial.println("");
		}
		if (COMMAND == "vvv") {
			Serial.println("");
			printinfo();
		}  
	}
}

void loop() {
	const unsigned int DTIME = 500; //time of delay
                                    // in milliseconds
	info();

	/* Let it blink! Blinking is always fine. */
	digitalWrite(LED_PIN, HIGH);
	delay(DTIME);
	digitalWrite(LED_PIN, LOW);
	delay(DTIME);
}

Nach dem Öffnen der seriellen Konsole oder des "Serial Monitors" genügt das Drücken einer beliebigen Taste, um das Programm zur Selbstauskunft über Name, Version, Autor und letztem Änderungsdatum zu veranlassen.

Danach kann es bei Bedarf durch Eingabe von "vvv" erneut hierzu veranlasst werden.

Erläuterungen

Die Daten werden am Anfang des Programms Zeichenketten-Variablen zugeordnet:

String VERSION = "0.2";
String NAME = "InfoTest";
String LASTAUTHOR = "Michael Stehmann";
String DATE = "2017-05-04";

Die Funktion printinfo() bewirkt die Ausgabe dieser Daten.

Die Funktion info() steuert, wann diese Ausgabe erfolgt. Sie ruft gegebenenfalls die Funktion printinfo() zur Ausgabe auf.

Die Funktion loop() wiederum ruft bei jedem Durchlauf die Funktion info() zur Prüfung, ob eine Ausgabe erfolgen soll, auf.

Als "Nutzlast" lässt dieses Programm eine LED, welche am Pin 13 angeschlossen ist, blinken. Die Leuchtdauer brträgt 500 Millisekunden, die Zeit zwischen den Leuchtphasen ist gleich lang.

An Pin 13 ist regelmäßig die auf dem Board vorhandene LED angeschlossen.

Das Programm kann unschwer um weitere Angaben erweitert werden.

Das Konzept von FYMT

Dokumentation zu FYMT