home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / LED / LEDWIND.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-09  |  9.5 KB  |  342 lines

  1. //
  2. // LED Window to Display Numbers
  3. //
  4. // Chris Hewitt, 100036,133 
  5. //
  6. // Displays red LEDs on a black background. The chiseled surrounding is
  7. // optional and is designed to fit on a GRAY background. Setting bSurround
  8. // to TRUE adds 2 pixels to height and width. Height is 24, width is 13 *
  9. // number of digits (not counting the surround).
  10. //
  11. // If bClock is TRUE, the LEDWindow becomes an HH:MM:SS clock, and
  12. // lLEDValue should be a time variable (seconds since 1st Jan 1970,
  13. // or, to be more exact, seconds since -05:00 on 0th Jan 1970!), just
  14. // feed it a time value, or, for an elapsed time counter, a number.
  15. //
  16. // The maximum size of an LEDWindow is 10 Digits.
  17. //
  18. // StarTimer and StopTimer may be used to make the LED register
  19. // time intervals between 1 and 60 seconds. The LED will be incremented
  20. // by one for each interval.
  21. //
  22. // The Bitmaps led0.bmp thru led9.bmp must be declared in the resource
  23. // file as LED0Bmp, LED1Bmp etc. (see example).
  24. //
  25.  
  26.  
  27. class LEDDigit : public TWindow
  28. {
  29. public:
  30.   LEDDigit( PTWindowsObject AParent, int DigitXLoc, BOOL bSurround,
  31.         HBITMAP hLED0Bmp );
  32.   void UpdateDigit( HBITMAP hLEDBmp );
  33.   virtual void Paint( HDC PaintDC, PAINTSTRUCT _FAR &PaintInfo );
  34.   HBITMAP hCurrentLEDBmp;        // Digit Currently in Window
  35. };
  36.  
  37.  
  38. class LEDWindow : public TWindow
  39. {
  40. public:
  41.   LEDWindow( PTWindowsObject AParent, int x, int y,
  42.         int nDigits, BOOL bSurround, BOOL bClock, LONG lValue );
  43.   ~LEDWindow( void );
  44.   void GetWindowClass(WNDCLASS& AWndClass);
  45.   void DisplayNumber( LONG lValue );
  46.   BOOL StartTimer( int nInterval );
  47.   void StopTimer( void );
  48.   void SetupLEDs( void );
  49.   virtual void Tick( RTMessage Msg ) = [WM_TIMER];
  50.   virtual void Paint(HDC PaintDC, PAINTSTRUCT&);
  51.   HBITMAP hLEDBmp[11];        // Bitmaps for LED numbers (0 - 9 + :)
  52.   BOOL bTimerActive;        // Switch indicating if this is a timer
  53.   BOOL bSurroundExists;        // True if Surround exists
  54.   BOOL bClockWindow;        // True if this is a Clock
  55.   BOOL bLEDCreated;             // First Time Switch
  56.   LONG lLEDValue;        // The Current Value of this LED window
  57.   LONG lClockWork;        // Used to store altered time values
  58.   struct tm *tmLEDValue;    // Time Struct for Clock Windows
  59.   int  nOldDigit[10];             // Old LED Digits
  60.   int  nMaxDigits;        // Maximum Number of Digits
  61.   int  nTimerInterval;        // The current Timer interval
  62.   int  nDigitStart;        // Start location of LED Digits in Window
  63.   int  nRow, nCol;        // The parent window offset of LED Window
  64.   LEDDigit *hLED[10];        // Handle array for LED Digit Children
  65.   //
  66.   // Note: LEDDigits are right to left: *hLED[0] (and nOldDigit[0]) is
  67.   //       the right-most (ones) digit.
  68.   //
  69.  
  70. };
  71.  
  72.  
  73. // LEDWindow Constructor
  74. // Called with parent window handle, int x and y offsets in parent,
  75. // int number of digits, and BOOL surround setting.
  76.  
  77. LEDWindow::LEDWindow( PTWindowsObject AParent, int x, int y,
  78.   int nDigits, BOOL bSurround, BOOL bClock, LONG lValue) :
  79.   TWindow( AParent, "" )
  80. {
  81.   lLEDValue = lValue;
  82.   if( bClock )
  83.   {
  84.     nDigits = 8;            // A clock HH:MM:SS
  85.     lClockWork = lLEDValue;        // Work field for localtime
  86.     if(lClockWork<86400l)        // Time is actually since -05:00
  87.       lClockWork = lClockWork + 18000l;    // on 0th Jan 1970 !
  88.     tmLEDValue = localtime(&lClockWork); // Point struct at Value
  89.   }
  90.  
  91.   if( nDigits>10 )
  92.     nDigits = 10;
  93.  
  94.   Attr.W = 13*nDigits;
  95.   Attr.H = 24;
  96.   nDigitStart = Attr.W;
  97.   if( bSurround )
  98.   {
  99.     Attr.W = Attr.W + 2;
  100.     Attr.H = Attr.H + 2;
  101.     nDigitStart--;
  102.   }
  103.   Attr.X = y;             // x and y are reversed because
  104.   Attr.Y = x;             // of screen coordinates
  105.   Attr.Style = WS_VISIBLE|WS_CHILD;
  106.  
  107.   hLEDBmp[0] = LoadBitmap ( GetApplication()->hInstance, "LED0Bmp" );
  108.   hLEDBmp[1] = LoadBitmap ( GetApplication()->hInstance, "LED1Bmp" );
  109.   hLEDBmp[2] = LoadBitmap ( GetApplication()->hInstance, "LED2Bmp" );
  110.   hLEDBmp[3] = LoadBitmap ( GetApplication()->hInstance, "LED3Bmp" );
  111.   hLEDBmp[4] = LoadBitmap ( GetApplication()->hInstance, "LED4Bmp" );
  112.   hLEDBmp[5] = LoadBitmap ( GetApplication()->hInstance, "LED5Bmp" );
  113.   hLEDBmp[6] = LoadBitmap ( GetApplication()->hInstance, "LED6Bmp" );
  114.   hLEDBmp[7] = LoadBitmap ( GetApplication()->hInstance, "LED7Bmp" );
  115.   hLEDBmp[8] = LoadBitmap ( GetApplication()->hInstance, "LED8Bmp" );
  116.   hLEDBmp[9] = LoadBitmap ( GetApplication()->hInstance, "LED9Bmp" );
  117.   hLEDBmp[10] = LoadBitmap ( GetApplication()->hInstance, "LEDColBmp" );
  118.  
  119.   bTimerActive = FALSE;
  120.   bLEDCreated = FALSE;
  121.   bSurroundExists = bSurround;
  122.   bClockWindow = bClock;
  123.   nTimerInterval = 0;
  124.   nMaxDigits = nDigits;
  125.   SetupLEDs();            // Set up initial LED values
  126.   bLEDCreated = TRUE;
  127. }
  128.  
  129. // LEDWindow Destructor
  130. LEDWindow::~LEDWindow( void )
  131. {
  132.   for( int i=0; i<10; i++ )
  133.     DeleteObject( hLEDBmp[i] );
  134. }
  135.  
  136. // Modify the background colour attribute to GRAY
  137. void LEDWindow::GetWindowClass(WNDCLASS& AWndClass)
  138. {
  139.   HBRUSH BackBrush;
  140.  
  141.   TWindow::GetWindowClass(AWndClass);
  142.   BackBrush = GetStockObject(GRAY_BRUSH);
  143.   AWndClass.hbrBackground = BackBrush;
  144. }
  145.  
  146. // DisplayNumber - Accept a number to Display
  147. void LEDWindow::DisplayNumber( LONG lValue )
  148. {
  149.   lLEDValue = lValue;
  150.   if( bClockWindow )
  151.   {
  152.     lClockWork = lLEDValue;        // Work field for localtime
  153.     if(lClockWork<86400l)        // Handle 0th Jan 1970
  154.       lClockWork = lClockWork + 18000l;    // Add 5 hours
  155.     tmLEDValue = localtime(&lClockWork); // Point struct at Value
  156.   }
  157.   SetupLEDs();
  158. }
  159.  
  160. // StartTimer - Used to Start an LED Timer Window
  161. BOOL LEDWindow::StartTimer( int nInterval)
  162. {
  163.   if( nInterval == 0 || nInterval > 60)
  164.   {
  165.     return(FALSE);
  166.   }
  167.   // Set Timer (pass interval to SetTimer in milliseconds)
  168.   if(!SetTimer(HWindow, 1, (WORD) nInterval*1000, NULL))
  169.   {
  170.     return(FALSE);
  171.   }
  172.   else
  173.   {
  174.     bTimerActive = TRUE;
  175.     nTimerInterval = nInterval;
  176.     return(TRUE);
  177.   }
  178. }
  179.  
  180. // StopTimer - Stops a previously started LED timer window
  181. void LEDWindow::StopTimer(void)
  182. {
  183.   if(bTimerActive==TRUE)
  184.   {
  185.     KillTimer(HWindow, 1);
  186.     bTimerActive=FALSE;
  187.     nTimerInterval = 0;
  188.   }
  189. }
  190.  
  191. // Tick - Called for each WM_TIMER received by this LEDWindow
  192. void LEDWindow::Tick(RTMessage)
  193. {
  194.   lLEDValue = lLEDValue + 1;
  195.   if(bClockWindow)
  196.   {
  197.     lClockWork = lClockWork + 1;           // Work field for localtime
  198.     tmLEDValue = localtime(&lClockWork);       // Point struct at Value
  199.   }
  200.   SetupLEDs();
  201. }
  202.  
  203. // Paint LEDWindow surround
  204. void LEDWindow::Paint(HDC PaintDC, PAINTSTRUCT&)
  205. {
  206.   HPEN OldPen;
  207.   HPEN WhitePen;
  208.   HPEN DkGrayPen;
  209.   BOOL MadeDC;
  210.  
  211.   // Secure the device context
  212.   if ( PaintDC == 0 )
  213.   {
  214.     PaintDC = GetDC( HWindow );
  215.     MadeDC = TRUE;
  216.   }
  217.   else
  218.     MadeDC = FALSE;
  219.  
  220.   // Paint the surround (if required)
  221.   if(bSurroundExists)
  222.   {
  223.     WhitePen = GetStockObject(WHITE_PEN);
  224.     DkGrayPen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
  225.  
  226.     OldPen = SelectObject(PaintDC, DkGrayPen);
  227.     MoveTo( PaintDC, 0, 0);
  228.     LineTo( PaintDC, (nMaxDigits*13)+1, 0);
  229.     MoveTo( PaintDC, 0, 0);
  230.     LineTo( PaintDC, 0, 24);
  231.  
  232.     SelectObject(PaintDC, WhitePen);
  233.     MoveTo( PaintDC, 0, 24);
  234.     LineTo( PaintDC, (nMaxDigits*13)+1, 24);
  235.     MoveTo( PaintDC, (nMaxDigits*13)+1, 0);
  236.     LineTo( PaintDC, (nMaxDigits*13)+1, 25);
  237.  
  238.     SetPixel(PaintDC, (nMaxDigits*13)+1, 0, RGB(192, 192, 192));
  239.     SetPixel(PaintDC, 0, 24, RGB(192, 192, 192));
  240.  
  241.     SelectObject(PaintDC, OldPen);
  242.     DeleteObject(DkGrayPen);
  243.   }
  244.  
  245.   if ( MadeDC )
  246.     ReleaseDC( HWindow, PaintDC );
  247.  
  248. }
  249.  
  250. // SetupLEDS - Work out which LEDDigits need to be replaced,
  251. // then invalidate the little suckers. This is simple except if
  252. // bClock = TRUE, in which case we need to handle ':'s.
  253. void LEDWindow::SetupLEDs( void )
  254. {
  255.   int  nNewDigit;
  256.   LONG lWorkValue;
  257.  
  258.   lWorkValue = lLEDValue;
  259.   for( int i=0; i<nMaxDigits; i++ )
  260.   {
  261.     if(bClockWindow && (i==2 || i==5))
  262.     // Digit 10 is ':', use for positions 2 and 5 of clocks
  263.     nNewDigit = 10;
  264.     else
  265.     {
  266.       if(bClockWindow)
  267.       {
  268.     // Treat Clock as 3 different values
  269.     if(i==0)
  270.       lWorkValue=tmLEDValue->tm_sec;
  271.     if(i==3)
  272.       lWorkValue=tmLEDValue->tm_min;
  273.     if(i==6)
  274.       lWorkValue=tmLEDValue->tm_hour;
  275.       }
  276.       // This will Generate a Warning: this is expected
  277.       nNewDigit = lWorkValue-((lWorkValue/10)*10);
  278.       lWorkValue = lWorkValue/10;
  279.     }
  280.     if(!bLEDCreated)
  281.       hLED[i] = new LEDDigit( this, nDigitStart-(13*(i+1)-1),
  282.                 bSurroundExists, hLEDBmp[nNewDigit]);
  283.     else
  284.       if( nNewDigit != nOldDigit[i] )
  285.     hLED[i]->UpdateDigit(hLEDBmp[nNewDigit]);
  286.     nOldDigit[i] = nNewDigit;
  287.   }
  288. }
  289.  
  290.  
  291. // LEDDigit - Create a new LEDDigit
  292. LEDDigit::LEDDigit( PTWindowsObject AParent, int DigitXLoc, BOOL bSurround,
  293.             HBITMAP hLEDBmp ) :
  294.   TWindow( AParent, "" )
  295. {
  296.   Attr.W = 13;
  297.   Attr.H = 23;
  298.   Attr.Y = 0;
  299.   Attr.X = DigitXLoc;
  300.   if(bSurround)
  301.   {
  302.     Attr.X = Attr.X + 1;
  303.     Attr.Y = Attr.Y + 1;
  304.   }
  305.   Attr.Style = WS_VISIBLE|WS_CHILD;
  306.   hCurrentLEDBmp = hLEDBmp;
  307. }
  308.  
  309. // UpdateDigit - Set the digit in question invalid
  310. void LEDDigit::UpdateDigit(HBITMAP hLEDBmp)
  311. {
  312.   hCurrentLEDBmp = hLEDBmp;
  313.   InvalidateRect(HWindow, NULL, TRUE);
  314. }
  315.  
  316. // Paint - Paint the LEDDigit
  317. void LEDDigit::Paint(HDC PaintDC, PAINTSTRUCT&)
  318. {
  319.   HDC  MemDC;
  320.   BOOL MadeDC;
  321.  
  322.   // Secure the device context
  323.   if ( PaintDC == 0 )
  324.   {
  325.     PaintDC = GetDC( HWindow );
  326.     MadeDC = TRUE;
  327.   }
  328.   else
  329.     MadeDC = FALSE;
  330.  
  331.   MemDC = CreateCompatibleDC( PaintDC );
  332.  
  333.   SelectObject( MemDC, hCurrentLEDBmp);
  334.   BitBlt( PaintDC, 0, 0, 13, 24, MemDC, 0, 0, SRCCOPY );
  335.  
  336.   if ( MadeDC )
  337.     ReleaseDC( HWindow, PaintDC );
  338.  
  339.   DeleteDC( MemDC );
  340. }
  341.  
  342.