|
一日看网络发现这个,modules:opticalmouse [Small Electronic Thingies for All Kinds of Fun Stuff]
http://wiki.edwindertien.nl/doku.php?id=modules:opticalmouse
// Getting to see what your mouse sees
// grabbing binary image data from the serial port
import processing.serial.*;
Serial port;
int pixelsize=20;
int x_size = 18;
int y_size = 18;
int pict;
String buff = "";
void setup(){
size(pixelsize*x_size,pixelsize*y_size);
println("Available serial ports:");
println(Serial.list());
port = new Serial(this, Serial.list()[0], 38400);
port.buffer(x_size*y_size);
frameRate(20); // delay of 50 ms, 20Hz update
background(255);
}
void draw(){
if (pict>0){ // if there's a picture
for(int y=0;y<y_size;y++){
for (int x=0;x<x_size;x++){
fill(4*(byte)buff.charAt(y*y_size+x));
rect(pixelsize*x,pixelsize*y,pixelsize*x+pixelsize,pixelsize*y+pixelsize);
}
}
}
}
void serialEvent(Serial port) {
buff =port.readString();
pict++;
}
--------------------------------------------------------------------------------------------
// inspired by Martijn The's sketch, adding all functionality of the 2610 chip, skipping
// the whole library-thing
#define SCLK 2 // Serial clock pin on the Arduino
#define SDIO 3 // Serial data (I/O) pin on the Arduino
#define sensorConfig 0x00
#define sensorStatus 0x01
#define Delta_Y 0x02
#define Delta_X 0x03
#define SQUAL 0x04
#define Maximum_pixel 0x05
#define Minimum_pixel 0x06
#define Pixel_Sum 0x07
#define picture 0x08
#define Shutter_MSB 0x09
#define Shutter_LSB 0x0A
unsigned char pixels[325];
void setup(){
Serial.begin(38400);
pinMode (SCLK, OUTPUT);
pinMode (SDIO, INPUT);
reSync();
forceAwake(1); // LED on
}
void loop(){
getPicture(); // take snapshot
sendPicture(); // send the picture
delay(100);
}
void reSync(){
// ReSync (startup) mouse
digitalWrite(SCLK, HIGH);
delayMicroseconds(5);
digitalWrite(SCLK, LOW);
delayMicroseconds(1);
digitalWrite(SCLK, HIGH);
// wait for OptiMouse serial transaction timer to time out:
delay(1000);
}
void getPicture(void){
writeRegister(picture,0); // default write: initiate picture transfer
for (int x=0;x<18;x++){ // 18 rows of pixels
for (int y=17;y>=0;y--){// 18 collumns (reverse order)
unsigned char pix = readRegister(picture); // SPI read operation
int timeout = 12; // Try 12 times max to
while (!(pix & 0x40) && timeout>0){ // check for valid data
pix = readRegister(picture); // read the picture data
timeout--;
}
pixels[x*18+y]=pix&0x3F; // store the data (might still be invalid after 12 tries) // was pix&0x2F
}
}
}
void sendPicture(void){
for(int z=0;z<324;z++) {
Serial.print((unsigned char)(pixels[z]&0x3F)); // send block of 324 bytes //was pixels[z]&0x2F
}
}
void forceAwake(char value){
if (value>0) writeRegister(sensorConfig,0x01);
else writeRegister(sensorConfig,0x00);
}
signed char getDx(void){
return (signed char) readRegister(Delta_X);
}
signed char getDy(void){
return (signed char) readRegister(Delta_Y);
}
signed int getShutter(void){
signed int shutter = readRegister(Shutter_MSB) <<8;
shutter += readRegister(Shutter_LSB); // + LSB
return shutter;
}
uint8_t readRegister(uint8_t address){ // Bitbang SPI read operation
int i = 7;
uint8_t r = 0;
pinMode (SDIO, OUTPUT); // Write the address of the register we want to read:
for (; i>=0; i--) {
digitalWrite (SCLK, LOW);
digitalWrite (SDIO, address & (1 << i));
digitalWrite (SCLK, HIGH);
}
pinMode (SDIO, INPUT); // Switch data line from OUTPUT to INPUT
delayMicroseconds(100); // Wait according to datasheet
for (i=7; i>=0; i--) { // Fetch the data!
digitalWrite (SCLK, LOW);
digitalWrite (SCLK, HIGH);
r |= (digitalRead (SDIO) << i);
}
delayMicroseconds(100);
return r;
}
void writeRegister(uint8_t address, uint8_t data){
int i = 7;
// Set MSB high, to indicate write operation:
address |= 0x80;
// Write the address:
pinMode (SDIO, OUTPUT);
for (; i>=0; i--){
digitalWrite (SCLK, LOW);
digitalWrite (SDIO, address & (1 << i));
digitalWrite (SCLK, HIGH);
}
// Write the data:
for (i=7; i>=0; i--){
digitalWrite (SCLK, LOW);
digitalWrite (SDIO, data & (1 << i));
digitalWrite (SCLK, HIGH);
}
}
----------------------------------------------------------------------------------- |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|