-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.cpp
183 lines (159 loc) · 5.04 KB
/
main.cpp
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
#include "Arduino.h"
extern "C" void __cxa_pure_virtual() {}
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* RGB Color Mixer
* Copyright (C) 2010 Simon Newton
* An RGB mixer that behaves like a DMX USB Pro.
* http://opendmx.net/index.php/Arduino_RGB_Mixer
*/
#include "Common.h"
#include "MessageLabels.h"
#include "RDMHandlers.h"
#include "UsbProReceiver.h"
#include "UsbProSender.h"
#include "WidgetSettings.h"
// Define the variables from Common.h
char DEVICE_NAME[] = "Arduino RGB Mixer";
byte DEVICE_NAME_SIZE = sizeof(DEVICE_NAME);
char MANUFACTURER_NAME[] = "Open Lighting";
byte MANUFACTURER_NAME_SIZE = sizeof(MANUFACTURER_NAME);
UsbProSender sender;
RDMHandler rdm_handler(&sender);
// Pin constants
const byte LED_PIN = 13;
const byte PWM_PINS[] = {3, 5, 6, 9, 10, 11};
// device setting
const byte DEVICE_PARAMS[] = {0, 1, 0, 0, 40};
const byte DEVICE_ID[] = {1, 0};
// global state
byte led_state = LOW; // flash the led when we get data.
/**
* Send the Serial Number response
*/
void SendSerialNumberResponse() {
long serial = WidgetSettings.SerialNumber();
sender.SendMessageHeader(SERIAL_NUMBER_LABEL, sizeof(serial));
sender.Write((byte*) &serial, sizeof(serial));
sender.SendMessageFooter();
}
/**
* Send the device id / name response
*/
void SendDeviceResponse() {
sender.SendMessageHeader(NAME_LABEL,
sizeof(DEVICE_ID) + sizeof(DEVICE_NAME));
sender.Write(DEVICE_ID, sizeof(DEVICE_ID));
sender.Write((byte*) DEVICE_NAME, sizeof(DEVICE_NAME));
sender.SendMessageFooter();
}
/**
* Send the manufacturer id / name response
*/
void SendManufacturerResponse() {
int esta_id = WidgetSettings.EstaId();
sender.SendMessageHeader(MANUFACTURER_LABEL,
sizeof(esta_id) + sizeof(MANUFACTURER_NAME));
// ESTA ID is sent in little endian format
sender.Write(esta_id);
sender.Write(esta_id >> 8);
sender.Write((byte*) MANUFACTURER_NAME, sizeof(MANUFACTURER_NAME));
sender.SendMessageFooter();
}
/**
* Write the DMX values to the PWM pins.
* @param data the dmx data buffer.
* @param size the size of the dmx buffer.
*/
void SetPWM(const byte data[], unsigned int size) {
unsigned int start_address = WidgetSettings.StartAddress() - 1;
byte personality = WidgetSettings.Personality();
for (byte i = 0; i < sizeof(PWM_PINS) && start_address + i < size; ++i) {
byte value = data[start_address + i];
bool invert = false;
invert |= i < 3 && personality > 1;
invert |= i >= 3 && personality == 3;
analogWrite(PWM_PINS[i], invert ? 255 - value : value);
}
}
/**
* Called when there is no serial data
*/
void Idle() {
if (WidgetSettings.PerformWrite()) {
rdm_handler.QueueSetDeviceLabel();
}
}
/*
* Called when a full message is received from the host.
* @param label the message label.
* @param message the array of bytes that make up the message.
* @param message_size the size of the message.
*/
void TakeAction(byte label, const byte *message, unsigned int message_size) {
switch (label) {
case PARAMETERS_LABEL:
// Widget Parameters request
sender.WriteMessage(PARAMETERS_LABEL,
sizeof(DEVICE_PARAMS),
DEVICE_PARAMS);
break;
case DMX_DATA_LABEL:
if (message[0] == 0) {
// 0 start code
led_state = !led_state;
digitalWrite(LED_PIN, led_state);
SetPWM(&message[1], message_size);
}
break;
case SERIAL_NUMBER_LABEL:
SendSerialNumberResponse();
break;
case NAME_LABEL:
SendDeviceResponse();
break;
case MANUFACTURER_LABEL:
SendManufacturerResponse();
break;
case RDM_LABEL:
led_state = !led_state;
digitalWrite(LED_PIN, led_state);
rdm_handler.HandleRDMMessage(message, message_size);
break;
}
}
/**
* The main function
*/
int main(void) {
init();
WidgetSettings.Init();
byte personality = WidgetSettings.Personality();
// set the output pin levels according to the personality
for (byte i = 0; i < sizeof(PWM_PINS); i++) {
pinMode(PWM_PINS[i], OUTPUT);
bool invert = false;
invert |= i < 3 && personality > 1;
invert |= i >= 3 && personality == 3;
analogWrite(PWM_PINS[i], invert ? 255 : 0);
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, led_state);
UsbProReceiver receiver(TakeAction, Idle);
// this never returns
receiver.Read();
return 0;
}