2 * AtmoSerialConnection.cpp: Class for communication with the serial hardware of
3 * Atmo Light, opens and configures the serial port
5 * See the README.txt file for copyright information and how to reach the author(s).
12 #include "AtmoClassicConnection.h"
14 #if !defined(_ATMO_VLC_PLUGIN_)
15 # include "AtmoClassicConfigDialog.h"
27 CAtmoClassicConnection::CAtmoClassicConnection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
28 m_hComport = INVALID_HANDLE_VALUE;
31 CAtmoClassicConnection::~CAtmoClassicConnection() {
34 ATMO_BOOL CAtmoClassicConnection::OpenConnection() {
35 #if defined(_ATMO_VLC_PLUGIN_)
36 char *serdevice = m_pAtmoConfig->getSerialDevice();
40 int portNummer = m_pAtmoConfig->getComport();
41 m_dwLastWin32Error = 0;
42 if(portNummer < 1) return ATMO_FALSE; // make no real sense;-)
47 #if !defined(_ATMO_VLC_PLUGIN_)
48 char serdevice[16]; // com4294967295
49 sprintf(serdevice,"com%d",portNummer);
54 m_hComport = CreateFile(serdevice, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
55 if(m_hComport == INVALID_HANDLE_VALUE) {
56 // we have a problem here can't open com port... somebody else may use it?
57 // m_dwLastWin32Error = GetLastError();
60 /* change serial settings (Speed, stopbits etc.) */
61 DCB dcb; // für comport-parameter
62 dcb.DCBlength = sizeof(DCB);
63 GetCommState (m_hComport, &dcb); // ger current serialport settings
64 dcb.BaudRate = 38400; // set speed
65 dcb.ByteSize = 8; // set databits
66 dcb.Parity = NOPARITY; // set parity
67 dcb.StopBits = ONESTOPBIT; // set one stop bit
68 SetCommState (m_hComport, &dcb); // apply settings
73 m_hComport = open(serdevice,O_RDWR |O_NOCTTY);
79 memset(&tio,0,sizeof(tio));
80 tio.c_cflag = (CS8 | CREAD | HUPCL | CLOCAL);
81 tio.c_iflag = (INPCK | BRKINT);
82 cfsetispeed(&tio, bconst);
83 cfsetospeed(&tio, bconst);
84 if(!tcsetattr(m_hComport, TCSANOW, &tio)) {
85 tcflush(m_hComport, TCIOFLUSH);
98 void CAtmoClassicConnection::CloseConnection() {
99 if(m_hComport!=INVALID_HANDLE_VALUE) {
101 CloseHandle(m_hComport);
105 m_hComport = INVALID_HANDLE_VALUE;
109 ATMO_BOOL CAtmoClassicConnection::isOpen(void) {
110 return (m_hComport != INVALID_HANDLE_VALUE);
113 ATMO_BOOL CAtmoClassicConnection::HardwareWhiteAdjust(int global_gamma,
121 ATMO_BOOL storeToEeprom) {
122 if(m_hComport == INVALID_HANDLE_VALUE)
132 [4] brightness 0..255 ?
134 [5] Contrast Red 11 .. 100
135 [6] Contrast Green 11 .. 100
136 [7] Contrast Blue 11 .. 100
138 [8] Gamma Red 11 .. 35
139 [9] Gamma Red 11 .. 35
140 [10] Gamma Red 11 .. 35
142 [11] Globale Contrast 11 .. 100
144 [12] Store Data: 199 (else 0)
147 unsigned char sendBuffer[16];
148 sendBuffer[0] = 0xFF;
149 sendBuffer[1] = 0x00;
150 sendBuffer[2] = 0x00;
153 sendBuffer[4] = (global_gamma & 255);
155 sendBuffer[5] = (contrast_red & 255);
156 sendBuffer[6] = (contrast_green & 255);
157 sendBuffer[7] = (contrast_blue & 255);
159 sendBuffer[8] = (gamma_red & 255);
160 sendBuffer[9] = (gamma_green & 255);
161 sendBuffer[10] = (gamma_blue & 255);
163 sendBuffer[11] = (global_contrast & 255);
165 if(storeToEeprom == ATMO_TRUE)
166 sendBuffer[12] = 199; // store to eeprom!
171 WriteFile(m_hComport, sendBuffer, 13, &iBytesWritten, NULL); // send to COM-Port
173 iBytesWritten = write(m_hComport, sendBuffer, 13);
177 return (iBytesWritten == 13) ? ATMO_TRUE : ATMO_FALSE;
181 ATMO_BOOL CAtmoClassicConnection::SendData(pColorPacket data) {
182 if(m_hComport == INVALID_HANDLE_VALUE)
185 unsigned char buffer[19];
188 buffer[0] = 0xFF; // Start Byte
189 buffer[1] = 0x00; // Start channel 0
190 buffer[2] = 0x00; // Start channel 0
197 for(int i=0; i < 5 ; i++) {
198 if(m_ChannelAssignment && (i < m_NumAssignedChannels))
199 idx = m_ChannelAssignment[i];
202 if((idx>=0) && (idx<data->numColors)) {
203 buffer[iBuffer++] = data->zone[idx].r;
204 buffer[iBuffer++] = data->zone[idx].g;
205 buffer[iBuffer++] = data->zone[idx].b;
207 buffer[iBuffer++] = 0;
208 buffer[iBuffer++] = 0;
209 buffer[iBuffer++] = 0;
214 WriteFile(m_hComport, buffer, 19, &iBytesWritten, NULL); // send to COM-Port
216 iBytesWritten = write(m_hComport, buffer, 19);
222 return (iBytesWritten == 19) ? ATMO_TRUE : ATMO_FALSE;
226 ATMO_BOOL CAtmoClassicConnection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
228 if(!ca) return ATMO_FALSE;
230 ca->setZoneIndex(0, 4); // Zone 5
231 ca->setZoneIndex(1, 3);
232 ca->setZoneIndex(2, 1);
233 ca->setZoneIndex(3, 0);
234 ca->setZoneIndex(4, 2);
238 #if !defined(_ATMO_VLC_PLUGIN_)
240 char *CAtmoClassicConnection::getChannelName(int ch)
242 if(ch < 0) return NULL;
247 sprintf(buf,"Summen Kanal [%d]",ch);
250 sprintf(buf,"Linker Kanal [%d]",ch);
253 sprintf(buf,"Rechter Kanal [%d]",ch);
256 sprintf(buf,"Oberer Kanal [%d]",ch);
259 sprintf(buf,"Unterer Kanal [%d]",ch);
262 sprintf(buf,"Kanal [%d]",ch);
269 ATMO_BOOL CAtmoClassicConnection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
271 CAtmoClassicConfigDialog *dlg = new CAtmoClassicConfigDialog(hInst, parent, cfg);
273 INT_PTR result = dlg->ShowModal();