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; }  // Helper function

void handleSpeed(u8 * packet) {
  u32 arg;
  if (packetScanf(packet,"s%d\n",&arg) != 3 || badMHz(arg)) 
    return;
  u8 mhz = arg;                 // Stash result in a byte
  EEPROM.write(0, &mhz, 1);     // Write it to address 0
  reenterBootloader();          // And reboot
}

void setup() {
  u8 mhz;             
  EEPROM.read(0, &mhz, 1);      // Read 1 byte from address 0
  if (badMHz(mhz)) mhz = 72;    // Default to 72MHz
  Processor.setMHz(mhz);        // Set the processor speed

  Body.reflex('s',handleSpeed); // Create reflex to change speed and reboot
}

void loop() { 
  println(Processor.getMHz());  // Report current speed
  delay(2000);                  // Wait a while
}

Generated on Fri Apr 22 06:54:11 2011 for SFB by doxygen 1.5.9