Skip to content

Commit

Permalink
added initial input code
Browse files Browse the repository at this point in the history
  • Loading branch information
damian authored and damian committed Jul 14, 2011
1 parent 3824f24 commit 238527e
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 2 deletions.
177 changes: 177 additions & 0 deletions src/WatterottInput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* WatterottInput.cpp
* wind
*
* Created by damian on 14/07/11.
* Copyright 2011 frey damian@frey.co.nz. All rights reserved.
*
*/

#include "WatterottInput.h"
#include "ofMain.h"
#include <linux/spi/spidev.h>

static const uint8_t BITS_PER_WORD = 8;

/// open ioctl to device (eg /dev/spidev3.1), with given mode
/// (eg SPI_CPHA | SPI_CPOL); communicate at given speed (bps)
int WatterottInput::setup( const char* device, uint8_t _mode, uint32_t _speed )
{
speed = _speed;

int ret = 0;
mode = _mode;
uint8_t bits = BITS_PER_WORD;

fd = open(device, O_RDWR);
if (fd < 0)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't open device");
return false;
}

/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't set spi mode");
return false;
}

ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't get spi mode");
return false;
}

/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't set bits per word");
return false;
}

ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't get bits per word");
return false;
}

/*
* max speed hz
*/
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't set max speed hz");
return false;
}

ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
{
ofLog(OF_LOG_ERROR, "WatterottInput: can't get max speed hz");
return false;
}

ofLog(OF_LOG_VERBOSE, "spi mode: %d", mode);
ofLog(OF_LOG_VERBOSE, "bits per word: %d", bits);
ofLog(OF_LOG_VERBOSE, "max speed: %d Hz (%d KHz)", speed, speed/1000);

return ret;
}



#define CMD_START (0x80)
#define CMD_12BIT (0x00)
#define CMD_8BIT (0x08)
#define CMD_DIFF (0x00)
#define CMD_SINGLE (0x04)
#define CMD_X_POS (0x10)
#define CMD_Z1_POS (0x30)
#define CMD_Z2_POS (0x40)
#define CMD_Y_POS (0x50)
#define CMD_PWD (0x00)
#define CMD_ALWAYSON (0x03)


/// read pressure and, if pressure > thresh, read x/y,
void WatterottInput::update()
{
struct spi_ioc_transfer tr[4] = { 0, 0, 0, 0 };
tr[0].speed_hz = speed<10000000?speed:10000; // restrict to 10Mhz
tr[0].bits_per_word = BITS_PER_WORD;
tr[0].cs_change = 0;
tr[0].len = 1;

tr[1] = tr[0];
tr[2] = tr[0];
tr[3] = tr[0];

uint8_t tx[2];
uint8_t rx[2];

tx[0] = ( CMD_START | CMD_8BIT | CMD_DIFF | CMD_Z1_POS );
tx[1] = ( CMD_START | CMD_8BIT | CMD_DIFF | CMD_Z2_POS );
tr[0].tx_buf = (unsigned long)&tx[0];
tr[0].rx_buf = 0;
tr[1].tx_buf = 0;
tr[1].rx_buf = (unsigned long)&rx[0];
tr[2].tx_buf = (unsigned long)&tx[1];
tr[2].rx_buf = 0;
tr[3].tx_buf = 0;
tr[3].rx_buf = (unsigned long)&rx[1];
tr[3].cs_change = 1;

int ret = ioctl( fd, SPI_IOC_MESSAGE(4), tr );

pressure = rx[0] + (127-rx[1]);
printf("read: %i -> pressure %i \n", ret, pressure );

// if we have pressure, read x and y
if ( isDown() )
{
x=0;
y=0;
for( int i=2; i!=0; i--) //2 samples
{
unsigned char rx_pos[2][2];
// get X data
tx[0] = (CMD_START | CMD_12BIT | CMD_DIFF | CMD_X_POS);
tr[0].tx_buf = (unsigned long)&tx[0];
tr[0].rx_buf = 0;
tr[1].tx_buf = 0;
tr[1].rx_buf = (unsigned long)&rx_pos[0][0];
tr[1].len = 2;

// get Y data
tx[1] = (CMD_START | CMD_12BIT | CMD_DIFF | CMD_Y_POS);
tr[2].tx_buf = (unsigned long)&tx[1];
tr[2].rx_buf = 0;
tr[3].tx_buf = 0;
tr[3].rx_buf = (unsigned long)&rx_pos[1][0];
tr[3].len = 2;

x += 1023-((rx_pos[0][0]<<3)|(rx_pos[0][1]>>5));
y += 1023-((rx_pos[1][0]<<3)|(rx_pos[1][1]>>5));

ioctl( fd, SPI_IOC_MESSAGE(4), tr );
}
x >>= 1; //x/2
y >>= 1; //y/2

printf(" got x %4i y %4i", x, y );

}

}


