Rev 1459 | 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; |
||
13 | |||
14 | void uart_init() { |
||
15 | |||
16 | rx_pos = 0; |
||
17 | tx_pos = 0; |
||
18 | |||
19 | UCR=(1 << TXEN) | (1 << RXEN); |
||
20 | USR |= (1<<U2X); |
||
21 | UCSRB |= (1<<RXCIE); |
||
22 | // UCSRB |= (1<<TXCIE); |
||
23 | |||
24 | UBRR = (F_CPU / (BAUD_RATE * 8L) - 1); |
||
25 | |||
26 | fdevopen(uart_putchar, NULL); |
||
27 | |||
28 | printf("servoboard %s\n> ", VERSION); |
||
29 | |||
30 | |||
31 | } |
||
32 | |||
33 | ISR(USART_RXC_vect) { |
||
34 | |||
35 | if (rx_pos < RX_BUFFER_SZ) { |
||
36 | |||
37 | uint8_t i = UDR; |
||
1460 | acid | 38 | |
1459 | acid | 39 | uart_putchar(i); |
40 | |||
41 | if (i == '\r') { |
||
42 | |||
43 | uart_putchar('\n'); |
||
44 | |||
45 | rx_buffer[rx_pos] = 0; |
||
46 | rx_pos = 0; |
||
47 | |||
48 | if (!strcasecmp(rx_buffer, "HELP")) { |
||
49 | printf("DISPLAY Display servo settings\n" |
||
50 | "EXPORT Export srvo settings\n" |
||
1460 | acid | 51 | "SET s=n[,l,u] Set servo settings\n" |
1459 | acid | 52 | " s = servo number (1-6)\n" |
53 | " n = neutral position (0-255)\n" |
||
54 | " l = lower limit (0-255)\n" |
||
55 | " u = upper limit (0-255)\n" |
||
56 | "LOAD Load settings from eeprom\n" |
||
57 | "SAVE Write settings to eeprom\n"); |
||
58 | } else if (!strcasecmp(rx_buffer, "DISPLAY")) { |
||
59 | for(i = 0; i < 6; i++) { |
||
1460 | acid | 60 | printf("SERVO %d, NEUTRAL %d, LIMIT %d-%d\n", |
61 | (i + 1), |
||
1459 | acid | 62 | (pwm_neutral_position[i]), |
1460 | acid | 63 | (pwm_limit[i] & 0xff), |
1459 | acid | 64 | (pwm_limit[i] >> 8) |
65 | ); |
||
66 | } |
||
67 | } else if (!strcasecmp(rx_buffer, "EXPORT")) { |
||
68 | for(i = 0; i < 6; i++) { |
||
1460 | acid | 69 | printf("SET %d=%d,%d,%d\n", |
70 | (i + 1), |
||
1459 | acid | 71 | (pwm_neutral_position[i]), |
1460 | acid | 72 | (pwm_limit[i] & 0xff), |
1459 | acid | 73 | (pwm_limit[i] >> 8) |
74 | ); |
||
75 | } |
||
76 | } else if (!strcasecmp(rx_buffer, "LOAD")) { |
||
77 | eeprom_init(); |
||
78 | } else if (!strcasecmp(rx_buffer, "SAVE")) { |
||
79 | eeprom_write(); |
||
80 | // } else if (!strncasecmp(rx_buffer, "INC ", 4)) { |
||
81 | // } else if (!strncasecmp(rx_buffer, "DEC ", 4)) { |
||
82 | } else if (!strncasecmp(rx_buffer, "SET ", 4)) { |
||
83 | char *s, *t; |
||
84 | uint8_t servo; |
||
85 | s = strtok_r(rx_buffer, " ", &t); |
||
86 | if (s) { |
||
87 | s = strtok_r(NULL, "=", &t); |
||
88 | if (s) { |
||
89 | servo = atoi(s); |
||
90 | if (servo >= 1 && servo <= 6) { |
||
91 | servo--; |
||
92 | s = strtok_r(NULL, ",", &t); |
||
93 | if (s) { |
||
94 | uint8_t h = 0, l = 0; |
||
95 | pwm_neutral_position[servo] = atoi(s); |
||
96 | s = strtok_r(NULL, ",", &t); |
||
97 | if (s) { |
||
98 | l = atoi(s); |
||
99 | s = strtok_r(NULL, ",", &t); |
||
100 | if (s) { |
||
101 | h = atoi(s); |
||
102 | } else { |
||
103 | h = 0xff; |
||
104 | } |
||
105 | } |
||
106 | pwm_limit[servo] = (h << 8) | l; |
||
107 | i = servo; |
||
1460 | acid | 108 | printf("SERVO %d, NEUTRAL %d, LIMIT %d-%d\n", |
109 | (i + 1), |
||
1459 | acid | 110 | (pwm_neutral_position[i]), |
1460 | acid | 111 | (pwm_limit[i] & 0xff), |
1459 | acid | 112 | (pwm_limit[i] >> 8) |
113 | ); |
||
114 | } |
||
115 | } else { |
||
116 | printf("Invalid servo\n"); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } else { |
||
121 | printf("Invalid command, type HELP\n"); |
||
122 | } |
||
123 | |||
124 | uart_putchar('>'); |
||
125 | uart_putchar(' '); |
||
126 | |||
127 | } else { |
||
128 | |||
129 | rx_buffer[rx_pos++] = i; |
||
130 | |||
131 | } |
||
132 | |||
133 | } else { |
||
134 | |||
135 | printf("Receive buffer full.\n"); |
||
136 | |||
137 | } |
||
138 | |||
139 | }; |
||
140 | |||
141 | /* |
||
142 | ISR(USART_TXC_vect) { |
||
143 | }*/ |
||
144 | |||
145 | int uart_putchar (char c) { |
||
146 | if (c == '\n') { |
||
147 | uart_putchar('\r'); |
||
148 | } |
||
149 | loop_until_bit_is_set(USR, UDRE); |
||
150 | UDR = c; |
||
151 | return 0; |
||
152 | } |
||
153 |