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).
15 #include "MoMoConnection.h"
17 #if !defined(_ATMO_VLC_PLUGIN_)
18 # include "MoMoConfigDialog.h"
30 CMoMoConnection::CMoMoConnection(CAtmoConfig *cfg) : CAtmoConnection(cfg) {
31 m_hComport = INVALID_HANDLE_VALUE;
34 CMoMoConnection::~CMoMoConnection() {
37 ATMO_BOOL CMoMoConnection::OpenConnection() {
38 #if defined(_ATMO_VLC_PLUGIN_)
39 char *serdevice = m_pAtmoConfig->getSerialDevice();
43 int portNummer = m_pAtmoConfig->getComport();
44 m_dwLastWin32Error = 0;
45 if(portNummer < 1) return ATMO_FALSE; // make no real sense;-)
50 #if !defined(_ATMO_VLC_PLUGIN_)
51 char serdevice[16]; // com4294967295
52 sprintf(serdevice,"com%d",portNummer);
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();
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
76 m_hComport = open(serdevice,O_RDWR |O_NOCTTY);
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);
101 void CMoMoConnection::CloseConnection() {
102 if(m_hComport!=INVALID_HANDLE_VALUE) {
104 CloseHandle(m_hComport);
108 m_hComport = INVALID_HANDLE_VALUE;
112 ATMO_BOOL CMoMoConnection::isOpen(void) {
113 return (m_hComport != INVALID_HANDLE_VALUE);
116 ATMO_BOOL CMoMoConnection::HardwareWhiteAdjust(int global_gamma,
124 ATMO_BOOL storeToEeprom) {
129 ATMO_BOOL CMoMoConnection::SendData(pColorPacket data) {
130 if(m_hComport == INVALID_HANDLE_VALUE)
133 int channels = getNumChannels();
134 DWORD bufSize = channels * 3;
135 unsigned char *buffer = new unsigned char[ bufSize ];
140 int i_green = channels;
141 int i_blue = channels * 2;
145 i_red = 0, i_green = 3, i_blue = 6
147 i_red = 0, i_green = 4, i_blue = 8
150 for(int i=0; i < channels ; i++) {
151 if(m_ChannelAssignment && (i < m_NumAssignedChannels))
152 idx = m_ChannelAssignment[i];
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;
161 buffer[i_green++] = 0;
162 buffer[i_blue++] = 0;
167 WriteFile(m_hComport, buffer, bufSize, &iBytesWritten, NULL); // send to COM-Port
169 iBytesWritten = write(m_hComport, buffer, bufSize);
176 return (iBytesWritten == bufSize) ? ATMO_TRUE : ATMO_FALSE;
180 ATMO_BOOL CMoMoConnection::CreateDefaultMapping(CAtmoChannelAssignment *ca)
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);
191 int CMoMoConnection::getNumChannels()
193 return m_pAtmoConfig->getMoMo_Channels();
197 #if !defined(_ATMO_VLC_PLUGIN_)
199 char *CMoMoConnection::getChannelName(int ch)
201 if(ch < 0) return NULL;
206 sprintf(buf,"Channel [%d]",ch);
209 sprintf(buf,"Channel [%d]",ch);
212 sprintf(buf,"Channel [%d]",ch);
215 sprintf(buf,"Channel [%d]",ch);
218 sprintf(buf,"Channel [%d]",ch);
225 ATMO_BOOL CMoMoConnection::ShowConfigDialog(HINSTANCE hInst, HWND parent, CAtmoConfig *cfg)
227 CMoMoConfigDialog *dlg = new CMoMoConfigDialog(hInst, parent, cfg);
229 INT_PTR result = dlg->ShowModal();