37 changes: 37 additions & 0 deletions src/WatterottInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* WatterottInput.h
* wind
*
* Created by damian on 14/07/11.
* Copyright 2011 frey damian@frey.co.nz. All rights reserved.
*
*/

#pragma once

class WatterottInput
{
public:
WatterottInput() { pressure = 0; x=0; y=0; }

/// open ioctl to device (eg /dev/spidev3.1), with given mode
/// (eg SPI_CPHA | SPI_CPOL); communicate at given speed (bps)
int setup( const char* device, uint8_t mode=0, uint32_t speed=500000 );

/// read pressure and, if pressure > thresh, read x/y,
void update();

bool isDown() { return pressure > 10; }
int getX() { return x; }
int getY() { return y; }

private:

int pressure;
int x, y;
int fd;

uint32_t speed;
uint8_t mode;

};
2 changes: 0 additions & 2 deletions src/WatterottScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ uint16_t WatterottScreen::rgb565( float r, float g, float b )

static uint8_t mode;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay = 0;

const static uint8_t LCD_DATA = 0x72;
const static uint8_t LCD_REGISTER = 0x70;
Expand Down
2 changes: 2 additions & 0 deletions src/Wind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ void Wind::update( unsigned char* pixels, int width, int height )
grayDiffSmall.getWidth(), grayDiffSmall.getHeight(),
grayDiffSmall.getPixels() );

input.update();

PROFILE_SECTION_POP();
#endif
}
Expand Down
8 changes: 8 additions & 0 deletions src/Wind.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
#include "ofxPd.h"
#include "ofxXmlSettings.h"

#include "Constants.h"

#ifdef SCREEN
#include "WatterottScreen.h"
#include "WatterottInput.h"
#endif


class Wind : public GuiListener
{
Expand Down Expand Up @@ -90,6 +97,7 @@ class Wind : public GuiListener

#ifdef SCREEN
WatterottScreen screen;
WatterottInput input;
#endif

typedef enum { SI_NONE, SI_FOCUS, SI_DIFF, SI_GRAY_CONTRASTED } ShowingImage;
Expand Down
6 changes: 6 additions & 0 deletions wind.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
2D87F61613CDE1DA00C414EC /* ofAppNoWindow.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D87F61413CDE1DA00C414EC /* ofAppNoWindow.cpp */; };
2D87F7B113CE012A00C414EC /* WatterottScreen.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D87F7AF13CE012A00C414EC /* WatterottScreen.cpp */; };
2D87F89113CEDA1B00C414EC /* Wind.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D87F89013CEDA1B00C414EC /* Wind.cpp */; };
2D87F99813CF4F3F00C414EC /* WatterottInput.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D87F99713CF4F3F00C414EC /* WatterottInput.cpp */; };
2DA682EC139E935A00278E68 /* opencv.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2DA68044139E935600278E68 /* opencv.a */; };
2DA68303139E935A00278E68 /* ofxCvColorImage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2DA68060139E935700278E68 /* ofxCvColorImage.cpp */; };
2DA68304139E935A00278E68 /* ofxCvContourFinder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2DA68063139E935700278E68 /* ofxCvContourFinder.cpp */; };
Expand Down Expand Up @@ -160,6 +161,8 @@
2D87F88F13CEDA1B00C414EC /* Wind.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Wind.h; sourceTree = "<group>"; };
2D87F89013CEDA1B00C414EC /* Wind.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Wind.cpp; sourceTree = "<group>"; };
2D87F89813CEDE5D00C414EC /* Constants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = "<group>"; };
2D87F99613CF4F3F00C414EC /* WatterottInput.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WatterottInput.h; sourceTree = "<group>"; };
2D87F99713CF4F3F00C414EC /* WatterottInput.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WatterottInput.cpp; sourceTree = "<group>"; };
2DA67FA3139E935600278E68 /* cv.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cv.h; sourceTree = "<group>"; };
2DA67FA4139E935600278E68 /* cv.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = cv.hpp; sourceTree = "<group>"; };
2DA67FA5139E935600278E68 /* cvaux.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cvaux.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -775,6 +778,8 @@
E4B69E1F0A3A1BDC003C02F2 /* testApp.h */,
2D87F5CC13CDDC1D00C414EC /* Gui.cpp */,
2D87F58213CDD4B100C414EC /* Gui.h */,
2D87F99613CF4F3F00C414EC /* WatterottInput.h */,
2D87F99713CF4F3F00C414EC /* WatterottInput.cpp */,
);
path = src;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -968,6 +973,7 @@
2D87F61613CDE1DA00C414EC /* ofAppNoWindow.cpp in Sources */,
2D87F7B113CE012A00C414EC /* WatterottScreen.cpp in Sources */,
2D87F89113CEDA1B00C414EC /* Wind.cpp in Sources */,
2D87F99813CF4F3F00C414EC /* WatterottInput.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 238527e

Please sign in to comment.