Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
508 kaklik 1
/* 
2
 * Copyright (C) 2004 Darren Hutchinson (dbh@gbdt.com.au)
3
 * 
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU Library General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or (at your
7
 * option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
12
 * License for more details.
13
 * 
14
 * You should have received a copy of the GNU Library General Public License
15
 * along with this program; see the file COPYING.  If not, write to
16
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17
 * MA 02111-1307, USA. 
18
 *
19
 * $Id: paddle.c,v 1.4 2004/03/03 07:37:11 dbh Exp $
20
 */
21
 
22
/* This file handles the interface to the EQ-6/Atlas hand paddle.
23
 *
24
 * This paddle generates a continuous serial stream containing information
25
 * about the state of the paddle. Unlike many simple paddles the EQ-6 has a
26
 * small microprocessor to handle the various functions (debouncing, switch
27
 * refersal, etc)
28
 *
29
 * See the file eq6-serial.txt for further information on the prototcol
30
 * used by the hand paddle.
31
 */
32
#include <avr/io.h>
33
#include <avr/interrupt.h>
34
#include <inttypes.h>
35
 
36
#include "paddle.h"
37
#include "eq6.h"
38
#include "combine.h"
39
 
40
uint8_t  paddleGuideRate = SPEED_1_X;
41
 
42
/* paddleInit() initializes the serial port used for the EQ6/Atlas
43
 * hand paddle. The communication with this port is only in one
44
 * direction (from paddle to mount).
45
 *
46
 * Passed:
47
 *         Nothing
48
 *
49
 * Returns:
50
 *         Nothing.
51
 */
52
void
53
paddleInit(void)
54
{
55
    // The handpaddle is connected to UART port #0 (pin 10)
56
 
57
    // Set serial rate
58
    UBRR0H = (CLK_RATE / PADDLE_RATE / 16 - 1) >> 8;
59
    UBRR0L = (CLK_RATE / PADDLE_RATE / 16 - 1) & 0xff;
60
 
61
    /* Setup registers
62
     * 9 bits, RX enable
63
     */
64
    UCSR0B = 0;            // Disable all interrupts
65
    UCSR0C = _BV(URSEL0) | _BV(UCSZ01) | _BV(UCSZ00);
66
    UCSR0B = _BV(RXCIE0) | _BV(UCSZ02) | _BV(RXEN0);
67
}
68
 
69
/* paddleInt() is called whenever a character is received on USART0.
70
 * These characters are send by the hand paddle
71
 *
72
 * Passed:
73
 *         Nothing
74
 *
75
 * Returns:
76
 *         Nothing
77
 *
78
 * Notes:
79
 *         This function inplemented a state machine to properly
80
 *         synchronize with the incoming character stream.
81
 *
82
 *         Interrupts are disabled during processing
83
 */
