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 "MoMoConnection.h"
14 #if !defined(_ATMO_VLC_PLUGIN_)
15 # include "MoMoConfigDialog.h"
27 CMoMoConnection::CMoMoConnection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
28 m_hComport = INVALID_HANDLE_VALUE;
31 CMoMoConnection::~CMoMoConnection() {
34 ATMO_BOOL CMoMoConnection::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 = 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
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 CMoMoConnection::CloseConnection() {
99 if(m_hComport!=INVALID_HANDLE_VALUE) {
101 CloseHandle(m_hComport);
105 m_hComport = INVALID_HANDLE_VALUE;
109 ATMO_BOOL CMoMoConnection::isOpen(void) {
110 return (m_hComport != INVALID_HANDLE_VALUE);
113 ATMO_BOOL CMoMoConnection::HardwareWhiteAdjust(int global_gamma,
121 ATMO_BOOL storeToEeprom) {
126 ATMO_BOOL CMoMoConnection::SendData(pColorPacket data) {
127 if(m_hComport == INVALID_HANDLE_VALUE)
130 int channels = getNumChannels();
131 DWORD bufSize = channels * 3;
132 unsigned char *buffer = new unsigned char[ bufSize ];
137 int i_green = channels;
138 int i_blue = channels * 2;
142 i_red = 0, i_green = 3, i_blue = 6
144 i_red = 0, i_green = 4, i_blue = 8
147 for(int i=0; i < channels ; i++) {
148 if(m_ChannelAssignment && (i < m_NumAssignedChannels))
149 idx = m_ChannelAssignment[i];
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;
158 buffer[i_green++] = 0;
159 buffer[i_blue++] = 0;
164 WriteFile(m_hComport, buffer, bufSize, &iBytesWritten, NULL); // send to COM-Port
166 iBytesWritten = write(m_hComport, buffer, bufSize);
173 return (iBytesWritten == bufSize) ? ATMO_TRUE : ATMO_FALSE;
177 ATMO_BOOL CMoMoConnection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
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);
188 int CMoMoConnection::getNumChannels()
190 return m_pAtmoConfig->getMoMo_Channels();
194 #if !defined(_ATMO_VLC_PLUGIN_)
196 char *CMoMoConnection::getChannelName(int ch)
198 if(ch < 0) return NULL;
203 sprintf(buf,"Channel [%d]",ch);
206 sprintf(buf,"Channel [%d]",ch);
209 sprintf(buf,"Channel [%d]",ch);
212 sprintf(buf,"Channel [%d]",ch);
215 sprintf(buf,"Channel [%d]",ch);
222 ATMO_BOOL CMoMoConnection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
224 CMoMoConfigDialog *dlg = new CMoMoConfigDialog(hInst, parent, cfg);
226 INT_PTR result = dlg->ShowModal();