Subversion Repositories FlightCtrl

Rev

Rev 1477 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1459 acid 1
 
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include <avr/io.h>
6
#include <avr/interrupt.h>
7
#include "servoboard.h"
8
#include "uart.h"
9
 
10
uint8_t rx_buffer[RX_BUFFER_SZ];
11
//uint8_t tx_buffer[TX_BUFFER_SZ];
12
uint8_t rx_pos, tx_pos;
1475 acid 13
uint8_t current_servo = 1;
1459 acid 14
 
15
void uart_init() {
16
 
17
        rx_pos = 0;
18
        tx_pos = 0;
19
 
20
        UCR=(1 << TXEN) | (1 << RXEN);
21
        USR   |= (1<<U2X);
22
        UCSRB |= (1<<RXCIE);
23
//      UCSRB |= (1<<TXCIE);
24
 
25
        UBRR = (F_CPU / (BAUD_RATE * 8L) - 1);
26
 
27
        fdevopen(uart_putchar, NULL);
28
 
29
        printf("servoboard %s\n> ", VERSION);
30
 
31
 
32
}
33
 
1475 acid 34
void display_cur_servo(uint8_t i) {
35
        printf("SERVO %d NEUTRAL %d LIMIT %d-%d\n", i + 1, pwm_neutral_position[i], (pwm_limit[i] & 0xff), (pwm_limit[i] >> 8));
36
}
37
 
38
 
