2 * AtmoTools.cpp: Collection of tool and helperfunction
4 * See the README.txt file for copyright information and how to reach the author(s).
10 #include "AtmoLiveView.h"
11 #include "AtmoSerialConnection.h"
13 #if !defined(_ATMO_VLC_PLUGIN_)
14 # include "AtmoColorChanger.h"
15 # include "AtmoLeftRightColorChanger.h"
16 # include "AtmoDummyConnection.h"
17 # include "AtmoDmxSerialConnection.h"
21 CAtmoTools::CAtmoTools(void)
25 CAtmoTools::~CAtmoTools(void)
29 void CAtmoTools::ShowShutdownColor(CAtmoDynData *pDynData)
31 pDynData->LockCriticalSection();
33 CAtmoConnection *atmoConnection = pDynData->getAtmoConnection();
34 CAtmoConfig *atmoConfig = pDynData->getAtmoConfig();
35 if((atmoConnection != NULL) && (atmoConfig!=NULL)) {
36 int r[ATMO_NUM_CHANNELS],g[ATMO_NUM_CHANNELS],b[ATMO_NUM_CHANNELS],i;
37 // set a special color? on shutdown of the software? mostly may use black or so ...
38 // if this function ist disabled ... atmo will continuing to show the last color...
39 if(atmoConnection->isOpen() == ATMO_TRUE) {
40 if(atmoConfig->isSetShutdownColor() == 1) {
41 for(i=0;i<ATMO_NUM_CHANNELS;i++) {
42 r[i] = atmoConfig->getShutdownColor_Red();
43 g[i] = atmoConfig->getShutdownColor_Green();
44 b[i] = atmoConfig->getShutdownColor_Blue();
46 atmoConnection->SendData(ATMO_NUM_CHANNELS,r,g,b);
51 pDynData->UnLockCriticalSection();
54 EffectMode CAtmoTools::SwitchEffect(CAtmoDynData *pDynData, EffectMode newEffectMode)
56 // may need a critical section??
57 if(pDynData == NULL) {
60 pDynData->LockCriticalSection();
62 CAtmoConfig *atmoConfig = pDynData->getAtmoConfig();
63 if(atmoConfig == NULL) {
64 pDynData->UnLockCriticalSection();
67 CAtmoConnection *atmoConnection = pDynData->getAtmoConnection();
69 EffectMode oldEffectMode = atmoConfig->getEffectMode();
70 CThread *currentEffect = pDynData->getEffectThread();
72 // stop and delete/cleanup current Effect Thread...
73 pDynData->setEffectThread(NULL);
74 if(currentEffect!=NULL) {
75 currentEffect->Terminate();
80 if((atmoConnection!=NULL) && (atmoConnection->isOpen()==ATMO_TRUE)) {
81 // neuen EffectThread nur mit aktiver Connection starten...
83 switch(newEffectMode) {
88 // get values from config - and put them to all channels?
89 int r[ATMO_NUM_CHANNELS],g[ATMO_NUM_CHANNELS],b[ATMO_NUM_CHANNELS];
90 for(int i=0;i<ATMO_NUM_CHANNELS;i++) {
91 r[i] = (atmoConfig->getStaticColor_Red() * atmoConfig->getWhiteAdjustment_Red())/255;
92 g[i] = (atmoConfig->getStaticColor_Green() * atmoConfig->getWhiteAdjustment_Green())/255;
93 b[i] = (atmoConfig->getStaticColor_Blue() * atmoConfig->getWhiteAdjustment_Blue())/255;
95 atmoConnection->SendData(ATMO_NUM_CHANNELS,r,g,b);
99 currentEffect = new CAtmoLiveView(pDynData);
102 #if !defined(_ATMO_VLC_PLUGIN_)
104 currentEffect = new CAtmoColorChanger(atmoConnection, atmoConfig);
108 #if !defined(_ATMO_VLC_PLUGIN_)
109 case emLrColorChange:
110 currentEffect = new CAtmoLeftRightColorChanger(atmoConnection, atmoConfig);
117 atmoConfig->setEffectMode(newEffectMode);
119 pDynData->setEffectThread(currentEffect);
121 if(currentEffect!=NULL)
122 currentEffect->Run();
124 pDynData->UnLockCriticalSection();
125 return oldEffectMode;
128 ATMO_BOOL CAtmoTools::RecreateConnection(CAtmoDynData *pDynData)
130 pDynData->LockCriticalSection();
132 CAtmoConnection *current = pDynData->getAtmoConnection();
133 AtmoConnectionType act = pDynData->getAtmoConfig()->getConnectionType();
134 pDynData->setAtmoConnection(NULL);
135 if(current != NULL) {
136 current->CloseConnection();
141 case actSerialPort: {
142 CAtmoSerialConnection *tempConnection = new CAtmoSerialConnection(pDynData->getAtmoConfig());
143 if(tempConnection->OpenConnection() == ATMO_FALSE) {
144 #if !defined(_ATMO_VLC_PLUGIN_)
145 char errorMsgBuf[200];
146 sprintf(errorMsgBuf,"Failed to open serial port com%d with errorcode: %d (0x%x)",
147 pDynData->getAtmoConfig()->getComport(),
148 tempConnection->getLastError(),
149 tempConnection->getLastError()
151 MessageBox(0,errorMsgBuf,"Error",MB_ICONERROR | MB_OK);
153 delete tempConnection;
155 pDynData->UnLockCriticalSection();
158 pDynData->setAtmoConnection(tempConnection);
160 CAtmoTools::SetChannelAssignment(pDynData,
161 pDynData->getAtmoConfig()->getCurrentChannelAssignment());
163 pDynData->UnLockCriticalSection();
167 #if !defined(_ATMO_VLC_PLUGIN_)
169 CAtmoDummyConnection *tempConnection = new CAtmoDummyConnection(pDynData->getHinstance(),
170 pDynData->getAtmoConfig());
171 if(tempConnection->OpenConnection() == ATMO_FALSE) {
172 delete tempConnection;
174 pDynData->UnLockCriticalSection();
177 pDynData->setAtmoConnection(tempConnection);
179 CAtmoTools::SetChannelAssignment(pDynData, pDynData->getAtmoConfig()->getCurrentChannelAssignment());
181 pDynData->UnLockCriticalSection();
186 // create here your DMX connections... instead of the dummy....
187 CAtmoDmxSerialConnection *tempConnection = new CAtmoDmxSerialConnection(pDynData->getAtmoConfig());
188 if(tempConnection->OpenConnection() == ATMO_FALSE) {
189 delete tempConnection;
191 pDynData->UnLockCriticalSection();
194 pDynData->setAtmoConnection(tempConnection);
196 CAtmoTools::SetChannelAssignment(pDynData, pDynData->getAtmoConfig()->getCurrentChannelAssignment());
198 pDynData->UnLockCriticalSection();
204 pDynData->UnLockCriticalSection();
210 tColorPacket CAtmoTools::WhiteCalibration(CAtmoConfig *pAtmoConfig, tColorPacket ColorPacket)
212 int w_adj_red = pAtmoConfig->getWhiteAdjustment_Red();
213 int w_adj_green = pAtmoConfig->getWhiteAdjustment_Green();
214 int w_adj_blue = pAtmoConfig->getWhiteAdjustment_Blue();
216 for (int i = 0; i < ATMO_NUM_CHANNELS; i++) {
217 ColorPacket.channel[i].r = (unsigned char)(((int)w_adj_red * (int)ColorPacket.channel[i].r) / 255);
218 ColorPacket.channel[i].g = (unsigned char)(((int)w_adj_green * (int)ColorPacket.channel[i].g) / 255);
219 ColorPacket.channel[i].b = (unsigned char)(((int)w_adj_blue * (int)ColorPacket.channel[i].b) / 255);
224 tColorPacket CAtmoTools::ApplyGamma(CAtmoConfig *pAtmoConfig, tColorPacket ColorPacket)
229 int CAtmoTools::SetChannelAssignment(CAtmoDynData *pDynData, int index)
231 CAtmoConfig *pAtmoConfig = pDynData->getAtmoConfig();
232 CAtmoConnection *pAtmoConnection = pDynData->getAtmoConnection();
233 int oldIndex = pAtmoConfig->getCurrentChannelAssignment();
235 tChannelAssignment *ca = pAtmoConfig->getChannelAssignment(index);
236 if((ca!=NULL) && (pAtmoConnection!=NULL)) {
237 pAtmoConnection->SetChannelAssignment(ca);
238 pAtmoConfig->setCurrentChannelAssignment(index);
244 #if !defined(_ATMO_VLC_PLUGIN_)
246 void CAtmoTools::SaveBitmap(HDC hdc,HBITMAP hBmp,char *fileName) {
248 BITMAPFILEHEADER bmpFileHeader;
249 ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
250 bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
252 GetDIBits(hdc,hBmp,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
253 if(bmpInfo.bmiHeader.biSizeImage<=0)
254 bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth * abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
255 void *pBuf = malloc(bmpInfo.bmiHeader.biSizeImage);
256 bmpInfo.bmiHeader.biCompression=BI_RGB;
258 GetDIBits(hdc,hBmp,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
261 bmpFileHeader.bfReserved1=0;
262 bmpFileHeader.bfReserved2=0;
263 bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
264 #ifdef _ATMO_VLC_PLUGIN_
265 bmpFileHeader.bfType = VLC_TWOCC('M','B');
267 bmpFileHeader.bfType = MakeWord('M','B');
269 bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
273 fp = fopen(fileName,"wb");
274 fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
275 fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
276 fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);