Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed_adns9800 by
main.cpp
- Committer:
- gigabite
- Date:
- 2017-01-09
- Revision:
- 1:b6f857ea3ce2
- Parent:
- 0:7a6a7374b6d7
- Child:
- 2:899aa692d83f
File content as of revision 1:b6f857ea3ce2:
#include "mbed.h"
#include "adns9800.h"
InterruptIn pulse(p8); // connects to FPGA and listens for start recording pulse
DigitalOut gndPin(p7);
DigitalOut ledPin(LED1);
ADNS9800 adns(p11,p12,p13,p10);
Serial pc(USBTX, USBRX);
int dx, dy, movementFlag, recFlag;
Ticker tmr;
uint32_t time_start, time_now, time_elapsed;
void displayRegisters()
{
int oreg[4] = {0x00,0x3F,0x2A,0x02};
char* oregname[] = {"Product_ID:","Inverse_Product_ID:","SROM_Version:","Motion:"};
uint8_t regres;
int rctr;
for(rctr=0; rctr<4; rctr++){
pc.printf("---\r\n");
regres = adns.read_reg(oreg[rctr]);
pc.printf(oregname[rctr]);
pc.printf("%02x\r\n", regres);
wait_ms(1);
}
}
void read_motion()
{
int motion, obs;
adns.read_burst(&motion, &obs, &dx, &dy);
if(recFlag == 1 && (dx > 0 || dy > 0))
{
time_now = us_ticker_read();
time_elapsed = time_now - time_start;
time_start = time_now;
movementFlag = 1;
}
}
void start_rec()
{
recFlag = 1;
ledPin = 1;
time_start = us_ticker_read();
}
void stop_rec()
{
ledPin = 0;
recFlag = 0;
}
void format_and_send()
{
// follow LSA's packet format
/*
* Data format from microblaze to PC:
* 100000TT_0TTTTTTT_0TTTTTTT_0PXXXXXX_0PYYYYYY
* Where T = delta timestamp, P = polarity, X = delta x, Y = delta y
* Only first byte has MSB set
* timestamp in 100us per increment
* dx = 0(P)XXXXXX where 2nd bit is polarity
*/
uint8_t outPkt[5];
time_elapsed = time_elapsed / 100;
outPkt[0] = 0x84 | ((time_elapsed >> 14) & 0x00000003);
outPkt[1] = ((time_elapsed >> 7) & 0x0000007F);
outPkt[2] = (time_elapsed & 0x0000007F);
if(dx & 0x8000)
{
dx = (dx^0xFFFF) + 1;
outPkt[3] = 0x40 | (dx & 0x3F); // negative
}
else
{
outPkt[3] = dx & 0x3F;
}
if(dy & 0x8000)
{
dy = (dy^0xFFFF) + 1;
outPkt[4] = 0x40 | (dy & 0x3F);
}
else
{
outPkt[4] = dy & 0x3F;
}
// send packet
for (int i=0; i<5; i++)
{
pc.putc(outPkt[i]);
}
}
int main() {
gndPin = 0; // lazy to connect to true ground pin
movementFlag = 0;
recFlag = 0;
pc.baud(115200);
pc.printf("ADNS9800 test\r\n");
adns.initialize();
// display registers
displayRegisters();
pc.printf("Sensor initialized\r\n");
// for sync with FPGA
pulse.rise(&start_rec);
pulse.fall(&stop_rec);
tmr.attach_us(&read_motion, 1000); // timer interrupt to read registers
while(1) {
if(movementFlag == 1)
{
format_and_send();
movementFlag = 0;
}
}
}
