Synergia AVR 0.1
|
00001 /* 00002 Copyright (c) 2011 Grzegorz Hajdukiewicz 00003 00004 Permission is hereby granted, free of charge, to any person obtaining 00005 a copy of this software and associated documentation files (the 00006 "Software"), to deal in the Software without restriction, including 00007 without limitation the rights to use, copy, modify, merge, publish, 00008 distribute, sublicense, and/or sell copies of the Software, and to 00009 permit persons to whom the Software is furnished to do so, subject to 00010 the following conditions: 00011 00012 The above copyright notice and this permission notice shall be 00013 included in all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00018 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00019 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00020 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00021 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00022 */ 00023 00024 #include <avr/io.h> 00025 #include <util/delay.h> 00026 #include "mouse.h" 00027 00028 void MOUSE::sck(char state){ 00029 if (state == 1) M_PORT |= (1 << SCK); 00030 else M_PORT &= ~(1<<SCK); 00031 } 00032 00033 char MOUSE::read(char addr){ 00034 char value=0; 00035 char i; 00036 00037 M_DDR |= (1 << SDIO); // set both SDIO & SCK as output 00038 00039 for (i=7; i>=0; i--) 00040 { 00041 sck(0); 00042 00043 //send i(th) register adress bit 00044 if ((addr & (1<<i)) != 0) M_PORT |= (1<<SDIO); 00045 else M_PORT &= ~(1<<SDIO); 00046 00047 sck(1); 00048 } 00049 00050 _delay_us(100); //wait for mouse to read data 00051 M_DDR &= ~(1 << SDIO); //set SDIO as input 00052 00053 //receive data 00054 for (i=7; i>=0; i--) 00055 { 00056 sck(0); 00057 value |= (M_PIN & (1<<SDIO)) << i; 00058 sck(1); 00059 } 00060 _delay_us(100); 00061 return value; 00062 } 00063 00064 void MOUSE::write(char addr, char data){ 00065 int i; 00066 00067 addr |= (1<<7); //to enable write mode 00068 00069 M_DDR |= (1 << SDIO); // set both SDIO & SCK as output 00070 00071 for (i=7; i>=0; i--) 00072 { 00073 sck(0); 00074 00075 //send i(th) register adress bit 00076 if ((addr & (1<<i)) != 0) M_PORT |= (1<<SDIO); 00077 else M_PORT &= ~(1<<SDIO); 00078 00079 sck(1); 00080 } 00081 00082 for (i=7; i>=0; i--) 00083 { 00084 sck(0); 00085 00086 //send i(th) register adress bit 00087 if ((data & (1<<i)) != 0) M_PORT |= (1<<SDIO); 00088 else M_PORT &= ~(1<<SDIO); 00089 00090 sck(1); 00091 } 00092 } 00093 00094 MOUSE & MOUSE::operator>>(PosDelta &delta){ 00095 delta.x = read(0x03); 00096 delta.y = read(0x02); 00097 return *this; 00098 } 00099 00100 MOUSE & MOUSE::operator>>(MouseData &data){ 00101 data.qual = read(0x04); 00102 return *this; 00103 } 00104 00105 MOUSE::MOUSE(){ 00106 char a; 00107 00108 M_DDR |= (1 << SCK); 00109 M_PORT |= (1 << SCK); 00110 _delay_us(5); 00111 M_PORT &= ~(1 << SCK); 00112 _delay_us(1); 00113 M_PORT |= (1 << SCK); 00114 _delay_ms(1000); 00115 a = read(0x00); 00116 a &= ~0x01; 00117 write(0x00, a); 00118 } 00119