Serie: C++
c
155 líneas
· Actualizado 2026-07-21
deviceAlpha.h
C++/Mavis_Homework/FinalProject/deviceAlpha.h
/*
* deviceAlpha.h -- PC simulator for the Seneca "Device Alpha" board
* (Final Project version: adds 4 push buttons and a display-clear call)
*
* This header lets finalproject.c compile and run on a normal computer:
* - LED / BUZZER state changes are printed as [SIM] messages
* - the 7-Segment display is printed as [7SEG] messages
* - GetTemperature() returns a realistic room temperature with noise
* - the four push buttons PB1..PB4 are simulated through the keyboard:
* when the program polls the buttons and no press is pending, the
* simulator reads one line from standard input; typing 1, 2, 3 or 4
* (then Enter) simulates pressing and releasing that button.
*
* On the real lab computer, delete this file and include the official
* deviceAlpha.h that comes with the Device Alpha software instead.
*/
#ifndef DEVICE_ALPHA_H
#define DEVICE_ALPHA_H
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
/* ---- device identifiers ---- */
#define BLUE_LED 0
#define GREEN_LED 1
#define YELLOW_LED 2
#define RED_LED 3
#define BUZZER 0
#define TEMPERATURE_SENSOR 0
#define PB1 1
#define PB2 2
#define PB3 3
#define PB4 4
/* ---- states ---- */
#define OFF 0
#define ON 1
#define RELEASED 0
#define PRESSED 1
static const char *simLedNames[] = { "BLUE", "GREEN", "YELLOW", "RED" };
static void SetLEDState(int led, int state)
{
printf(" [SIM] %s LED %s\n", simLedNames[led], state == ON ? "ON" : "OFF");
}
static void SetBuzzerState(int buzzer, int state)
{
(void)buzzer;
printf(" [SIM] BUZZER %s\n", state == ON ? "ON" : "OFF");
}
static void DisplayIntValue(int value)
{
printf(" [7SEG] %d\n", value);
}
static void DisplayDoubleValue(double value, int decimals)
{
printf(" [7SEG] %.*f\n", decimals, value);
}
static void ClearSevenSegmentDisplay(void)
{
printf(" [7SEG] (cleared)\n");
}
static void Wait(int milliseconds)
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep((useconds_t)milliseconds * 1000);
#endif
}
static double GetTemperature(int sensor)
{
(void)sensor;
/* room temperature about 22 degrees with small noise */
return 22.0 + ((rand() % 41) - 20) / 100.0;
}
/*
* Simulated push buttons.
*
* simPendingButton : which button (1..4) is currently "pressed"
* simPressPollsLeft : how many more polls of that button report PRESSED
*
* When every button is released and the program polls any button, the
* simulator blocks and reads one line from stdin -- exactly like a real
* system idling until the user presses something.
*/
static int simPendingButton = 0;
static int simPressPollsLeft = 0;
static void simReadNextButtonEvent(void)
{
char inputLine[64];
printf(" [SIM] (waiting for a button: type 1=PB1 2=PB2 3=PB3 4=PB4)\n");
if (fgets(inputLine, sizeof inputLine, stdin) == NULL)
{
printf(" [SIM] (end of input - stopping simulation)\n");
exit(0);
}
if (inputLine[0] >= '1' && inputLine[0] <= '4')
{
simPendingButton = inputLine[0] - '0';
simPressPollsLeft = 8;
printf(" [SIM] PB%d pressed\n", simPendingButton);
}
/* anything else: ignored, all buttons stay released this round */
}
static int GetPushButtonState(int button)
{
int pressedNow;
if (simPendingButton == 0)
{
simReadNextButtonEvent();
}
if (simPendingButton == 0)
{
return RELEASED;
}
/* the press lasts a fixed number of polls, whichever button is polled,
so a press that the current state ignores expires on its own */
simPressPollsLeft--;
pressedNow = (button == simPendingButton && simPressPollsLeft > 0);
if (simPressPollsLeft <= 0)
{
printf(" [SIM] PB%d released\n", simPendingButton);
simPendingButton = 0;
}
return pressedNow ? PRESSED : RELEASED;
}
#endif /* DEVICE_ALPHA_H */
Artículos relacionados
C++
c
Actualizado 2026-07-21
finalproject.c
finalproject.c — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.c).
Leer artículo →
C++
cpp
Actualizado 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
Leer artículo →
C++
c
Actualizado 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/deviceAlpha.h).
Leer artículo →
C++
c
Actualizado 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
Leer artículo →
C++
cpp
Actualizado 2026-07-21
lab8.cpp
lab8.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.cpp).
Leer artículo →
C++
cpp
Actualizado 2026-04-03
basic_output.cpp
basic_output.cpp — cpp source code from the C++ learning materials (C++/Part1_基礎入門/Ch01_認識CPP與開發環境/basic_output.cpp).
Leer artículo →