forked from zapmaker/OptiMouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptiMouse.cpp
125 lines (99 loc) · 3.39 KB
/
OptiMouse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
OptiMouse.cpp - Part ofoptical mouse sensor library for Arduino
Copyright (c) 2008 Martijn The. All right reserved.
http://www.martijnthe.nl/
Conversion to Arduino 1.x by zapmaker (zapmaker.org)
Based on sketches by Benoît Rousseau.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
* Includes
******************************************************************************/
#include "OptiMouse.h"
/******************************************************************************
* Definitions
******************************************************************************/
/******************************************************************************
* Constructors
******************************************************************************/
OptiMouse::OptiMouse(uint8_t sclkPin, uint8_t sdioPin)
{
_sclkPin = sclkPin;
_sdioPin = sdioPin;
pinMode (_sclkPin, OUTPUT);
pinMode (_sdioPin, INPUT);
}
/******************************************************************************
* User API
******************************************************************************/
void OptiMouse::begin(void)
{
// Re-sync (see datasheet §5.4):
// Toggle the SLCK line from high to low to high....
digitalWrite(_sclkPin, HIGH);
delayMicroseconds(5);
digitalWrite(_sclkPin, LOW);
delayMicroseconds(1);
digitalWrite(_sclkPin, HIGH);
// Wait at least tSIWTT (0.9 second?) for the
// OptiMouse serial transaction timer to time out:
delay(1000);
}
// Private Methods /////////////////////////////////////////////////////////////
uint8_t OptiMouse::readRegister(uint8_t address)
{
int i = 7;
uint8_t r = 0;
// Write the address of the register we want to read:
pinMode (_sdioPin, OUTPUT);
for (; i>=0; i--)
{
digitalWrite (_sclkPin, LOW);
digitalWrite (_sdioPin, address & (1 << i));
digitalWrite (_sclkPin, HIGH);
}
// Switch data line from OUTPUT to INPUT
pinMode (_sdioPin, INPUT);
// Wait a bit...
delayMicroseconds(100);
// Fetch the data!
for (i=7; i>=0; i--)
{
digitalWrite (_sclkPin, LOW);
digitalWrite (_sclkPin, HIGH);
r |= (digitalRead (_sdioPin) << i);
}
delayMicroseconds(100);
return r;
}
void OptiMouse::writeRegister(uint8_t address, uint8_t data)
{
int i = 7;
// Set MSB high, to indicate write operation:
address |= 0x80;
// Write the address:
pinMode (_sdioPin, OUTPUT);
for (; i>=0; i--)
{
digitalWrite (_sclkPin, LOW);
digitalWrite (_sdioPin, address & (1 << i));
digitalWrite (_sclkPin, HIGH);
}
// Write the data:
for (i=7; i>=0; i--)
{
digitalWrite (_sclkPin, LOW);
digitalWrite (_sdioPin, data & (1 << i));
digitalWrite (_sclkPin, HIGH);
}
}