1459 acid 39
ISR(USART_RXC_vect) {
40
 
1475 acid 41
        uint8_t i = UDR;
42
 
1459 acid 43
        if (rx_pos < RX_BUFFER_SZ) {
44
 
1475 acid 45
                if (i == '+' || i == '-') {
46
                        if (i == '+') {
47
                                if (pwm_neutral_position[current_servo] < 255) {
48
                                        pwm_neutral_position[current_servo]++;
49
                                }
50
                        } else if (i == '-') {
51
                                if (pwm_neutral_position[current_servo] > 0) {
52
                                        pwm_neutral_position[current_servo]--;
53
                                }
1461 acid 54
                        }
1475 acid 55
                        display_cur_servo(current_servo);
1461 acid 56
                        return;
57
                }
58
 
1459 acid 59
                uart_putchar(i);
60
 
61
                if (i == '\r') {
62
 
63
                        uart_putchar('\n');
64
 
65
                        rx_buffer[rx_pos] = 0;
66
                        rx_pos = 0;
67
 
68
                        if (!strcasecmp(rx_buffer, "HELP")) {
1475 acid 69
                                printf("DISPLAY         Display servo settings\n"
70
                                           "SET s=n[,l,u]   Set servo settings\n"
71
                                           "                   s = servo number (1-6)\n"
72
                                           "                   n = neutral position (0-255)\n"
73
                                           "                   l = lower limit (0-255)\n"
74
                                           "                   u = upper limit (0-255)\n"
75
                                           "CUR n           Set current servo. + = increase, - = decrease value\n"
76
                                           "SETLL           Set lower limit to servo position\n"
77
                                           "SETUL           Set upper limit\n"
78
                                           "LOAD            Load settings from eeprom\n"
79
                                           "SAVE            Write settings to eeprom\n");
1459 acid 80
                        } else if (!strcasecmp(rx_buffer, "DISPLAY")) {
81
                                for(i = 0; i < 6; i++) {
1475 acid 82
                                        display_cur_servo(i);
1459 acid 83
                                }
1475 acid 84
#if DEBUG_SIGNAL
85
                        } else if (!strcasecmp(rx_buffer, "TV")) {
86
                                display_values = !display_values;
87
                                printf("\n");
88
#endif
1459 acid 89
                        } else if (!strcasecmp(rx_buffer, "LOAD")) {
90
                                eeprom_init();
91
                        } else if (!strcasecmp(rx_buffer, "SAVE")) {
92
                                eeprom_write();
1461 acid 93
                        } else if (!strncasecmp(rx_buffer, "CUR ", 4)) {
94
                                char *s, *t;
95
                                s = strtok_r(rx_buffer, " ", &t);
96
                                if (s) {
1475 acid 97
                                        s = strtok_r(NULL, ",", &t);
1461 acid 98
                                        if (s) {
1475 acid 99
                                                i = atoi(s);
1461 acid 100
                                                if (i >= 1 && i <= 6) {
1475 acid 101
                                                        current_servo = i - 1;
1461 acid 102
                                                        printf("CURRENT SERVO %d\n", i);
103
                                                } else {
104
                                                        printf("Invalid servo\n");
105
                                                }
106
                                        }
107
                                }
1475 acid 108
                        } else if (!strcasecmp(rx_buffer, "SETLL")) {
109
                                uint8_t l, h;
110
                                l = pwm_neutral_position[current_servo];
111
                                h = (pwm_limit[current_servo] >> 8);
112
                                if (h < l) {
113
                                        h = l;
114
                                }
115
                                pwm_limit[current_servo] = (h << 8) | l;
116
                                display_cur_servo(current_servo);
117
                        } else if (!strcasecmp(rx_buffer, "SETUL")) {
118
                                uint8_t l, h;
119
                                h = pwm_neutral_position[current_servo];
120
                                l = (pwm_limit[current_servo] & 0xff);
121
                                if (l > h) {
122
                                        l = h;
123
                                }
124
                                pwm_limit[current_servo] = (h << 8) | l;
125
                                display_cur_servo(current_servo);
1459 acid 126
                        } else if (!strncasecmp(rx_buffer, "SET ", 4)) {
127
                                char *s, *t;
128
                                uint8_t servo;
129
                                s = strtok_r(rx_buffer, " ", &t);
130
                                if (s) {
131
                                        s = strtok_r(NULL, "=", &t);
132
                                        if (s) {
133
                                                servo = atoi(s);
134
                                                if (servo >= 1 && servo <= 6) {
135
                                                        servo--;
136
                                                        s = strtok_r(NULL, ",", &t);
137
                                                        if (s) {
1477 acid 138
                                                                uint8_t h = 0xff, l = 0;
1459 acid 139
                                                                pwm_neutral_position[servo] = atoi(s);
140
                                                                s = strtok_r(NULL, ",", &t);
141
                                                                if (s) {
142
                                                                        l = atoi(s);
1461 acid 143
                                                                        s = strtok_r(NULL, "", &t);
1459 acid 144
                                                                        if (s) {
145
                                                                                h = atoi(s);
146
                                                                        }
147
                                                                }
148
                                                                pwm_limit[servo] = (h << 8) | l;
1477 acid 149
                                                                set_pwm_active();
1459 acid 150
                                                                i = servo;
1460 acid 151
                                                                printf("SERVO %d, NEUTRAL %d, LIMIT %d-%d\n",
152
                                                                        (i + 1),
1459 acid 153
                                                                        (pwm_neutral_position[i]),
1460 acid 154
                                                                        (pwm_limit[i] & 0xff),
1459 acid 155
                                                                        (pwm_limit[i] >> 8)
156
                                                                );
157
                                                        }
158
                                                } else {
159
                                                        printf("Invalid servo\n");
160
                                                }
161
                                        }
162
                                }
163
                        } else {
164
                                printf("Invalid command, type HELP\n");
165
                        }
166
 
167
                        uart_putchar('>');
168
                        uart_putchar(' ');
169
 
170
                } else {
171
 
172
                        rx_buffer[rx_pos++] = i;
173
 
174
                }
175
 
176
        } else {
177
 
178
                printf("Receive buffer full.\n");
1475 acid 179
                rx_pos = 0;
1459 acid 180
 
181
        }
1478 acid 182
 
1459 acid 183
};
184
 
185
/*
186
ISR(USART_TXC_vect) {
187
}*/
188
 
189
int uart_putchar (char c) {
190
        if (c == '\n') {
191
                uart_putchar('\r');
192
        }
193
        loop_until_bit_is_set(USR, UDRE);
194
        UDR = c;
195
        return 0;
196
}
197