// Demonstrate using a custom category with the 'Profile' profiling object
//
// This sketch is just a small extension to profile2.cpp

void reportProfile(u8 *) {
  logNormal("BG=%f SI=%f OI=%f PH=%f AH=%f S0=%f\n",
            Profile.getPPM(TICKER_BACKGROUND)/10000.0,
            Profile.getPPM(TICKER_SERIAL_ISR)/10000.0,
            Profile.getPPM(TICKER_OTHER_ISR)/10000.0,
            Profile.getPPM(TICKER_PACKET_HANDLER)/10000.0,
            Profile.getPPM(TICKER_ALARM_HANDLER)/10000.0,
            Profile.getPPM(TICKER_SKETCH0)/10000.0);
}

void doWork(u32 when) {
  // Normally, time spent in this alarm handler would be assigned to
  // TICKER_ALARM_HANDLER.  But we'll assign it to SKETCH0 instead:

  Ticker.pushTime(TICKER_SKETCH0); // Start billing to a custom category

  // Here we just waste time with a busy loop.  (Note the 'volatile'
  // keeps the compiler from throwing away the whole loop!)
  for (volatile u32 i = 0; i < 100000; ++i) { }  

  Alarms.set(Alarms.currentAlarmNumber(), when+250); // Run four times a second

  Ticker.popTime();                // Finished with our custom work
}

void setup() {
  Profile.begin();              // Start longer-term profiling system
  QLED.begin();                 // Let's use QLEDs to drive some alarm usage
  Body.reflex('p',reportProfile);
  Alarms.set(Alarms.create(doWork),millis()); // Create and schedule a worker
}

void loop() {
  // Spin the LEDs
  for (u32 i = 0, f = NORTH; i <= 3; ++i, f = CLOCKWISE_FACE(f)) {
    QLED.off(pinInFace(FACE_LED_PIN,f),i*80);      // First, increasing off time
    QLED.on(pinInFace(FACE_LED_PIN,f),100);        // then same amount of blink time
    QLED.off(pinInFace(FACE_LED_PIN,f),(3-i)*80);  // then decreasing off time to even up
  }

  // once per second
  delay(1000);
}

