Problem 2. Consider an 8-light lighting system that is controlled by an array of
ID: 3743165 • Letter: P
Question
Problem 2. Consider an 8-light lighting system that is controlled by an array of 8-bit registers. The first element of the array controls light #1, the second element of the array controls light #2, and so on. We call these registers Light Status Register or LSR. For each LSR, the bits control the light based on the below specification Bit 7-6: control the light intensity: 00 off, 01 low, 10 medium, 11 high. Bit 5-4: control the color: blue 00, green 01, red 10, yellow 11. Bit 3 controls whether the light flickers: 0 no flicker, 1 flicker Bit 2-0 are unused .Explanation / Answer
I am pasting the code here, please copy paste it in lightsystem.c file and then compile and run with following commands.
Compile Command : gcc -o light lightsystem.c
Run Command : ./light
The CODE:
#include<stdio.h>
#include<stdlib.h>
/* Answer C in next 3 lines */
#define INTENSITY_MASK(I) ((I>>6) & 3)
#define COLOR_MASK(C) ((C>>4) & 3)
#define FLICKER_MASK(F) ((F>>3) & 1)
/* Answer D in next 3 lines */
typedef enum {BLUE=0, GREEN=1, RED=2, YELLOW=3}COLOR;
typedef enum {OFF=0, LOW_INTENSITY=1, MED_INTENSITY=2, HIGH_INTENSITY=3}INTENSITY;
typedef enum {NO_FLICKER=0, FLICKER=1}FLICKERING;
/* Answer E */
COLOR extractColor(unsigned int LSR)
{
int col;
col = COLOR_MASK(LSR);
switch(col)
{
case 0:
printf("BLUE ");
return BLUE;
case 1:
printf("GREEN ");
return GREEN;
case 2:
printf("RED ");
return RED;
case 3:
printf("YELLOW ");
return YELLOW;
}
}
/* Answer B*/
void extractMeaning(unsigned int LSR)
{
int col;
col = COLOR_MASK(LSR);
switch(col)
{
case 0:
printf("BLUE ");
break;
case 1:
printf("GREEN ");
break;
case 2:
printf("RED ");
break;
case 3:
printf("YELLOW ");
break;
}
col = INTENSITY_MASK(LSR);
switch(col)
{
case 0:
printf("OFF ");
break;
case 1:
printf("LOW INTENSITY");
break;
case 2:
printf("MED_INTENSITY ");
break;
case 3:
printf("HIGH INTENSITY ");
break;
}
col = FLICKER_MASK(LSR);
switch(col)
{
case 0:
printf("NO FLICKER ");
break;
case 1:
printf("With FLICKER ");
break;
}
}
/* Answer F*/
unsigned int makeLSR(INTENSITY newIntensity, COLOR newColor, FLICKERING newFlickering)
{
int LSR;
LSR = (newIntensity<<6) | (newColor<<4) | (newFlickering<<3);
return LSR;
}
void main()
{
COLOR col = extractColor(0xff);
int LSR_ARRAY[8] = {0xff,0xab,0x90,0xad,0x88,0x89,0x77,0x99};
int i, temp;
int lsr = makeLSR(MED_INTENSITY, BLUE, NO_FLICKER);
printf("Make LSR : %X ",lsr);
//Answer A
lsr = makeLSR(HIGH_INTENSITY, GREEN, NO_FLICKER);
printf("Make LSR in HEXADECIMAL: %X ",lsr);
printf("Make LSR in Binary : ");
temp = lsr;
for(i=7;i>=0;i--)
{
printf("%d", (temp>>i & 1));
}
printf(" ");
//Answer B
extractMeaning(0xAD);
// Answer G starts from here and till the end of main
printf("Before Change in LSR ARRAY : ");
for(i=0;i<8;i++)
{
printf("LSR_ARRAY[%d] = %X ",i,LSR_ARRAY[i]);
}
for(i=0;i<8;i++)
{
temp = INTENSITY_MASK(LSR_ARRAY[i]);
if(temp != HIGH_INTENSITY)
{
temp = temp+1;
lsr = (temp<<6) | (COLOR_MASK(LSR_ARRAY[i])<<4) | (FLICKER_MASK(LSR_ARRAY[i])<<3);
LSR_ARRAY[i] = lsr;
}
}
printf("After Change in LSR ARRAY : ");
for(i=0;i<8;i++)
{
printf("LSR_ARRAY[%d] = %X ",i,LSR_ARRAY[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.