@@ -114,24 +114,23 @@ ISR(TIMER0_COMPA_vect)
114114{
115115 // Process X output
116116 if (mouseDistanceX > 0 ) {
117+ // Change phase and range check
118+ if (mouseDirectionX == 0 ) {
119+ mouseEncoderPhaseX -- ;
120+ if (mouseEncoderPhaseX < 0 ) mouseEncoderPhaseX = 3 ;
121+ } else {
122+ mouseEncoderPhaseX ++ ;
123+ if (mouseEncoderPhaseX > 3 ) mouseEncoderPhaseX = 0 ;
124+ }
125+
117126 // Set the output pins according to the current phase
118127 if (mouseEncoderPhaseX == 0 ) X1_PORT |= X1 ; // Set X1 to 1
119128 if (mouseEncoderPhaseX == 1 ) X2_PORT |= X2 ; // Set X2 to 1
120129 if (mouseEncoderPhaseX == 2 ) X1_PORT &= ~X1 ; // Set X1 to 0
121130 if (mouseEncoderPhaseX == 3 ) X2_PORT &= ~X2 ; // Set X2 to 0
122-
123- // Change phase
124- if (mouseDirectionX == 0 ) mouseEncoderPhaseX -- ; else mouseEncoderPhaseX ++ ;
125131
126132 // Decrement the distance left to move
127133 mouseDistanceX -- ;
128-
129- // Range check the phase
130- if ((mouseDirectionX == 1 ) && (mouseEncoderPhaseX > 3 )) mouseEncoderPhaseX = 0 ;
131- if ((mouseDirectionX == 0 ) && (mouseEncoderPhaseX < 0 )) mouseEncoderPhaseX = 3 ;
132- } else {
133- // Reset the phase if the mouse isn't moving
134- mouseEncoderPhaseX = 0 ;
135134 }
136135
137136 // Set the timer top value for the next interrupt
@@ -143,24 +142,23 @@ ISR(TIMER2_COMPA_vect)
143142{
144143 // Process Y output
145144 if (mouseDistanceY > 0 ) {
145+ // Change phase and range check
146+ if (mouseDirectionY == 0 ) {
147+ mouseEncoderPhaseY -- ;
148+ if (mouseEncoderPhaseY < 0 ) mouseEncoderPhaseY = 3 ;
149+ } else {
150+ mouseEncoderPhaseY ++ ;
151+ if (mouseEncoderPhaseY > 3 ) mouseEncoderPhaseY = 0 ;
152+ }
153+
146154 // Set the output pins according to the current phase
147155 if (mouseEncoderPhaseY == 3 ) Y1_PORT &= ~Y1 ; // Set Y1 to 0
148156 if (mouseEncoderPhaseY == 2 ) Y2_PORT &= ~Y2 ; // Set Y2 to 0
149157 if (mouseEncoderPhaseY == 1 ) Y1_PORT |= Y1 ; // Set Y1 to 1
150158 if (mouseEncoderPhaseY == 0 ) Y2_PORT |= Y2 ; // Set Y2 to 1
151159
152- // Change phase
153- if (mouseDirectionY == 0 ) mouseEncoderPhaseY -- ; else mouseEncoderPhaseY ++ ;
154-
155160 // Decrement the distance left to move
156161 mouseDistanceY -- ;
157-
158- // Range check the phase
159- if ((mouseDirectionY == 1 ) && (mouseEncoderPhaseY > 3 )) mouseEncoderPhaseY = 0 ;
160- if ((mouseDirectionY == 0 ) && (mouseEncoderPhaseY < 0 )) mouseEncoderPhaseY = 3 ;
161- } else {
162- // Reset the phase if the mouse isn't moving
163- mouseEncoderPhaseY = 0 ;
164162 }
165163
166164 // Set the timer top value for the next interrupt
@@ -429,7 +427,7 @@ void processMouse(void)
429427 } else {
430428 // Set to 0V
431429 RB_DDR |= RB ; // 1 = output
432- RB_PORT &= RB ; // Button low
430+ RB_PORT &= ~ RB ; // Button low
433431 }
434432
435433 // Clear USB report processing activity on expansion port pin D0
0 commit comments