84
SIGNAL(SIG_USART0_RECV)
85
{
86
    uint8_t             lowerByte;
87
    uint8_t             b8;
88
 
89
    static void         *pLabel = &&wait_for_sync;
90
 
91
    static uint8_t      raDir = 0;
92
    static uint8_t      raSpeed = 0;
93
    static uint8_t      decDir = 0;
94
 
95
    static uint8_t      oldRaDir = (uint8_t)-1;
96
    static uint8_t      oldRaSpeed = (uint8_t)-1;
97
    static uint8_t      oldDecDir = (uint8_t)-1;
98
    static uint8_t      oldDecSpeed = (uint8_t)-1;
99
 
100
    /* Read the nine bits of transferred data. We don't do this
101
     * during initialization as the order is important!
102
     */
103
    b8 = UCSR0B & _BV(RXB80);
104
    lowerByte = UDR0;
105
 
106
    /* Jump to the code that handles the current reception system. Note that
107
     * this feature is GCC specific.
108
     */
109
    goto *pLabel;
110
 
111
    /* Wait for the RA sync byte
112
     */
113
wait_for_sync:
114
    if ((b8 == 0) && (lowerByte == RA_SYNC))
115
        pLabel = &&get_ra_direction;
116
    return;
117
 
118
    /* Get the RA direction - this will have b8 == 0
119
     */
120
get_ra_direction:
121
    if (b8 == 0)
122
    {
123
        raDir = lowerByte & _BV(RA_DIR_BIT);
124
        pLabel = &&get_ra_speed;
125
    }
126
    else
127
        pLabel = &&wait_for_sync;
128
    return;
129
 
130
    /* Get the RA speed - this will have b8 == 0
131
     */
132
get_ra_speed:
133
    if (b8 == 0)
134
    {
135
        raSpeed = lowerByte & RA_SPEED_MASK;
136
        pLabel = &&get_dec_sync;
137
    }
138
    else
139
        pLabel = &&wait_for_sync;
140
    return;
141
 
142
    /* Get the mystery fourth byte
143
     */
144
get_dec_sync:
145
    if (b8 != 0)
146
    {
147
        pLabel = &&get_dec_direction;
148
    }
149
    else
150
        pLabel = &&wait_for_sync;
151
    return;
152
 
153
    /* Get the DEC direction - this will have b8 != 0
154
     */
155
get_dec_direction:
156
    if (b8 != 0)
157
    {
158
        decDir = lowerByte & _BV(DEC_DIR_BIT);
159
        pLabel = &&get_dec_speed;
160
    }
161
    else
162
        pLabel = &&wait_for_sync;
163
    return;
164
 
165
    /* Get the RA speed - this will have b8 == 0
166
     */
167
get_dec_speed:
168
    // We've got all the words - the next state will always be wait for sync
169
    pLabel = &&wait_for_sync;
170
    lowerByte &= DEC_SPEED_MASK;
171
 
172
    /* If the parity is correct and any of the bytes are different then
173
     * process them
174
     */
175
    if (    b8 != 0
176
            && (        (oldRaDir != raDir) 
177
                        || (oldRaSpeed != raSpeed) 
178
                        || (oldDecDir != decDir) 
179
                        || (oldDecSpeed != lowerByte)))
180
    {
181
        /* Update "old" indications */
182
        oldRaDir = raDir;
183
        oldRaSpeed = raSpeed;
184
        oldDecDir = decDir;
185
        oldDecSpeed = lowerByte;
186
 
187
        /* Process the data from the paddle
188
         * - if the RA speed is one then setup the siderial speed
189
         * - convert the RA speed/direction into a device independent form
190
         * - convert the DEC speed/direction into a device independent form
191
         */
192
        if (raSpeed == RA_SPEED_0)
193
        {
194
            rateInput.paddleRaRate = -paddleGuideRate;
195
 
196
            /* This is a little trick because the "direction" bit doesn't
197
             * work then the speed is zero. Do a pre-invert if the
198
             * siderial rate is for the northern hemisphere
199
             */
200
            if (rateInput.siderialRate == SPEED_SIDERIAL)
201
                rateInput.paddleRaRate = -rateInput.paddleRaRate;
202
        }
203
        else if (raSpeed == RA_SPEED_1)
204
        {
205
            rateInput.paddleRaRate = SPEED_0_X;
206
            rateInput.siderialRate = raDir ? SPEED_SIDERIAL : -SPEED_SIDERIAL;
207
        }
208
        else if (raSpeed == RA_SPEED_2)
209
            rateInput.paddleRaRate = paddleGuideRate;
210
        else if (raSpeed == RA_SPEED_8)
211
            rateInput.paddleRaRate = SPEED_8_X;
212
        else if (raSpeed == RA_SPEED_16)
213
            rateInput.paddleRaRate = SPEED_16_X;
214
 
215
#if 0
216
        /* The direction of the RA keys is reversed when operating in
217
         * the southern hemisphere, so modify them back
218
         */
219
        if ((raDir == 0) ^ (rateInput.siderialRate == SPEED_SIDERIAL))
220
             rateInput.paddleRaRate = -rateInput.paddleRaRate;
221
#else
222
        /* Use the keys as returned by the paddle. These are reversed when
223
         * operating in the southern hemisphere. This ensures that the
224
         * "right" key always increases the RA angle
225
         */
226
        if (raDir == 0)
227
             rateInput.paddleRaRate = -rateInput.paddleRaRate;
228
#endif
229
 
230
        if (lowerByte == DEC_SPEED_0)
231
            rateInput.paddleDecRate = SPEED_0_X;
232
        else if (lowerByte == DEC_SPEED_2)
233
            rateInput.paddleDecRate = paddleGuideRate;
234
        else if (lowerByte == DEC_SPEED_8)
235
            rateInput.paddleDecRate = SPEED_8_X;
236
        else if (lowerByte == DEC_SPEED_16)
237
            rateInput.paddleDecRate = SPEED_16_X;
238
 
239
        if (decDir != 0)
240
            rateInput.paddleDecRate = -rateInput.paddleDecRate;
241
 
242
        /* Ok, all parsed. Force an update of the combined speed
243
         */
244
        updateMountSpeed();
245
    }
246
}