]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/MoMoConnection.cpp
enhanced & corrected AtmoLight filter module
[vlc] / modules / video_filter / atmo / MoMoConnection.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 "MoMoConnection.h"
13
14 #if !defined(_ATMO_VLC_PLUGIN_)
15 # include "MoMoConfigDialog.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 CMoMoConnection::CMoMoConnection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
28     m_hComport = INVALID_HANDLE_VALUE;
29 }
30
31 CMoMoConnection::~CMoMoConnection() {
32 }
33
34 ATMO_BOOL CMoMoConnection::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  = 9600;        // 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 = B9600;
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 CMoMoConnection::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 CMoMoConnection::isOpen(void) {
110          return (m_hComport != INVALID_HANDLE_VALUE);
111 }
112
113 ATMO_BOOL CMoMoConnection::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     return ATMO_FALSE;
123 }
124
125
126 ATMO_BOOL CMoMoConnection::SendData(pColorPacket data) {
127    if(m_hComport == INVALID_HANDLE_VALUE)
128           return ATMO_FALSE;
129
130    int channels = getNumChannels();
131    DWORD bufSize = channels * 3;
132    unsigned char *buffer = new unsigned char[ bufSize ];
133    DWORD iBytesWritten;
134
135    Lock();
136    int i_red   = 0;
137    int i_green = channels;
138    int i_blue  = channels * 2;
139    int idx;
140    /*
141      3 ch
142      i_red = 0, i_green = 3, i_blue = 6
143      4 ch
144      i_red = 0, i_green = 4, i_blue = 8
145    */
146
147    for(int i=0; i < channels ; i++) {
148        if(m_ChannelAssignment && (i < m_NumAssignedChannels))
149           idx = m_ChannelAssignment[i];
150        else
151           idx = -1;
152        if((idx>=0) && (idx<data->numColors)) {
153           buffer[i_red++]   = data->zone[idx].r;
154           buffer[i_green++] = data->zone[idx].g;
155           buffer[i_blue++]  = data->zone[idx].b;
156        } else {
157           buffer[i_red++]   = 0;
158           buffer[i_green++] = 0;
159           buffer[i_blue++]  = 0;
160        }
161    }
162
163 #if defined(WIN32)
164    WriteFile(m_hComport, buffer, bufSize, &iBytesWritten, NULL); // send to COM-Port
165 #else
166    iBytesWritten = write(m_hComport, buffer, bufSize);
167    tcdrain(m_hComport);
168 #endif
169    delete []buffer;
170
171    Unlock();
172
173    return (iBytesWritten == bufSize) ? ATMO_TRUE : ATMO_FALSE;
174 }
175
176
177 ATMO_BOOL CMoMoConnection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
178 {
179    if(!ca) return ATMO_FALSE;
180    ca->setSize( getNumChannels() );  // oder 4 ? depending on config!
181    ca->setZoneIndex(0, 0); // Zone 5
182    ca->setZoneIndex(1, 1);
183    ca->setZoneIndex(2, 2);
184    ca->setZoneIndex(3, 3);
185    return ATMO_TRUE;
186 }
187
188 int CMoMoConnection::getNumChannels()
189 {
190    return m_pAtmoConfig->getMoMo_Channels();
191 }
192
193
194 #if !defined(_ATMO_VLC_PLUGIN_)
195
196 char *CMoMoConnection::getChannelName(int ch)
197 {
198   if(ch < 0) return NULL;
199   char buf[30];
200
201   switch(ch) {
202       case 0:
203           sprintf(buf,"Channel [%d]",ch);
204           break;
205       case 1:
206           sprintf(buf,"Channel [%d]",ch);
207           break;
208       case 2:
209           sprintf(buf,"Channel [%d]",ch);
210           break;
211       case 3:
212           sprintf(buf,"Channel [%d]",ch);
213           break;
214       default:
215           sprintf(buf,"Channel [%d]",ch);
216           break;
217   }
218
219   return strdup(buf);
220 }
221
222 ATMO_BOOL CMoMoConnection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
223 {
224     CMoMoConfigDialog *dlg = new CMoMoConfigDialog(hInst, parent, cfg);
225
226     INT_PTR result = dlg->ShowModal();
227
228     delete dlg;
229
230     if(result == IDOK)
231       return ATMO_TRUE;
232     else
233       return ATMO_FALSE;
234 }
235
236 #endif