Skip to content

Instantly share code, notes, and snippets.

@briancline
Last active August 29, 2015 13:57
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 briancline/9428940 to your computer and use it in GitHub Desktop.
Save briancline/9428940 to your computer and use it in GitHub Desktop.
OSX C++ version of the Python script I normally use under Linux to gather temperature from Go!Temp temperature sensors. Thanks for making everything about USB HID difficult, Apple.
/*
* Ugh. This code is pretty bad. But, it's the only basis I've been able
* to use successfully on OSX, since Apple stupidly auto-claims every single
* HID device on the system, making it unwritable via libusb, etc. unless you
* write an actual driver for it.
*
* So this will have to do.
*/
/*********************************************************************************
Copyright (c) 2010, Vernier Software & Technology
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Vernier Software & Technology nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL VERNIER SOFTWARE & TECHNOLOGY BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <sys/time.h>
#include <Carbon/Carbon.h>
#include "GoIO_DLL_interface.h"
#define MAX_NUM_MEASUREMENTS 100
bool GetAvailableDeviceName(char *deviceName, gtype_int32 nameLength,
gtype_int32 *pVendorId, gtype_int32 *pProductId);
static void OSSleep(unsigned long msToSleep);
int main(int argc, char* argv[])
{
char deviceName[GOIO_MAX_SIZE_DEVICE_NAME];
gtype_int32 usbVendorId;
gtype_int32 usbProductId;
gtype_int32 rawMeasurements[MAX_NUM_MEASUREMENTS];
gtype_real64 volts[MAX_NUM_MEASUREMENTS];
gtype_real64 calbMeasurements[MAX_NUM_MEASUREMENTS];
gtype_int32 numMeasurements, i;
gtype_real64 avgReadingC;
gtype_real64 avgReadingF;
gtype_real64 ts;
GoIO_Init();
bool bFoundDevice = GetAvailableDeviceName(deviceName, GOIO_MAX_SIZE_DEVICE_NAME,
&usbVendorId, &usbProductId);
if (!bFoundDevice) {
printf("No Go devices found.\n");
GoIO_Uninit();
return 1;
}
GOIO_SENSOR_HANDLE hDevice = GoIO_Sensor_Open(deviceName, usbVendorId, usbProductId, 0);
if (hDevice == NULL) {
return 1;
}
GoIO_Sensor_SetMeasurementPeriod(hDevice, 0.040, SKIP_TIMEOUT_MS_DEFAULT); //40ms measurement period
GoIO_Sensor_SendCmdAndGetResponse(hDevice, SKIP_CMD_ID_START_MEASUREMENTS,
NULL, 0, NULL, NULL, SKIP_TIMEOUT_MS_DEFAULT);
do {
OSSleep(250);
numMeasurements = GoIO_Sensor_ReadRawMeasurements(hDevice, rawMeasurements,
MAX_NUM_MEASUREMENTS);
} while (numMeasurements < 1);
struct timeval tv;
gettimeofday(&tv, NULL);
ts = tv.tv_sec + ((gtype_real64)tv.tv_usec / 1000000.0);
avgReadingC = 0.0;
for (i = 0; i < numMeasurements; i++) {
volts[i] = GoIO_Sensor_ConvertToVoltage(hDevice, rawMeasurements[i]);
calbMeasurements[i] = GoIO_Sensor_CalibrateData(hDevice, volts[i]);
avgReadingC += calbMeasurements[i];
}
if (numMeasurements > 1) {
avgReadingC = avgReadingC / numMeasurements;
}
avgReadingF = (9.0 / 5.0) * avgReadingC + 32.0;
printf("%f|%f|%f\n", ts, avgReadingC, avgReadingF);
GoIO_Sensor_Close(hDevice);
GoIO_Uninit();
return 0;
}
bool GetAvailableDeviceName(char *deviceName, gtype_int32 nameLength,
gtype_int32 *pVendorId, gtype_int32 *pProductId)
{
bool bFoundDevice = false;
int numDevices = GoIO_UpdateListOfAvailableDevices(VERNIER_DEFAULT_VENDOR_ID,
USB_DIRECT_TEMP_DEFAULT_PRODUCT_ID);
deviceName[0] = 0;
if (numDevices > 0)
{
GoIO_GetNthAvailableDeviceName(deviceName, nameLength,
VERNIER_DEFAULT_VENDOR_ID,
USB_DIRECT_TEMP_DEFAULT_PRODUCT_ID,
0);
*pVendorId = VERNIER_DEFAULT_VENDOR_ID;
*pProductId = USB_DIRECT_TEMP_DEFAULT_PRODUCT_ID;
bFoundDevice = true;
}
return bFoundDevice;
}
void OSSleep(
unsigned long msToSleep)//milliseconds
{
/*
* Vernier apparently hasn't built this SDK sample in a while; the calls
* below have been deprecated since 10.8 came out. XCode will very nicely
* inform you during build...
*/
AbsoluteTime absTime = ::AddDurationToAbsolute(msToSleep * durationMillisecond, ::UpTime());
::MPDelayUntil(&absTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment