]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/AtmoClassicConnection.cpp
enhanced & corrected AtmoLight filter module
[vlc] / modules / video_filter / atmo / AtmoClassicConnection.cpp
1 /*
2  * AtmoSerialConnection.cpp: Class for communication with the serial hardware of
3  * Atmo Light, opens and configures the serial port
4  *
5  * See the README.txt file for copyright information and how to reach the author(s).
6  *
7  * $Id$
8  */
9
10
11 #include "AtmoDefs.h"
12 #include "AtmoClassicConnection.h"
13
14 #if !defined(_ATMO_VLC_PLUGIN_)
15 # include "AtmoClassicConfigDialog.h"
16 #endif
17
18 #include <stdio.h>
19 #include <fcntl.h>
20
21 #if !defined(WIN32)
22 #include <termios.h>
23 #include <unistd.h>
24 #endif
25
26
27 CAtmoClassicConnection::CAtmoClassicConnection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
28     m_hComport = INVALID_HANDLE_VALUE;
29 }
30
31 CAtmoClassicConnection::~CAtmoClassicConnection() {
32 }
33
34 ATMO_BOOL CAtmoClassicConnection::OpenConnection() {
35 #if defined(_ATMO_VLC_PLUGIN_)
36      char *serdevice = m_pAtmoConfig->getSerialDevice();
37      if(!serdevice)
38         return ATMO_FALSE;
39 #else
40      int portNummer = m_pAtmoConfig->getComport();
41      m_dwLastWin32Error = 0;
42          if(portNummer < 1) return ATMO_FALSE; // make no real sense;-)
43 #endif
44
45          CloseConnection();
46
47 #if !defined(_ATMO_VLC_PLUGIN_)
48      char serdevice[16];  // com4294967295
49      sprintf(serdevice,"com%d",portNummer);
50 #endif
51
52 #if defined(WIN32)
53
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();
58             return ATMO_FALSE;
59      }
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
69
70 #else
71
72      int bconst = B38400;
73      m_hComport = open(serdevice,O_RDWR |O_NOCTTY);
74      if(m_hComport < 0) {
75             return ATMO_FALSE;
76      }
77
78      struct termios tio;
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);
86      } else {
87          // can't change parms
88         close(m_hComport);
89         m_hComport = -1;
90         return false;
91      }
92
93 #endif
94
95      return true;
96 }
97
98 void CAtmoClassicConnection::CloseConnection() {
99   if(m_hComport!=INVALID_HANDLE_VALUE) {
100 #if defined(WIN32)
101      CloseHandle(m_hComport);
102 #else
103      close(m_hComport);
104 #endif
105          m_hComport = INVALID_HANDLE_VALUE;
106   }
107 }
108
109 ATMO_BOOL CAtmoClassicConnection::isOpen(void) {
110          return (m_hComport != INVALID_HANDLE_VALUE);
111 }
112
113 ATMO_BOOL CAtmoClassicConnection::HardwareWhiteAdjust(int global_gamma,
114                                                      int global_contrast,
115                                                      int contrast_red,
116                                                      int contrast_green,
117                                                      int contrast_blue,
118                                                      int gamma_red,
119                                                      int gamma_green,
120                                                      int gamma_blue,
121                                                      ATMO_BOOL storeToEeprom) {
122      if(m_hComport == INVALID_HANDLE_VALUE)
123             return ATMO_FALSE;
124
125      DWORD iBytesWritten;
126 /*
127 [0] = 255
128 [1] = 00
129 [2] = 00
130 [3] = 101
131
132 [4]  brightness  0..255 ?
133
134 [5]  Contrast Red     11 .. 100
135 [6]  Contrast  Green  11 .. 100
136 [7]  Contrast  Blue   11 .. 100
137
138 [8]   Gamma Red    11 .. 35
139 [9]   Gamma Red    11 .. 35
140 [10]  Gamma Red    11 .. 35
141
142 [11]  Globale Contrast  11 .. 100
143
144 [12]  Store Data: 199 (else 0)
145
146 */
147      unsigned char sendBuffer[16];
148      sendBuffer[0] = 0xFF;
149      sendBuffer[1] = 0x00;
150      sendBuffer[2] = 0x00;
151      sendBuffer[3] = 101;
152
153      sendBuffer[4] = (global_gamma & 255);
154
155      sendBuffer[5] = (contrast_red & 255);
156      sendBuffer[6] = (contrast_green & 255);
157      sendBuffer[7] = (contrast_blue & 255);
158
159      sendBuffer[8]  = (gamma_red & 255);
160      sendBuffer[9]  = (gamma_green & 255);
161      sendBuffer[10] = (gamma_blue & 255);
162
163      sendBuffer[11] = (global_contrast & 255);
164
165      if(storeToEeprom == ATMO_TRUE)
166         sendBuffer[12] = 199; // store to eeprom!
167      else
168         sendBuffer[12] = 0;
169
170 #if defined(WIN32)
171      WriteFile(m_hComport, sendBuffer, 13, &iBytesWritten, NULL); // send to COM-Port
172 #else
173      iBytesWritten = write(m_hComport, sendBuffer, 13);
174      tcdrain(m_hComport);
175 #endif
176
177      return (iBytesWritten == 13) ? ATMO_TRUE : ATMO_FALSE;
178 }
179
180
181 ATMO_BOOL CAtmoClassicConnection::SendData(pColorPacket data) {
182    if(m_hComport == INVALID_HANDLE_VALUE)
183           return ATMO_FALSE;
184
185    unsigned char buffer[19];
186    DWORD iBytesWritten;
187
188    buffer[0] = 0xFF;  // Start Byte
189    buffer[1] = 0x00;  // Start channel 0
190    buffer[2] = 0x00;  // Start channel 0
191    buffer[3] = 15; //
192    int iBuffer = 4;
193    int idx;
194
195    Lock();
196
197    for(int i=0; i < 5 ; i++) {
198        if(m_ChannelAssignment && (i < m_NumAssignedChannels))
199           idx = m_ChannelAssignment[i];
200        else
201           idx = -1;
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;
206        } else {
207           buffer[iBuffer++] = 0;
208           buffer[iBuffer++] = 0;
209           buffer[iBuffer++] = 0;
210        }
211    }
212
213 #if defined(WIN32)
214    WriteFile(m_hComport, buffer, 19, &iBytesWritten, NULL); // send to COM-Port
215 #else
216    iBytesWritten = write(m_hComport, buffer, 19);
217    tcdrain(m_hComport);
218 #endif
219
220    Unlock();
221
222    return (iBytesWritten == 19) ? ATMO_TRUE : ATMO_FALSE;
223 }
224
225
226 ATMO_BOOL CAtmoClassicConnection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
227 {
228    if(!ca) return ATMO_FALSE;
229    ca->setSize(5);
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);
235    return ATMO_TRUE;
236 }
237
238 #if !defined(_ATMO_VLC_PLUGIN_)
239
240 char *CAtmoClassicConnection::getChannelName(int ch)
241 {
242   if(ch < 0) return NULL;
243   char buf[30];
244
245   switch(ch) {
246       case 0:
247           sprintf(buf,"Summen Kanal [%d]",ch);
248           break;
249       case 1:
250           sprintf(buf,"Linker Kanal [%d]",ch);
251           break;
252       case 2:
253           sprintf(buf,"Rechter Kanal [%d]",ch);
254           break;
255       case 3:
256           sprintf(buf,"Oberer Kanal [%d]",ch);
257           break;
258       case 4:
259           sprintf(buf,"Unterer Kanal [%d]",ch);
260           break;
261       default:
262           sprintf(buf,"Kanal [%d]",ch);
263           break;
264   }
265
266   return strdup(buf);
267 }
268
269 ATMO_BOOL CAtmoClassicConnection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
270 {
271     CAtmoClassicConfigDialog *dlg = new CAtmoClassicConfigDialog(hInst, parent, cfg);
272
273     INT_PTR result = dlg->ShowModal();
274
275     delete dlg;
276
277     if(result == IDOK)
278       return ATMO_TRUE;
279     else
280       return ATMO_FALSE;
281 }
282
283 #endif