Newer
Older
weather_station / WeatherStation.ino

#include <avr/pgmspace.h>

#include <avrpins.h>
#include <max3421e.h>
#include <usbhost.h>
#include <usb_ch9.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <address.h>
#include <hid.h>
#include <hiduniversal.h>

#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>

#include "hidwh1080rptparser.h"

#define XBEE_SLEEP 2
#define MAX_RESET 7 //MAX3421E pin 12
#define MAX_GPX   8 //MAX3421E pin 17

USB                                             Usb;
USBHub                                          Hub(&Usb);
HIDUniversal                                    Hid(&Usb);

WH1080ReportParser parser;

void startUsb()
{
  // Set up reset and GPX ports -- USB Host library seems to be missing this,
  // see https://github.com/watterott/KnowledgeBase/wiki/SparkFun-USB-Host-Shield
  pinMode(MAX_GPX, INPUT);
  pinMode(MAX_RESET, OUTPUT);
  digitalWrite(MAX_RESET, LOW);
  delay(20); //wait 20ms
  digitalWrite(MAX_RESET, HIGH);
  delay(20); //wait 20ms

  if (Usb.Init() == -1)
      Serial.println("{\"error\":\"OSC did not start.\"}");
      
  delay( 200 );

  if (!Hid.SetReportParser(0, &parser))
      ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1  ); 
}

void setup()
{
  Serial.begin( 9600 );
  pinMode(XBEE_SLEEP, OUTPUT);   // XBee sleep pin (DTR)
  digitalWrite(XBEE_SLEEP, LOW); // ensure awake
  
  startUsb();
}

void loop()
{
    Usb.Task();
    if ((Usb.getUsbTaskState() == USB_STATE_RUNNING) && parser.startDelay && (parser.startDelay <= millis())) {
      int rcode = Hid.SetReport(0, 0, 0x02, 0x01, sizeof(parser.report), parser.report);
      if (rcode != 0) {
        Serial.print("{\"error\":\"Rcode = ");
        Serial.print(rcode, HEX);
        Serial.println("\"}");
        parser.startDelay = millis() + 5000;
      } else {
        parser.startDelay = 0;
      }
    }
/*    if ((parser.lastMillis + 60000) < millis()) {
      parser.lastMillis = millis();
      startUsb();
    } */
}