Skip to content

Instantly share code, notes, and snippets.

@rolfarley
Created November 13, 2012 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolfarley/4067762 to your computer and use it in GitHub Desktop.
Save rolfarley/4067762 to your computer and use it in GitHub Desktop.
// Derived from the USBFTDILoopback example sketch which is part of the
// librairies associated with the USB Shield available from Circuits at Home
//
// This interfaces with an FTDI based USB particulate sensor.
// The expected output is comma separated.
// The first value is the time in millseconds, followed by the frequency in Hz, and
// an optional third column (if enabled) is the temperature:
// 5, 622021929,28.5000
// 10, 622021909,28.5000
// 15, 622021929,28.5000
//
// To be added - initial configuration of PM sensor -
#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 <cdcftdi.h>
#include <printhex.h>
#include <message.h>
#include <hexdump.h>
#include <parsetools.h>
//#include <AndroidAccessory.h> -- this and Usb headers from ADK_release_20120606
// conflicts with Usb headers from USBShield
#include <adk.h>
#include <stdlib.h>
#include <stdio.h>
#include "pgmstrings.h"
//AndroidAccessory acc("Google, Inc.",
// "DemoKit",
// "DemoKit Arduino Board",
// "1.0",
// "http://www.android.com",
// "0000000012345678");
const int MAX_STRING_LEN=200;
// Data from Vector NAV IMU is of the form $VNYMR,+154.568,+000.896,-126.803,-02.2525,-02.1256,
const int YAW=2; // position of yaw,pitch,roll data in comma delimited output
const int PITCH=3;
const int ROLL=4;
const int NUM_IMU_FIELDS=3;
char *values[NUM_IMU_FIELDS]; // array to hold data to send to Android
char stringBuffer[MAX_STRING_LEN+1];
boolean processData;
class FTDIAsync : public FTDIAsyncOper
{
public:
virtual uint8_t OnInit(FTDI *pftdi);
};
uint8_t FTDIAsync::OnInit(FTDI *pftdi)
{
uint8_t rcode = 0;
rcode = pftdi->SetBaudRate(115200);
if (rcode)
{
ErrorMessage<uint8_t>(PSTR("SetBaudRate"), rcode);
return rcode;
}
rcode = pftdi->SetFlowControl(FTDI_SIO_DISABLE_FLOW_CTRL);
if (rcode)
ErrorMessage<uint8_t>(PSTR("SetFlowControl"), rcode);
return rcode;
}
USB Usb;
USBHub Hub(&Usb);
FTDIAsync FtdiAsync;
FTDI Ftdi(&Usb, &FtdiAsync);
ADK adk(&Usb, "Arduino Demo Kit", // Manufacturer Name
"ArduinoSensor", // Model Name
"Sketch to interface to USB PM Sensor", // Description (user-visible string)
"1.0", // Version
"http://www.android.com", // URL (web page to visit if no installed apps support the accessory)
"123456789"); // Serial Number (optional)
uint32_t next_time;
void setup()
{
Serial.begin( 115200 );
Serial.println("Start");
if (Usb.Init() == -1)
Serial.println("OSC did not start.");
delay( 200 );
next_time = millis() + 5000;
}
void loop()
{ // JJ
char *str;
char *p;
int field;
long PMValue;
byte msg[5];
uint16_t len;
uint8_t rcode;
long yaw = 622021909;
Usb.Task();
if( Usb.getUsbTaskState() == USB_STATE_RUNNING )
{ // II
char strbuf[] = "DEADBEEF";
//char strbuf[] = "The quick brown fox jumps over the lazy dog";
//char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage
Serial.print(".");
//rcode = Ftdi.SndData(strlen(strbuf), (uint8_t*)strbuf);
//if (rcode)
//ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
delay(50);
uint8_t buf[64];
for (uint8_t i=0; i<64; i++)
buf[i] = 0;
uint16_t rcvd = 64;
// rcode = Ftdi.RcvData(&rcvd, buf);
// if (rcode && rcode != hrNAK)
// ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
// Debug code - send dummy data
if(adk.isReady()) {
yaw += 1;
// Shift PM Value from long int to 4 byte msg, higher order byte in msg[1]
msg[0] = 0x1;
msg[1] = yaw >> 24;
msg[2] = yaw >> 16;
msg[3] = yaw >> 8;
msg[4] = yaw & 0xff;
Serial.print(msg[0], HEX);
Serial.print(msg[1], HEX);
Serial.print(msg[2], HEX);
Serial.print(msg[3], HEX);
Serial.print(msg[4], HEX);
len = sizeof(msg);
rcode = adk.SndData(len, msg);
//signature: SndData(uint16_t nbytes, uint8_t *dataptr);
if(rcode && rcode != hrNAK) {
Serial.print("Failed to send");
Serial.println(rcode);
}
else {
Serial.println("DEBUG Data send successful");
Serial.println(rcode);
}
}
delay(500);
} //II - USB State
} //JJ - loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment