blob: 30f9d53e99e62f473ea26b5351eadc019b934c39 (
plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#ifndef _MOUSE_SENSOR_H
#define _MOUSE_SENSOR_H
#include "../../stdafx.h"
#include "sensor.h"
class Mouse_Sensor : public Sensor
{
public:
Mouse_Sensor()
{
this->parent = NULL;
this->moduleId = 0;
this->hardwarePort = NULL;
this->hardwareDDR = NULL;
this->hardwarePin = NULL;
this->pinSDA = 0;
this->pinSCK = 0;
this->registerConfig = 0;
this->registerPixelData = 0;
this->registerSqual = 0;
this->registerDeltaX = 0;
this->registerDeltaY = 0;
this->configReset = 0;
this->configAwake = 0;
this->newImage = false;
}
Mouse_Sensor(uint32 sensorId)
{
this->parent = NULL;
this->moduleId = sensorId;
this->newImage = false;
switch(sensorId)
{
case IO_SENSOR_MOUSE_LEFT:
this->hardwarePort = &PORTC;
this->hardwareDDR = &DDRC;
this->hardwarePin = &PINC;
this->pinSDA = (1 << 4);
this->pinSCK = (1 << 6);
this->registerConfig = 0x00;
this->registerPixelData = 0x08;
this->registerSqual = 0x4;
this->registerDeltaX = 0x3;
this->registerDeltaY = 0x2;
this->configReset = 0x80;
this->configAwake = 0x01;
break;
case IO_SENSOR_MOUSE_RIGHT:
this->hardwarePort = &PORTC;
this->hardwareDDR = &DDRC;
this->hardwarePin = &PINC;
this->pinSDA = (1 << 5);
this->pinSCK = (1 << 7);
this->registerConfig = 0x00;
this->registerPixelData = 0x08;
this->registerSqual = 0x4;
this->registerDeltaX = 0x3;
this->registerDeltaY = 0x2;
this->configReset = 0x80;
this->configAwake = 0x01;
break;
default:
this->hardwarePort = NULL;
this->hardwareDDR = NULL;
this->hardwarePin = NULL;
this->pinSDA = 0;
this->pinSCK = 0;
this->registerConfig = 0;
this->registerPixelData = 0;
this->registerSqual = 0;
this->registerDeltaX = 0;
this->registerDeltaY = 0;
this->configReset = 0;
this->configAwake = 0;
break;
}
*hardwareDDR |= pinSCK;
*hardwarePort &= ~pinSCK;
Write(registerConfig, configReset);
Write(registerConfig, configAwake);
}
protected:
//Hardware
volatile uint8* hardwarePort;
volatile uint8* hardwareDDR;
volatile uint8* hardwarePin;
uint8 pinSDA;
uint8 pinSCK;
bool newImage;
//Registers and Settings
uint8 registerConfig;
uint8 registerPixelData;
uint8 registerSqual;
uint8 registerDeltaX;
uint8 registerDeltaY;
uint8 configReset;
uint8 configAwake;
public:
void WriteByte(uint8 newByte)
{
*hardwareDDR |= pinSDA;//Set SDA output
for(uint8 i = 0; i < 8; i++)
{
*hardwarePort &= ~pinSCK;//prepare SCK
//write data
*hardwarePort = (*hardwarePort & (~(*hardwarePin))) |
((newByte >> 7) * pinSDA);
newByte = newByte << 1;//prepare next byte
asm volatile("nop");
*hardwarePort |= pinSCK;
}
}
void Write(int8 adr, uint8 data)
{
WriteByte(adr | 0x80);
WriteByte(data);
usleep(100);
}
uint8 ReadByte()
{
uint8 data=0;
*hardwareDDR &= ~pinSDA;//Set SDA input
for(uint8 i = 0; i < 8; i++)
{
*hardwarePort &= ~pinSCK;//Prepare data
data = data << 1;
asm volatile("nop");
*hardwarePort |= pinSCK;//Prepare for reading
data |= (*hardwarePin & pinSDA) / pinSDA;
}
return data;
}
uint8 Read(uint8 adr)
{
WriteByte(adr);
usleep(100);
return ReadByte();
}
void ImagePrepare()
{
Write(registerConfig, configAwake);
Write(registerPixelData, 0x00);
newImage = true;
}
uint8 ImageRead()
{
uint8 pixel = Read(registerPixelData);
if(newImage)
{
while (!(pixel & 0x80))//0x80 indicates first pixel
{
pixel=Read(registerPixelData);
}
newImage = false;
}
return pixel;
}
uint8 GetSqual()
{
return Read(registerSqual);
}
int8 GetXMovement()
{
return (int8)(Read(registerDeltaX));
}
int8 GetYMovement()
{
return (int8)(Read(registerDeltaY));
}
};
#endif
|