Series: C++
c
115 lines
· Updated 2026-07-21
deviceAlpha.h
C++/Mavis_Homework/Lab8/deviceAlpha.h
/*
* deviceAlpha.h -- PC simulator for the Seneca "Device Alpha" board
*
* This header lets lab8.c compile and run on a normal computer (no
* hardware needed). It prints what the board would do:
* - LED / HEATER / BUZZER state changes are printed as [SIM] messages
* - the 7-Segment display is printed as [7SEG] messages
* - GetTemperature() returns a realistic value that rises while the
* heater is on, with a little random noise
* - GetPushButtonState() automatically simulates a press-then-release
* after a few polls, so the wait loops behave like a real button
*
* On the real lab computer, delete this file and include the official
* deviceAlpha.h that comes with the Device Alpha software instead;
* lab8.c itself does not need any change.
*/
#ifndef DEVICE_ALPHA_H
#define DEVICE_ALPHA_H
#include <stdio.h>
#include <stdlib.h>
#include <time.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 HEATER 0
#define BUZZER 0
#define TEMPERATURE_SENSOR 0
#define ANY_PUSHBUTTON 0
/* ---- states ---- */
#define OFF 0
#define ON 1
#define RELEASED 0
#define PRESSED 1
static const char *simLedNames[] = { "BLUE", "GREEN", "YELLOW", "RED" };
static int simHeaterOn = 0;
static double simTemperature = 21.0; /* room temperature at start */
static void SetLEDState(int led, int state)
{
printf(" [SIM] %s LED %s\n", simLedNames[led], state == ON ? "ON" : "OFF");
}
static void SetHeaterState(int heater, int state)
{
(void)heater;
simHeaterOn = (state == ON);
printf(" [SIM] HEATER %s\n", 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 Wait(int milliseconds)
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep((useconds_t)milliseconds * 1000);
#endif
}
static double GetTemperature(int sensor)
{
(void)sensor;
if (simTemperature < 45.0 && simHeaterOn)
simTemperature += 0.35; /* heater warms things up */
/* add a little sensor noise: -0.25 .. +0.25 degrees */
return simTemperature + ((rand() % 51) - 25) / 100.0;
}
/*
* Simulated push button: stays RELEASED for the first few polls, then
* PRESSED for a few polls, then RELEASED again -- exactly the sequence
* the press-and-release wait loops in lab8.c expect.
*/
static int GetPushButtonState(int button)
{
static int pollCount = 0;
(void)button;
if (pollCount == 0)
printf(" [SIM] (waiting for a button... auto-pressing)\n");
pollCount++;
Wait(30);
return (pollCount % 8 < 4) ? RELEASED : PRESSED;
}
#endif /* DEVICE_ALPHA_H */
Related articles
C++
c
Updated 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/deviceAlpha.h).
Read article →
C++
c
Updated 2026-07-21
finalproject.c
finalproject.c — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.c).
Read article →
C++
cpp
Updated 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
Read article →
C++
c
Updated 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
Read article →
C++
cpp
Updated 2026-07-21
lab8.cpp
lab8.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.cpp).
Read article →
C++
cpp
Updated 2026-04-03
basic_output.cpp
basic_output.cpp — cpp source code from the C++ learning materials (C++/Part1_基礎入門/Ch01_認識CPP與開發環境/basic_output.cpp).
Read article →