// Handy dandy tool to send a reset command to the arduino. If you then wait
// 4 seconds, you should be able to start avrdude and upload to the device.

#include <stdio.h>
#include "LibAard.h"
#include "Commands.h"
#include "AardRemoteMessage.h"
#include "RawIR.h"

int
main(int argc, char ** argv) {
   AardRemoteMessage * msg;

   if (argc != 2) {
      fprintf(stderr, "usage: aardreset devicename\n");
      return 2;
   }
   LibAard l;
   if (l.OpenAardRemote(argv[1]) < 0) {
      fprintf(stderr, "open of %s failed\n", argv[1]);
      return 2;
   }

   // Get synced up and make sure the aardremote is talking.

   int bilgecount = 0;
   while (bilgecount < 4) {
      int code = l.WaitForStartup();
      if (code == MKAS_RESET) {
         bilgecount = 0;
      } else if (code == MKAS_BILGE) {
         ++bilgecount;
      } else {
         bilgecount = 0;
      }
   }

   // See if I can reset the arduino with watchdog timer.

   l.SendCommand(MKCR_RESET);

   printf("OK, you have 4 seconds...\n");
   return 0;
}
