Color and blinking

Alright so I done most of this, i need to Put the output in color and it should be in color. And when it says blinking, the text should be blinking.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <windows.h>

using namespace std;
/* prototype (function typedef) for DLL function Oup32fp: */

     typedef void (_stdcall *oupfuncPtr)(short portaddr, short datum);

#define PPORT_BASE 0x378 // identify port address

// After successful initialization, this variable will contain function pointer.
     oupfuncPtr oup32fp;

// Wrapper function for the function pointer - call these functions to perform I/O.

     void  Out32 (short portaddr, short datum)
     {
          (oup32fp)(portaddr,datum);
     }

HINSTANCE hLib;

int initialize()
{
     /* Load the library */
     hLib = LoadLibrary("inpout32.dll");
     if (hLib == NULL) {
          fprintf(stderr,"LoadLibrary Failed.\n");
          getch();
          return -1;
                      }
    /* get the address of the function */
     oup32fp = (oupfuncPtr) GetProcAddress(hLib, "Out32");

     if (oup32fp == NULL) {
          fprintf(stderr,"GetProcAddress for Oup32 Failed.\n");
          getch();
          return -1;
     }
     return 0;
}
// ---------------- DO NOT CHANGE ANY OF THE ABOVE CODE --------------------------

int main()
{
     short value; // smaller integer type for port data
     int port = PPORT_BASE; // memory address for parallel port
     initialize(); // call function to set up port for output

     for (int counter = 1; counter <= 5; counter ++)
     {
         Out32(port,9);// Turn North/South GREEN & Hold WEST/EAST RED
         cout << "---North & South = ON          East & West = OFF---\n";
         Sleep(3000);// Holds Green Light for 3 sec

         Out32(port,4);// WEST/EAST  RED and NORTH/SOUTH YELLOW
         cout << "---North & South = BLINKING         East & West = OFF---\n";
         Sleep(1000);//Holds YELLOW NORTH/SOUTH

         Out32(port,3); // Turn all light RED
         cout << "---North & South = OFF            East & West = OFF---\n";
         Sleep(500); // Hold RED half sec

         Out32(port,34);// Turn EAST/ WESTGREEN GREEB AND NORTH/SOUTH RED
         cout << "---North & South = OFF            East & West = ON---\n";
         Sleep(3000);// Holds Green Light for 3 sec

         Out32(port,18);// Turns all lights RED and EAST/WEST YELLOW
         cout << "---North & South = OFF            East & West = BLINKING---\n";
         Sleep(2000);//Holds YELLOW EAST/WEST

         Out32(port,3); // Turn all light RED
         cout << "---North & South = OFF            East & West = OFF---\n";
         cout << "" << endl;
         cout << "Times ran = " << counter << endl;
         Sleep(500); // Hold RED half sec
     }


     FreeLibrary(hLib);
     cout << endl;
     system("PAUSE");
     return(0);
}
I've achieved color output easily enough, and can make text blink by altering it's color from whatever it was to 0 and back within a loop. For simplification of color output, a simple function will do the job.
1
2
3
4
5
6
void Color(int color)
{
   //sample usage: Color(7); (7 is default color)
   HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hCon,color);
}


Color(10) is green, and Color(12) is red. Depending on what you want to do, you can potentially have it alternate from Color(12) to Color(0) and back within a loop for a specified duration, or until a specific key is pressed which breaks the loop, and Sleep can be used to set how fast the blinking is. Some prefer enums for various colors, but I don't really see the advantage as there are only 15 values aside from 0 which apply a black background, therefore the most desired colors can be found at values 1-15.

For all the color codes, check out this link.
https://imagizer.imageshack.us/v2/682x421q50/594/vmfy.jpg

The hosting site seems to have robbed the pic of some of it's clarity, but you should get the idea.
Last edited on
Topic archived. No new replies allowed.