]> git.sesse.net Git - vlc/blob - modules/video_filter/atmo/MoMoConnection.cpp
Qt: kill a bunch of warnings
[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 #ifdef HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "AtmoDefs.h"
15 #include "MoMoConnection.h"
16
17 #if !defined(_ATMO_VLC_PLUGIN_)
18 # include "MoMoConfigDialog.h"
19 #endif
20
21 #include <stdio.h>
22 #include <fcntl.h>
23
24 #if !defined(WIN32)
25 #include <termios.h>
26 #include <unistd.h>
27 #endif
28
29
30 CMoMoConnection::CMoMoConnection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
31     m_hComport = INVALID_HANDLE_VALUE;
32 }
33
34 CMoMoConnection::~CMoMoConnection() {
35 }
36
37 ATMO_BOOL CMoMoConnection::OpenConnection() {
38 #if defined(_ATMO_VLC_PLUGIN_)
39      char *serdevice = m_pAtmoConfig->getSerialDevice();
40      if(!serdevice)
41         return ATMO_FALSE;
42 #else
43      int portNummer = m_pAtmoConfig->getComport();
44      m_dwLastWin32Error = 0;
45          if(portNummer < 1) return ATMO_FALSE; // make no real sense;-)
46 #endif
47
48          CloseConnection();
49
50 #if !defined(_ATMO_VLC_PLUGIN_)
51      char serdevice[16];  // com4294967295
52      sprintf(serdevice,"com%d",portNummer);
53 #endif
54
55 #if defined(WIN32)
56
57      m_hComport = CreateFile(serdevice, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
58      if(m_hComport == INVALID_HANDLE_VALUE) {
59 //      we have a problem here can't open com port... somebody else may use it?
60 //          m_dwLastWin32Error = GetLastError();
61             return ATMO_FALSE;
62      }
63      /* change serial settings (Speed, stopbits etc.) */
64      DCB dcb; // für comport-parameter
65      dcb.DCBlength = sizeof(DCB);
66      GetCommState (m_hComport, &dcb); // ger current serialport settings
67      dcb.BaudRate  = 9600;        // set speed
68      dcb.ByteSize  = 8;            // set databits
69      dcb.Parity    = NOPARITY;     // set parity
70      dcb.StopBits  = ONESTOPBIT;   // set one stop bit
71      SetCommState (m_hComport, &dcb);    // apply settings
72
73 #else
74
75      int bconst = B9600;
76      m_hComport = open(serdevice,O_RDWR |O_NOCTTY);
77      if(m_hComport < 0) {
78             return ATMO_FALSE;
79      }
80
81      struct termios tio;
82      memset(&tio,0,sizeof(tio));
83      tio.c_cflag = (CS8 | CREAD | HUPCL | CLOCAL);
84      tio.c_iflag = (INPCK | BRKINT);
85      cfsetispeed(&tio, bconst);
86      cfsetospeed(&tio, bconst);
87      if(!tcsetattr(m_hComport, TCSANOW, &tio)) {
88          tcflush(m_hComport, TCIOFLUSH);
89      } else {
90          // can't change parms
91         close(m_hComport);
92         m_hComport = -1;
93         return false;
94      }
95
96 #endif
97
98      return true;
99 }
100
101 void CMoMoConnection::CloseConnection() {
102   if(m_hComport!=INVALID_HANDLE_VALUE) {
103 #if defined(WIN32)
104      CloseHandle(m_hComport);
105 #else
106      close(m_hComport);
107 #endif
108          m_hComport = INVALID_HANDLE_VALUE;
109   }
110 }
111
112 ATMO_BOOL CMoMoConnection::isOpen(void) {
113          return (m_hComport != INVALID_HANDLE_VALUE);
114 }
115
116 ATMO_BOOL CMoMoConnection::HardwareWhiteAdjust(int global_gamma,
117                                                      int global_contrast,
118                                                      int contrast_red,
119                                                      int contrast_green,
120                                                      int contrast_blue,
121                                                      int gamma_red,
122                                                      int gamma_green,
123                                                      int gamma_blue,
124                                                      ATMO_BOOL storeToEeprom) {
125     return ATMO_FALSE;
126 }
127
128
129 ATMO_BOOL CMoMoConnection::SendData(pColorPacket data) {
130    if(m_hComport == INVALID_HANDLE_VALUE)
131           return ATMO_FALSE;
132
133    int channels = getNumChannels();
134    DWORD bufSize = channels * 3;
135    unsigned char *buffer = new unsigned char[ bufSize ];
136    DWORD iBytesWritten;
137
138    Lock();
139    int i_red   = 0;
140    int i_green = channels;
141    int i_blue  = channels * 2;
142    int idx;
143    /*
144      3 ch
145      i_red = 0, i_green = 3, i_blue = 6
146      4 ch
147      i_red = 0, i_green = 4, i_blue = 8
148    */
149
150    for(int i=0; i < channels ; i++) {
151        if(m_ChannelAssignment && (i < m_NumAssignedChannels))
152           idx = m_ChannelAssignment[i];
153        else
154           idx = -1;
155        if((idx>=0) && (idx<data->numColors)) {
156           buffer[i_red++]   = data->zone[idx].r;
157           buffer[i_green++] = data->zone[idx].g;
158           buffer[i_blue++]  = data->zone[idx].b;
159        } else {
160           buffer[i_red++]   = 0;
161           buffer[i_green++] = 0;
162           buffer[i_blue++]  = 0;
163        }
164    }
165
166 #if defined(WIN32)
167    WriteFile(m_hComport, buffer, bufSize, &iBytesWritten, NULL); // send to COM-Port
168 #else
169    iBytesWritten = write(m_hComport, buffer, bufSize);
170    tcdrain(m_hComport);
171 #endif
172    delete []buffer;
173
174    Unlock();
175
176    return (iBytesWritten == bufSize) ? ATMO_TRUE : ATMO_FALSE;
177 }
178
179
180 ATMO_BOOL CMoMoConnection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
181 {
182    if(!ca) return ATMO_FALSE;
183    ca->setSize( getNumChannels() );  // oder 4 ? depending on config!
184    ca->setZoneIndex(0, 0); // Zone 5
185    ca->setZoneIndex(1, 1);
186    ca->setZoneIndex(2, 2);
187    ca->setZoneIndex(3, 3);
188    return ATMO_TRUE;
189 }
190
191 int CMoMoConnection::getNumChannels()
192 {
193    return m_pAtmoConfig->getMoMo_Channels();
194 }
195
196
197 #if !defined(_ATMO_VLC_PLUGIN_)
198
199 char *CMoMoConnection::getChannelName(int ch)
200 {
201   if(ch < 0) return NULL;
202   char buf[30];
203
204   switch(ch) {
205       case 0:
206           sprintf(buf,"Channel [%d]",ch);
207           break;
208       case 1:
209           sprintf(buf,"Channel [%d]",ch);
210           break;
211       case 2:
212           sprintf(buf,"Channel [%d]",ch);
213           break;
214       case 3:
215           sprintf(buf,"Channel [%d]",ch);
216           break;
217       default:
218           sprintf(buf,"Channel [%d]",ch);
219           break;
220   }
221
222   return strdup(buf);
223 }
224
225 ATMO_BOOL CMoMoConnection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
226 {
227     CMoMoConfigDialog *dlg = new CMoMoConfigDialog(hInst, parent, cfg);
228
229     INT_PTR result = dlg->ShowModal();
230
231     delete dlg;
232
233     if(result == IDOK)
234       return ATMO_TRUE;
235     else
236       return ATMO_FALSE;
237 }
238
239 #endif