Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m supposed to revise the code below to meet the requirements of 2 and 3, than

ID: 2085903 • Letter: I

Question

I'm supposed to revise the code below to meet the requirements of 2 and 3, thanks in advance

2. The user may use the left pushbutton (SW1 or Switch 1) to control the frequency of LED flashing. The starting frequency is 1 Hz, and the frequency should change as follows with each push on SW1: 1Hz => 0.5 Hz => 2 Hz => 1 Hz => 0.5 Hz => 2 Hz … (repeating) Note that 0.5 Hz means the LED will flash every two seconds.

3. The user may use the right pushbutton (SW2 or Switch 2) to control the color of the LED. The starting color is white, and it should change as follows with each push on SW2: white => red => green => blue => white => … (repeat.)

#include <stdint.h>

#include <stdbool.h>

#include <stdio.h>

#include "launchpad.h"

// Event-drive code for flashing LED

void

flashLED(uint32_t time) // the scheduled time

{

static enum {OFF, ON} state = OFF; // LED ON/OFF state

int delay = 1000; // next LED event time

// Flip LED and record state change

switch (state) {

case OFF:

ledTurnOnOff(true, true, true);

state = ON;

break;

case ON:

ledTurnOnOff(false, false, false);

state = OFF;

break;

}

// Schedule the next callback

schdCallback(flashLED, time + delay);

}

// Event driven code for checking push button

void

checkPushButton(uint32_t time)

{

int code = pbRead();

uint32_t delay;

switch (code) {

case 1:

uprintf("%s ", "SW1 is pushed.");

delay = 250; // Use an inertia for soft debouncing

break;

case 2:

uprintf("%s ", "SW2 is pushed");

delay = 250; // Use an inertia for soft debouncing

break;

default:

delay = 10;

}

schdCallback(checkPushButton, time + delay);

}

int main(void)

{

lpInit();

uprintf("%s ", "Welcome to ECE367!");

// Schedule the first callback events for LED flashing and push button checking.

// Those trigger callback chains. The time unit is millisecond.

schdCallback(flashLED, 1000);

schdCallback(checkPushButton, 1005);

// Loop forever

while (true) {

schdExecute();

}

}

Explanation / Answer


#include <stdint.h>

#include <stdbool.h>

#include <stdio.h>

#include "launchpad.h"

// Event-drive code for flashing LED

#define 1_Hz_delay   1000
#define Half_Hz_delay  2000
#define 2_Hz_delay   500

#define Selected_LED_ON(led_state) {led_buffer[4]={OFF,OFF,OFF,OFF}; ; led_buffer[led_state] = ON; ledTurnOnOff(led_buffer[0],led_buffer[1],led_buffer[2],led_buffer[3]);}
#define Selected_LED_OFF(led_state) {led_buffer[4]={OFF,OFF,OFF,OFF};; led_buffer[led_state] = OFF; ledTurnOnOff(led_buffer[0],led_buffer[1],led_buffer[2],led_buffer[3]);}

static enum {OFF = 0, ON} led_buffer[4]={OFF,OFF,OFF,OFF};

static enum {WHITE = 0,RED,GREEN,BLUE} led_state = WHITE; //0
/*
WHITE = 0,
RED = 1,
GREEN = 2,
BLUE = 3
*/
uint32_t led_delay = ;

void flashLED(uint32_t time) // the scheduled time
{

static enum {OFF, ON} state = OFF; // LED ON/OFF state

int delay = 1000; // next LED event time

// Flip LED and record state change

switch (state) {

  case : OFF

  Selected_LED_ON(led_state); // passing selected the

  state = ON;

  break;

  case ON:

  Selected_LED_OFF(led_state);// passing selected the led

  state = OFF;

  break;

}

// Schedule the next callback

schdCallback(flashLED, time + led_delay);
}

// Event driven code for checking push button

void checkPushButton(uint32_t time)
{

int code = pbRead();

uint32_t delay;

switch (code) {

  case 1:

   uprintf("%s ", "SW1 is pushed.");
   
   switch (led_delay){

    case 1_Hz_delay:

     led_delay = Half_Hz_delay;

    break;

    case Half_Hz_delay :

     led_delay = 2_Hz_delay;

    break;

    case 2_Hz_delay:

     led_delay = 1_Hz_delay;

    break;

    default:

     led_delay = 1_Hz_delay;

   }

   delay = 250; // Use an inertia for soft debouncing

  break;

  case 2:

   uprintf("%s ", "SW2 is pushed");
   
   switch (led_state){

    case WHITE:

     led_state = RED;

    break;

    case RED :

     led_state = GREEN;

    case GREEN:

     led_state = BLUE;

    break;

    case BLUE:

     led_state = WHITE;

    break;

    default:

     led_state = WHITE;

   }

   delay = 250; // Use an inertia for soft debouncing

  break;

  default:

   delay = 10;

}

schdCallback(checkPushButton, time + delay);

}

int main(void)
{

lpInit();

uprintf("%s ", "Welcome to ECE367!");

// Schedule the first callback events for LED flashing and push button checking.

// Those trigger callback chains. The time unit is millisecond.

schdCallback(flashLED, 1000);

schdCallback(checkPushButton, 1005);

// Loop forever

while (true) {

  schdExecute();

}

}