-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_volume_generator.ino
230 lines (190 loc) · 5.75 KB
/
udp_volume_generator.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <SD.h>
#include <SPI.h>
#include <ctype.h>
//#include <EthernetV2_0.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <avr/wdt.h>
/* Rich Moore, Jan 4, 2016
* *
* receives messages via UDP that control the amplitude of a squarewave. Message format is 2 ascii coded hex bytes
* Note: actual resolution is only 12-bits
*
* 0xxx where x is [0-F]
*
* examples:
* 00FF // note this is literal ascii string "00FF"
* 0123 // "0123"
*
*
* if an SD is used, it should contain a file called 'config.txt' with the following format:
*
* MAC address, 6 comma seperated ascii hex bytes
* IP address
* PORT
*
* Example:
* 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
* 192.168.11.21
* 8888
*
*
*/
// defines for the oscillator
#define PERIOD 10000 // 100Hz
#define DAC_MAX_VAL 4095 // DACs are only 6 bits
#define MAX_LINE_LEN 80
// Ed's IP address is '192.168.35.150'
// Enter a MAC address and IP address for your controller below.
//[TODO] read this in from a file on the SD card
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte myIp[4] = {192,168,35,150};
IPAddress ip(myIp[0], myIp[1],myIp[2],myIp[3]);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "ack"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
int packet_bytes;
unsigned int amplitude=DAC_MAX_VAL/4; // lower initial startup volume
int state=LOW;
unsigned int value;
unsigned long t_now=0;
unsigned long t_last=0;
unsigned long t_period=PERIOD;
unsigned long t_half_period=PERIOD/2;
File fd;
char config_filename[] = "config.txt";
char c,buf[MAX_LINE_LEN];
void setup() {
Serial.begin(9600);
// start the Ethernet and UDP:
IPAddress ip(myIp[0], myIp[1], myIp[2], myIp[3]);
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.print("IP address set to ");
Serial.print(Ethernet.localIP());
Serial.print(" Listening on port ");
Serial.println(localPort);
Serial.print("Setting amplitude to 0x");
Serial.println(amplitude,HEX);
dac_write(amplitude);
// set Data direction registers
DDRB = DDRB | B00000001; // LSB of Port B is D08, which is MSB of one DAC
DDRC = DDRC | B00111111; // Port C is normal Analog inputs A0-A5
DDRD = DDRD | B11101100; // Port D has pins 0-4 of one DAC. bits 0,1,4 are reserved for other uses
}
void loop() {
unsigned int msb,lsb;
int packetSize = Udp.parsePacket();
if (packetSize) {
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
msb = fromHex(packetBuffer[0],packetBuffer[1]);
lsb = fromHex(packetBuffer[2],packetBuffer[3]);
amplitude= (msb<<8) + lsb;
Serial.print("Setting amplitude to 0x");
Serial.println(amplitude,HEX);
dac_write(amplitude);
// echo the command sent back to the IP address and port that sent it
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
// toggle the outputs at the defined frequency
t_now=micros(); // get current time in microseconds
// check for rollover, once every 70 min
if (t_now < t_last)
t_last=0;
// have we waited long enough to toggle a 50% square wave
if ((t_now-t_last) >= t_half_period) {
// time to toggle outputs
if (state == LOW) {
// set state to high, set R & L DAC outputs to their respective amplitude values
state = HIGH;
dac_write(amplitude);
} else {
// set state to low, set R & L DAC outputs to zeroes
state = LOW;
dac_write(0);
}
t_last = t_now; // update time of last toggle
}
}
void dac_write(unsigned int dac_val) {
// DAC bit mapping is: MSB[PB0, PD7, PD6, PD5, PD3, PD2,PC5,PC4,PC3,PC2,PC1,PC0]LSB
#ifdef DEBUG
Serial.print("dac_val=0b");
Serial.println(dac_val,BIN);
Serial.println("Before");
Serial.print("PORTB=0x");
Serial.println(PORTB,HEX);
Serial.print("PORTC=0x");
Serial.println(PORTC,HEX);
Serial.print("PORTD=0x");
Serial.println(PORTD,HEX);
#endif //DEBUG
// DAC bits 0-5 are port C
PORTC = (unsigned char)(dac_val & 0x03F);
// DAC bit 11, PB0
if (dac_val & 0x0800)
PORTB = PORTB | 0x01;
else
PORTB = PORTB & ~0x01;
// DAC bit 10, PD7
if (dac_val & 0x0400)
PORTD = PORTD | 0x080;
else
PORTD = PORTD & ~0x080;
// DAC bit 9, PD6
if (dac_val & 0x0200)
PORTD = PORTD | 0x040;
else
PORTD = PORTD & ~0x040;
// DAC bit 8, PD5
if (dac_val & 0x0100)
PORTD = PORTD | 0x020;
else
PORTD = PORTD & ~0x020;
// DAC bit 7, PD3
if (dac_val & 0x080)
PORTD = PORTD | 0x08;
else
PORTD = PORTD & ~0x08;
// DAC bit 6
if (dac_val & 0x040)
PORTD = PORTD | 0x04;
else
PORTD = PORTD & ~0x04;
#ifdef DEBUG
Serial.println("After");
Serial.print("PORTB=0x");
Serial.println(PORTB,HEX);
Serial.print("PORTC=0x");
Serial.println(PORTC,HEX);
Serial.print("PORTD=0x");
Serial.println(PORTD,HEX);
#endif //DEBUG
return;
}
uint8_t fromHex(char hi, char lo)
{
uint8_t b;
hi = toupper(hi);
if( isxdigit(hi) ) {
if( hi > '9' ) hi -= 7; // software offset for A-F
hi -= 0x30; // subtract ASCII offset
b = hi<<4;
lo = toupper(lo);
if( isxdigit(lo) ) {
if( lo > '9' ) lo -= 7; // software offset for A-F
lo -= 0x30; // subtract ASCII offset
b = b + lo;
return b;
} // else error
} // else error
return 0;
}