AtmoTools.cpp
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:10k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*
  2.  * AtmoTools.cpp: Collection of tool and helperfunction
  3.  *
  4.  * See the README.txt file for copyright information and how to reach the author(s).
  5.  *
  6.  * $Id: 91e41c7a11b939d64b3b4677349178a8c8404ce1 $
  7.  */
  8. #include "AtmoTools.h"
  9. #include "AtmoLiveView.h"
  10. #include "AtmoSerialConnection.h"
  11. #if !defined(_ATMO_VLC_PLUGIN_)
  12. #   include "AtmoColorChanger.h"
  13. #   include "AtmoLeftRightColorChanger.h"
  14. #   include "AtmoDummyConnection.h"
  15. #   include "AtmoDmxSerialConnection.h"
  16. #endif
  17. CAtmoTools::CAtmoTools(void)
  18. {
  19. }
  20. CAtmoTools::~CAtmoTools(void)
  21. {
  22. }
  23. void CAtmoTools::ShowShutdownColor(CAtmoDynData *pDynData)
  24. {
  25.     pDynData->LockCriticalSection();
  26.     CAtmoConnection *atmoConnection = pDynData->getAtmoConnection();
  27.     CAtmoConfig *atmoConfig = pDynData->getAtmoConfig();
  28.     if((atmoConnection != NULL) && (atmoConfig!=NULL)) {
  29.        int r[ATMO_NUM_CHANNELS],g[ATMO_NUM_CHANNELS],b[ATMO_NUM_CHANNELS],i;
  30.        // set a special color? on shutdown of the software? mostly may use black or so ...
  31.        // if this function ist disabled ... atmo will continuing to show the last color...
  32.        if(atmoConnection->isOpen() == ATMO_TRUE) {
  33.         if(atmoConfig->isSetShutdownColor() == 1) {
  34.         for(i=0;i<ATMO_NUM_CHANNELS;i++) {
  35.         r[i] = atmoConfig->getShutdownColor_Red();
  36.         g[i] = atmoConfig->getShutdownColor_Green();
  37.         b[i] = atmoConfig->getShutdownColor_Blue();
  38.         }
  39.                 atmoConnection->SendData(ATMO_NUM_CHANNELS,r,g,b);
  40.         }
  41.        }
  42. }
  43.     pDynData->UnLockCriticalSection();
  44. }
  45. EffectMode CAtmoTools::SwitchEffect(CAtmoDynData *pDynData, EffectMode newEffectMode)
  46. {
  47.     // may need a critical section??
  48.     if(pDynData == NULL) {
  49.        return emUndefined;
  50.     }
  51.     pDynData->LockCriticalSection();
  52.     CAtmoConfig *atmoConfig = pDynData->getAtmoConfig();
  53.     if(atmoConfig == NULL) {
  54.        pDynData->UnLockCriticalSection();
  55.        return emUndefined;
  56.     }
  57.     CAtmoConnection *atmoConnection = pDynData->getAtmoConnection();
  58.     EffectMode oldEffectMode = atmoConfig->getEffectMode();
  59.     CThread *currentEffect = pDynData->getEffectThread();
  60.     // stop and delete/cleanup current Effect Thread...
  61.     pDynData->setEffectThread(NULL);
  62.     if(currentEffect!=NULL) {
  63.        currentEffect->Terminate();
  64.        delete currentEffect;
  65.        currentEffect = NULL;
  66.     }
  67.     if((atmoConnection!=NULL) && (atmoConnection->isOpen()==ATMO_TRUE)) {
  68.         // neuen EffectThread nur mit aktiver Connection starten...
  69.         switch(newEffectMode) {
  70.             case emDisabled:
  71.                 break;
  72.             case emStaticColor:
  73.                     // get values from config - and put them to all channels?
  74.                     int r[ATMO_NUM_CHANNELS],g[ATMO_NUM_CHANNELS],b[ATMO_NUM_CHANNELS];
  75.                     for(int i=0;i<ATMO_NUM_CHANNELS;i++) {
  76.                         r[i] = (atmoConfig->getStaticColor_Red()   * atmoConfig->getWhiteAdjustment_Red())/255;
  77.                         g[i] = (atmoConfig->getStaticColor_Green() * atmoConfig->getWhiteAdjustment_Green())/255;
  78.                         b[i] = (atmoConfig->getStaticColor_Blue()  * atmoConfig->getWhiteAdjustment_Blue())/255;
  79.                     }
  80.                     atmoConnection->SendData(ATMO_NUM_CHANNELS,r,g,b);
  81.                 break;
  82.             case emLivePicture:
  83.                 currentEffect = new CAtmoLiveView(pDynData);
  84.                 break;
  85. #if !defined(_ATMO_VLC_PLUGIN_)
  86.             case emColorChange:
  87.                 currentEffect = new CAtmoColorChanger(atmoConnection, atmoConfig);
  88.                 break;
  89. #endif
  90. #if !defined(_ATMO_VLC_PLUGIN_)
  91.             case emLrColorChange:
  92.                 currentEffect = new CAtmoLeftRightColorChanger(atmoConnection, atmoConfig);
  93.                 break;
  94. #endif
  95.         }
  96.     }
  97.     atmoConfig->setEffectMode(newEffectMode);
  98.     pDynData->setEffectThread(currentEffect);
  99.     if(currentEffect!=NULL)
  100.        currentEffect->Run();
  101.     pDynData->UnLockCriticalSection();
  102.     return oldEffectMode;
  103. }
  104. ATMO_BOOL CAtmoTools::RecreateConnection(CAtmoDynData *pDynData)
  105. {
  106.     pDynData->LockCriticalSection();
  107.     CAtmoConnection *current = pDynData->getAtmoConnection();
  108.     AtmoConnectionType act = pDynData->getAtmoConfig()->getConnectionType();
  109.     pDynData->setAtmoConnection(NULL);
  110.     if(current != NULL) {
  111.        current->CloseConnection();
  112.        delete current;
  113.     }
  114.     switch(act) {
  115.            case actSerialPort: {
  116.                CAtmoSerialConnection *tempConnection = new CAtmoSerialConnection(pDynData->getAtmoConfig());
  117.                if(tempConnection->OpenConnection() == ATMO_FALSE) {
  118. #if !defined(_ATMO_VLC_PLUGIN_)
  119.                   char errorMsgBuf[200];
  120.                   sprintf(errorMsgBuf,"Failed to open serial port com%d with errorcode: %d (0x%x)",
  121.                             pDynData->getAtmoConfig()->getComport(),
  122.                             tempConnection->getLastError(),
  123.                             tempConnection->getLastError()
  124.                         );
  125.                   MessageBox(0,errorMsgBuf,"Error",MB_ICONERROR | MB_OK);
  126. #endif
  127.                   delete tempConnection;
  128.                   pDynData->UnLockCriticalSection();
  129.                   return ATMO_FALSE;
  130.                }
  131.                pDynData->setAtmoConnection(tempConnection);
  132.                CAtmoTools::SetChannelAssignment(pDynData,
  133.                            pDynData->getAtmoConfig()->getCurrentChannelAssignment());
  134.                pDynData->UnLockCriticalSection();
  135.                return ATMO_TRUE;
  136.            }
  137. #if !defined(_ATMO_VLC_PLUGIN_)
  138.            case actDummy: {
  139.                CAtmoDummyConnection *tempConnection = new CAtmoDummyConnection(pDynData->getHinstance(),
  140.                                                                                pDynData->getAtmoConfig());
  141.                if(tempConnection->OpenConnection() == ATMO_FALSE) {
  142.                   delete tempConnection;
  143.                   pDynData->UnLockCriticalSection();
  144.                   return ATMO_FALSE;
  145.                }
  146.                pDynData->setAtmoConnection(tempConnection);
  147.                CAtmoTools::SetChannelAssignment(pDynData, pDynData->getAtmoConfig()->getCurrentChannelAssignment());
  148.                pDynData->UnLockCriticalSection();
  149.                return ATMO_TRUE;
  150.            }
  151.            case actDMX: {
  152.                // create here your DMX connections... instead of the dummy....
  153.                CAtmoDmxSerialConnection *tempConnection = new CAtmoDmxSerialConnection(pDynData->getAtmoConfig());
  154.                if(tempConnection->OpenConnection() == ATMO_FALSE) {
  155.                   delete tempConnection;
  156.                   pDynData->UnLockCriticalSection();
  157.                   return ATMO_FALSE;
  158.                }
  159.                pDynData->setAtmoConnection(tempConnection);
  160.                CAtmoTools::SetChannelAssignment(pDynData, pDynData->getAtmoConfig()->getCurrentChannelAssignment());
  161.                pDynData->UnLockCriticalSection();
  162.                return ATMO_TRUE;
  163.            }
  164. #endif
  165.            default: {
  166.                pDynData->UnLockCriticalSection();
  167.                return ATMO_FALSE;
  168.            }
  169.     }
  170. }
  171. tColorPacket CAtmoTools::WhiteCalibration(CAtmoConfig *pAtmoConfig, tColorPacket ColorPacket)
  172. {
  173.     int w_adj_red   = pAtmoConfig->getWhiteAdjustment_Red();
  174.     int w_adj_green = pAtmoConfig->getWhiteAdjustment_Green();
  175.     int w_adj_blue  = pAtmoConfig->getWhiteAdjustment_Blue();
  176.     for (int i = 0; i < ATMO_NUM_CHANNELS; i++)  {
  177.         ColorPacket.channel[i].r = (unsigned char)(((int)w_adj_red   * (int)ColorPacket.channel[i].r)   / 255);
  178.         ColorPacket.channel[i].g = (unsigned char)(((int)w_adj_green * (int)ColorPacket.channel[i].g) / 255);
  179.         ColorPacket.channel[i].b = (unsigned char)(((int)w_adj_blue  * (int)ColorPacket.channel[i].b)  / 255);
  180.     }
  181.     return ColorPacket;
  182. }
  183. tColorPacket CAtmoTools::ApplyGamma(CAtmoConfig *pAtmoConfig, tColorPacket ColorPacket)
  184. {
  185.   return ColorPacket;
  186. }
  187. int CAtmoTools::SetChannelAssignment(CAtmoDynData *pDynData, int index)
  188. {
  189.     CAtmoConfig *pAtmoConfig = pDynData->getAtmoConfig();
  190.     CAtmoConnection *pAtmoConnection = pDynData->getAtmoConnection();
  191.     int oldIndex = pAtmoConfig->getCurrentChannelAssignment();
  192.     tChannelAssignment *ca = pAtmoConfig->getChannelAssignment(index);
  193.     if((ca!=NULL) && (pAtmoConnection!=NULL)) {
  194.         pAtmoConnection->SetChannelAssignment(ca);
  195.         pAtmoConfig->setCurrentChannelAssignment(index);
  196.     }
  197.     return oldIndex;
  198. }
  199. #if !defined(_ATMO_VLC_PLUGIN_)
  200. void CAtmoTools::SaveBitmap(HDC hdc,HBITMAP hBmp,char *fileName) {
  201.      BITMAPINFO bmpInfo;
  202.      BITMAPFILEHEADER  bmpFileHeader;
  203.      ZeroMemory(&bmpInfo, sizeof(BITMAPINFO));
  204.      bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  205.      GetDIBits(hdc,hBmp,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
  206.      if(bmpInfo.bmiHeader.biSizeImage<=0)
  207.         bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth * abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
  208.      void *pBuf = malloc(bmpInfo.bmiHeader.biSizeImage);
  209.      bmpInfo.bmiHeader.biCompression=BI_RGB;
  210.      GetDIBits(hdc,hBmp,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);
  211.      bmpFileHeader.bfReserved1=0;
  212.      bmpFileHeader.bfReserved2=0;
  213.      bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
  214. #ifdef _ATMO_VLC_PLUGIN_
  215.      bmpFileHeader.bfType = VLC_TWOCC('M','B');
  216. #else
  217.      bmpFileHeader.bfType = MakeWord('M','B');
  218. #endif
  219.      bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
  220.      FILE *fp = NULL;
  221.      fp = fopen(fileName,"wb");
  222.      fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
  223.      fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
  224.      fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
  225.      fclose(fp);
  226. }
  227. #endif