chore: Add course material
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2013
|
||||
VisualStudioVersion = 12.0.31101.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Ex33", "Ex33.vcxproj", "{C2EDBD25-9006-0438-220A-8766E6F352A8}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Win32 = Debug|Win32
|
||||
Release|Win32 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{C2EDBD25-9006-0438-220A-8766E6F352A8}.Debug|Win32.ActiveCfg = Debug|Win32
|
||||
{C2EDBD25-9006-0438-220A-8766E6F352A8}.Debug|Win32.Build.0 = Debug|Win32
|
||||
{C2EDBD25-9006-0438-220A-8766E6F352A8}.Release|Win32.ActiveCfg = Release|Win32
|
||||
{C2EDBD25-9006-0438-220A-8766E6F352A8}.Release|Win32.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,141 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<SccProjectName>
|
||||
</SccProjectName>
|
||||
<SccLocalPath>
|
||||
</SccLocalPath>
|
||||
<ProjectGuid>{C2EDBD25-9006-0438-220A-8766E6F352A8}</ProjectGuid>
|
||||
<ProjectName>Ex33</ProjectName>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseOfMfc>false</UseOfMfc>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<PlatformToolset>v140</PlatformToolset>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\Debug\</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">.\Debug\</IntDir>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
|
||||
<OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\Release\</OutDir>
|
||||
<IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">.\Release\</IntDir>
|
||||
<LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
<RuntimeTypeInfo>true</RuntimeTypeInfo>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<PrecompiledHeaderOutputFile>.\Debug/Ex22.pch</PrecompiledHeaderOutputFile>
|
||||
<AssemblerListingLocation>.\Debug/</AssemblerListingLocation>
|
||||
<ObjectFileName>.\Debug/</ObjectFileName>
|
||||
<ProgramDataBaseFileName>.\Debug/</ProgramDataBaseFileName>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SuppressStartupBanner>true</SuppressStartupBanner>
|
||||
<AdditionalIncludeDirectories>C:\course\397\DOS_Unix;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<AdditionalOptions>/MACHINE:I386 %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalDependencies>odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<OutputFile>.\Debug/Ex33.exe</OutputFile>
|
||||
<SuppressStartupBanner>true</SuppressStartupBanner>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<ProgramDatabaseFile>.\Debug/Ex22.pdb</ProgramDatabaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
<Midl>
|
||||
<TypeLibraryName>.\Debug/Ex22.tlb</TypeLibraryName>
|
||||
</Midl>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<Culture>0x0409</Culture>
|
||||
</ResourceCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<StringPooling>true</StringPooling>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<PrecompiledHeader>
|
||||
</PrecompiledHeader>
|
||||
<PrecompiledHeaderOutputFile>.\Release/Ex22.pch</PrecompiledHeaderOutputFile>
|
||||
<AssemblerListingLocation>.\Release/</AssemblerListingLocation>
|
||||
<ObjectFileName>.\Release/</ObjectFileName>
|
||||
<ProgramDataBaseFileName>.\Release/</ProgramDataBaseFileName>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SuppressStartupBanner>true</SuppressStartupBanner>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<AdditionalOptions>/MACHINE:I386 %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalDependencies>odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<OutputFile>.\Release/Ex33.exe</OutputFile>
|
||||
<SuppressStartupBanner>true</SuppressStartupBanner>
|
||||
<ProgramDatabaseFile>.\Release/Ex22.pdb</ProgramDatabaseFile>
|
||||
<SubSystem>Console</SubSystem>
|
||||
</Link>
|
||||
<Midl>
|
||||
<TypeLibraryName>.\Release/Ex22.tlb</TypeLibraryName>
|
||||
</Midl>
|
||||
<ResourceCompile>
|
||||
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<Culture>0x0409</Culture>
|
||||
</ResourceCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Light.cpp" />
|
||||
<ClCompile Include="Pelican.cpp" />
|
||||
<ClCompile Include="Pole.cpp" />
|
||||
<ClCompile Include="Timer.cpp" />
|
||||
<ClCompile Include="TimerSet.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="..\DOS_Unix\Keyboard_DOS.cpp" />
|
||||
<ClCompile Include="..\DOS_Unix\Screen_DOS.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Light.h" />
|
||||
<ClInclude Include="Pelican.h" />
|
||||
<ClInclude Include="Pole.h" />
|
||||
<ClInclude Include="Timer.h" />
|
||||
<ClInclude Include="TimerSet.h" />
|
||||
<ClInclude Include="..\DOS_Unix\Keyboard.h" />
|
||||
<ClInclude Include="..\DOS_Unix\Screen.h" />
|
||||
<ClInclude Include="TimerT.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{17deccef-8801-49d5-8ba1-c5db56fa73c0}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cxx;rc;def;r;odl;idl;hpj;bat</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{eaf1391b-78b1-4db8-adb1-2585eaedd6e7}</UniqueIdentifier>
|
||||
<Extensions>h;hpp;hxx;hm;inl</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="DOS_Unix">
|
||||
<UniqueIdentifier>{f8ec245a-126d-413b-8f31-4f57d4342719}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Light.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Pelican.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Pole.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Timer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="TimerSet.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="main.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\DOS_Unix\Keyboard_DOS.cpp">
|
||||
<Filter>DOS_Unix</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\DOS_Unix\Screen_DOS.cpp">
|
||||
<Filter>DOS_Unix</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Pelican.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Pole.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Timer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TimerSet.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\DOS_Unix\Keyboard.h">
|
||||
<Filter>DOS_Unix</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\DOS_Unix\Screen.h">
|
||||
<Filter>DOS_Unix</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="TimerT.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Light.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
</Project>
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
|
||||
#include "Light.h"
|
||||
#include "Pelican.h"
|
||||
#include "Screen.h"
|
||||
#include <iostream>
|
||||
|
||||
Light::Light(std::string name, int x, int y, int color) : x(x), y(y), c(color), name(name) {}
|
||||
|
||||
Light::~Light() {
|
||||
std::cout << "Deleting light (" << name << ", " << x << ", " << y << ", " << c << ")"
|
||||
<< "\n";
|
||||
}
|
||||
|
||||
void Light::set(LightState newState) {
|
||||
state = newState;
|
||||
Screen::write(name.c_str(), x, y, state == FLASH ? (128 + c) : (state == OFF ? 0 : c));
|
||||
// 128 + color was used to make a character flash (but not any more)
|
||||
}
|
||||
|
||||
Light::LightState Light::get(void) { return state; }
|
||||
@@ -0,0 +1,30 @@
|
||||
// ======================================================
|
||||
//
|
||||
// Represents a light, implemented as a string written
|
||||
// to a console window
|
||||
//
|
||||
// ======================================================
|
||||
|
||||
#ifndef LIGHT_H
|
||||
#define LIGHT_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Light {
|
||||
public:
|
||||
Light(std::string name, int x, int y, int color);
|
||||
~Light();
|
||||
|
||||
enum LightState { ON, OFF, FLASH };
|
||||
|
||||
void set(LightState new_state);
|
||||
LightState get(void);
|
||||
|
||||
private:
|
||||
int c; // light colour
|
||||
int x, y;
|
||||
std::string name;
|
||||
LightState state;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
|
||||
|
||||
#include "Pelican.h"
|
||||
#include "Pole.h"
|
||||
|
||||
Pelican::Pelican(int x, int y)
|
||||
: pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), myPole(new Pole(x, y)),
|
||||
state(GREEN_IDLE) {
|
||||
myTimer = new TimerT<Pelican>(*this, &Pelican::timerExpired);
|
||||
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
}
|
||||
|
||||
Pelican::~Pelican() { delete myPole; }
|
||||
|
||||
void Pelican::press(void) {
|
||||
if (state == GREEN_IDLE) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
pressed = true;
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
} else if (state != RED_STATE) {
|
||||
pressed = true;
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
}
|
||||
}
|
||||
|
||||
void Pelican::timerExpired(void) {
|
||||
switch (state) {
|
||||
case RED_STATE:
|
||||
state = YELLOW_FLASHING;
|
||||
myTimer->timeoutRequest(FLASH_TIME);
|
||||
break;
|
||||
|
||||
case YELLOW_FLASHING:
|
||||
state = GREEN_TIMING;
|
||||
myTimer->timeoutRequest(GREEN_TIME);
|
||||
break;
|
||||
|
||||
case GREEN_TIMING:
|
||||
if (pressed) {
|
||||
state = YELLOW_STATE;
|
||||
myTimer->timeoutRequest(YELLOW_TIME);
|
||||
} else {
|
||||
state = GREEN_IDLE;
|
||||
}
|
||||
break;
|
||||
|
||||
case GREEN_IDLE:
|
||||
// should never happen
|
||||
break;
|
||||
|
||||
case YELLOW_STATE:
|
||||
state = RED_STATE;
|
||||
pressed = false;
|
||||
myTimer->timeoutRequest(RED_TIME);
|
||||
break;
|
||||
}
|
||||
|
||||
myPole->setTrafficLights(getState());
|
||||
myPole->setWaitLight(isPressed());
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// ======================================================
|
||||
//
|
||||
// Represents a Pelican crossing
|
||||
//
|
||||
// ======================================================
|
||||
|
||||
#ifndef PELICAN_H
|
||||
#define PELICAN_H
|
||||
|
||||
#include "TimerT.h"
|
||||
|
||||
class Pole;
|
||||
|
||||
class Pelican {
|
||||
public:
|
||||
Pelican(int x, int y);
|
||||
~Pelican();
|
||||
|
||||
// call when the WAIT button has been pressed
|
||||
void press(void);
|
||||
|
||||
// states that a Pelican can be in
|
||||
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
|
||||
|
||||
bool isPressed() const { return pressed; }
|
||||
PelicanState getState() const { return state; }
|
||||
|
||||
private:
|
||||
// state that this Pelican is in
|
||||
PelicanState state;
|
||||
|
||||
// called by PeleicanTimer when the lights need to change
|
||||
void timerExpired(void);
|
||||
|
||||
const unsigned int RED_TIME;
|
||||
const unsigned int YELLOW_TIME;
|
||||
const unsigned int GREEN_TIME;
|
||||
const unsigned int FLASH_TIME;
|
||||
|
||||
// indicates whether the WAIT light should be on
|
||||
bool pressed;
|
||||
|
||||
Pole *myPole;
|
||||
|
||||
// Timer that indicates when the lights should change
|
||||
TimerT<Pelican> *myTimer;
|
||||
|
||||
private:
|
||||
// prevent general use of copy ctor and operator=
|
||||
Pelican(const Pelican &);
|
||||
Pelican &operator=(const Pelican &);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "Pole.h"
|
||||
#include "Screen.h"
|
||||
|
||||
Pole::Pole(int x, int y) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)) {
|
||||
using namespace Screen; // we will discuss namespaces in the course
|
||||
|
||||
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
|
||||
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
|
||||
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
|
||||
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
|
||||
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
|
||||
|
||||
write(" _____ ", x, ++y, GRAY);
|
||||
write(" /___ /| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" | |/| ", x, ++y, GRAY);
|
||||
write(" |____| | ", x, ++y, GRAY);
|
||||
write(" | |/| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" |____|/ ", x, ++y, GRAY);
|
||||
write(" _||__ ", x, ++y, GRAY);
|
||||
write(" /___ /| ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" | | | ", x, ++y, GRAY);
|
||||
write(" |____|/ ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write(" || ", x, ++y, GRAY);
|
||||
write("========||===========", x, ++y, GRAY);
|
||||
write(" / || / ", x, ++y, GRAY);
|
||||
write("_ /_________ /_ ", x, ++y, GRAY);
|
||||
write("/ / ", x, ++y, GRAY);
|
||||
}
|
||||
|
||||
Pole::~Pole() {
|
||||
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
|
||||
delete lights[i];
|
||||
delete waitLight;
|
||||
}
|
||||
|
||||
void Pole::setTrafficLights(Pelican::PelicanState state) {
|
||||
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
|
||||
// red yellow green green man red man
|
||||
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
|
||||
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
|
||||
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
|
||||
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
|
||||
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
|
||||
};
|
||||
|
||||
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
|
||||
lights[i]->set(LightStates[state][i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
#ifndef POLE_H
|
||||
#define POLE_H
|
||||
|
||||
#include "Light.h"
|
||||
#include "Pelican.h"
|
||||
|
||||
class Pole {
|
||||
public:
|
||||
Pole(int x, int y);
|
||||
~Pole();
|
||||
|
||||
void setTrafficLights(Pelican::PelicanState state);
|
||||
void setWaitLight(bool pressed);
|
||||
|
||||
private:
|
||||
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
|
||||
|
||||
Light *lights[RED_MAN + 1];
|
||||
|
||||
Light *waitLight;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
|
||||
#include "Timer.h"
|
||||
#include "TimerSet.h"
|
||||
|
||||
void Timer::timeoutRequest(int seconds) { timeToGo = seconds; }
|
||||
|
||||
Timer::Timer() : timeToGo(-1) { TimerSet::Instance().addATimer(this); }
|
||||
|
||||
void Timer::tick() {
|
||||
if (isExpired()) {
|
||||
doExpired();
|
||||
}
|
||||
}
|
||||
|
||||
bool Timer::isExpired() {
|
||||
if (timeToGo > 0) {
|
||||
if (--timeToGo == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// ======================================================
|
||||
//
|
||||
// A general-purpose countdown timer
|
||||
// To use this class, specialize it
|
||||
// and override tick()
|
||||
//
|
||||
// ======================================================
|
||||
|
||||
#ifndef timer_H
|
||||
#define timer_H
|
||||
|
||||
#include <ctime>
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
Timer();
|
||||
|
||||
void timeoutRequest(int seconds);
|
||||
|
||||
// template method
|
||||
void tick();
|
||||
|
||||
protected:
|
||||
bool isExpired();
|
||||
|
||||
private:
|
||||
virtual void doExpired() = 0;
|
||||
|
||||
int timeToGo;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "TimerSet.h"
|
||||
#include "Timer.h"
|
||||
#include <iostream>
|
||||
|
||||
TimerSet::TimerSet() : previousTime(time(nullptr)) {}
|
||||
|
||||
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
|
||||
|
||||
void TimerSet::pollAllTimers() {
|
||||
std::time_t timeNow;
|
||||
if (time(&timeNow) > previousTime) {
|
||||
previousTime = timeNow;
|
||||
|
||||
for (auto it = timers.begin(); it != timers.end(); it++) {
|
||||
(*it)->tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TimerSet &TimerSet::Instance() {
|
||||
// Meyer's Singleton implementation
|
||||
static TimerSet pInstance;
|
||||
return pInstance;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef TIMERSET_H
|
||||
#define TIMERSET_H
|
||||
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
|
||||
class Timer;
|
||||
|
||||
class TimerSet {
|
||||
public:
|
||||
void pollAllTimers();
|
||||
void addATimer(Timer *);
|
||||
|
||||
private:
|
||||
TimerSet();
|
||||
std::vector<Timer *> timers;
|
||||
std::time_t previousTime;
|
||||
|
||||
// Meyer's singleton
|
||||
public:
|
||||
static TimerSet &Instance();
|
||||
|
||||
private:
|
||||
// prevent copying and assignment of the singleton
|
||||
TimerSet(const TimerSet &);
|
||||
TimerSet &operator=(const TimerSet &);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
// ======================================================
|
||||
//
|
||||
// Template for derievd Timers
|
||||
//
|
||||
// ======================================================
|
||||
|
||||
#ifndef TIMERT_H
|
||||
#define TIMERT_H
|
||||
|
||||
#include "Timer.h"
|
||||
|
||||
template <class Target> class TimerT : public Timer {
|
||||
public:
|
||||
typedef void (Target::*TargetFunc)();
|
||||
TimerT(Target &target, TargetFunc targetFunc) : myTarget(target), myTargetFunc(targetFunc) {}
|
||||
|
||||
private:
|
||||
void doExpired() { (myTarget.*myTargetFunc)(); }
|
||||
Target &myTarget;
|
||||
TargetFunc myTargetFunc;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
|
||||
#include "Keyboard.h"
|
||||
#include "Pelican.h"
|
||||
#include "TimerSet.h"
|
||||
|
||||
#include <cctype> // provides toupper()
|
||||
|
||||
int main() {
|
||||
Keyboard kbd;
|
||||
Pelican thePelican(30, 0);
|
||||
|
||||
char c = 0;
|
||||
|
||||
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
|
||||
|
||||
while (!kbd.isCharAvail()) {
|
||||
TimerSet::Instance().pollAllTimers(); // no key press to process, poll the timers
|
||||
}
|
||||
|
||||
c = kbd.getChar(); // get the character
|
||||
if (c == ' ') {
|
||||
// it's a space, so press the WAIT button
|
||||
thePelican.press();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user