speed2.cpp
This sketch sets up the processor speed based on a value read from EEPROM. To avoid a blink code, the sketch ensures the mhz value is in the range of 12..72, before passing it to
SFBProcessor::setMHz(), but note that only multiples of 12MHz are supported. If an 's32' packet, say, is handled by this sketch, the processor speed after rebooting will not be 32MHz, but 24MHz --- the next lower multiple of 12MHz.
bool badMHz(u32 v) { return v < 12 || v > 72; }
void handleSpeed(u8 * packet) {
u32 arg;
if (packetScanf(packet,"s%d\n",&arg) != 3 || badMHz(arg))
return;
u8 mhz = arg;
EEPROM.write(0, &mhz, 1);
reenterBootloader();
}
void setup() {
u8 mhz;
EEPROM.read(0, &mhz, 1);
if (badMHz(mhz)) mhz = 72;
Processor.setMHz(mhz);
Body.reflex('s',handleSpeed);
}
void loop() {
println(Processor.getMHz());
delay(2000);
}