Skip to content

Commit

Permalink
Implemented "stop file" command. Added StopAction. Driver now stops w…
Browse files Browse the repository at this point in the history
…hen stopDeviceIO is called (either explicitly or implicitly when the experiment ends).
  • Loading branch information
cstawarz committed Feb 5, 2020
1 parent aec001d commit c1f0b14
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 118 deletions.
6 changes: 6 additions & 0 deletions BlackrockLEDDriver.xcodeproj/project.pbxproj
Expand Up @@ -35,6 +35,7 @@
/* Begin PBXBuildFile section */
E1208E401D108EFE00DB9836 /* BlackrockLEDDriverPrepareAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1208E3E1D108EFE00DB9836 /* BlackrockLEDDriverPrepareAction.cpp */; };
E1208E431D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1208E411D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.cpp */; };
E15475FB23E9C4E80048D13D /* BlackrockLEDDriverStopAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E15475F923E9C4E80048D13D /* BlackrockLEDDriverStopAction.cpp */; };
E1A8E9BA19F947D900571433 /* BlackrockLEDDriverRunAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1A8E9B819F947D900571433 /* BlackrockLEDDriverRunAction.cpp */; };
E1D9A8FD19D3475500F91003 /* BlackrockLEDDriverDevice.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1D9A8FB19D3475500F91003 /* BlackrockLEDDriverDevice.cpp */; };
E1D9A8FF19D3477300F91003 /* BlackrockLEDDriverPlugin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1D9A8FE19D3477300F91003 /* BlackrockLEDDriverPlugin.cpp */; };
Expand Down Expand Up @@ -93,6 +94,8 @@
E1208E3F1D108EFE00DB9836 /* BlackrockLEDDriverPrepareAction.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = BlackrockLEDDriverPrepareAction.hpp; sourceTree = "<group>"; };
E1208E411D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlackrockLEDDriverReadTempsAction.cpp; sourceTree = "<group>"; };
E1208E421D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = BlackrockLEDDriverReadTempsAction.hpp; sourceTree = "<group>"; };
E15475F923E9C4E80048D13D /* BlackrockLEDDriverStopAction.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = BlackrockLEDDriverStopAction.cpp; sourceTree = "<group>"; };
E15475FA23E9C4E80048D13D /* BlackrockLEDDriverStopAction.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = BlackrockLEDDriverStopAction.hpp; sourceTree = "<group>"; };
E1A8E9B819F947D900571433 /* BlackrockLEDDriverRunAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlackrockLEDDriverRunAction.cpp; sourceTree = "<group>"; };
E1A8E9B919F947D900571433 /* BlackrockLEDDriverRunAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlackrockLEDDriverRunAction.h; sourceTree = "<group>"; };
E1D9A8F119D345F400F91003 /* BlackrockLEDDriver.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = BlackrockLEDDriver.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -136,6 +139,8 @@
E1208E3E1D108EFE00DB9836 /* BlackrockLEDDriverPrepareAction.cpp */,
E1A8E9B919F947D900571433 /* BlackrockLEDDriverRunAction.h */,
E1A8E9B819F947D900571433 /* BlackrockLEDDriverRunAction.cpp */,
E15475FA23E9C4E80048D13D /* BlackrockLEDDriverStopAction.hpp */,
E15475F923E9C4E80048D13D /* BlackrockLEDDriverStopAction.cpp */,
E1208E421D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.hpp */,
E1208E411D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.cpp */,
);
Expand Down Expand Up @@ -304,6 +309,7 @@
E1DCDC9F19FAABB500BB9A2D /* BlackrockLEDDriverAction.cpp in Sources */,
E1A8E9BA19F947D900571433 /* BlackrockLEDDriverRunAction.cpp in Sources */,
E1D9A8FD19D3475500F91003 /* BlackrockLEDDriverDevice.cpp in Sources */,
E15475FB23E9C4E80048D13D /* BlackrockLEDDriverStopAction.cpp in Sources */,
E1208E431D1093B700DB9836 /* BlackrockLEDDriverReadTempsAction.cpp in Sources */,
E1DCDCA219FAADA700BB9A2D /* BlackrockLEDDriverSetIntensityAction.cpp in Sources */,
E1D9A8FF19D3477300F91003 /* BlackrockLEDDriverPlugin.cpp in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions BlackrockLEDDriver/Actions/BlackrockLEDDriverStopAction.cpp
@@ -0,0 +1,29 @@
//
// BlackrockLEDDriverStopAction.cpp
// BlackrockLEDDriver
//
// Created by Christopher Stawarz on 2/4/20.
// Copyright © 2020 The MWorks Project. All rights reserved.
//

#include "BlackrockLEDDriverStopAction.hpp"


BEGIN_NAMESPACE_MW_BLACKROCK_LEDDRIVER


void StopAction::describeComponent(ComponentInfo &info) {
Action::describeComponent(info);
info.setSignature("action/blackrock_led_driver_stop");
}


bool StopAction::execute() {
if (auto sharedDevice = weakDevice.lock()) {
sharedDevice->stop();
}
return true;
}


END_NAMESPACE_MW_BLACKROCK_LEDDRIVER
33 changes: 33 additions & 0 deletions BlackrockLEDDriver/Actions/BlackrockLEDDriverStopAction.hpp
@@ -0,0 +1,33 @@
//
// BlackrockLEDDriverStopAction.hpp
// BlackrockLEDDriver
//
// Created by Christopher Stawarz on 2/4/20.
// Copyright © 2020 The MWorks Project. All rights reserved.
//

#ifndef BlackrockLEDDriverStopAction_hpp
#define BlackrockLEDDriverStopAction_hpp

#include "BlackrockLEDDriverAction.h"


BEGIN_NAMESPACE_MW_BLACKROCK_LEDDRIVER


class StopAction : public Action {

public:
static void describeComponent(ComponentInfo &info);

using Action::Action;

bool execute() override;

};


END_NAMESPACE_MW_BLACKROCK_LEDDRIVER


#endif /* BlackrockLEDDriverStopAction_hpp */
43 changes: 8 additions & 35 deletions BlackrockLEDDriver/BlackrockLEDDriverCommand.h
Expand Up @@ -227,22 +227,21 @@ struct SetFileTimePeriodMessageBody {
using SetFileTimePeriodMessage = Message<0x05, 0x05, 0x06, SetFileTimePeriodMessageBody>;


using StartFilePlayingRequest = Message<0x05, 0x05, 0x07>;
struct FilePlayingResponseBody {
BYTE filePlaying;
};


struct StartFilePlayingResponseBody {
BYTE filePlayStarted;
};
using StartFilePlayingResponse = Message<0x05, 0x05, 0x07, StartFilePlayingResponseBody>;
using StartFilePlayingRequest = Message<0x05, 0x05, 0x07>;
using StartFilePlayingResponse = Message<0x05, 0x05, 0x07, FilePlayingResponseBody>;


using IsFilePlayingRequest = Message<0x05, 0x05, 0x08>;
using IsFilePlayingResponse = Message<0x05, 0x05, 0x08, FilePlayingResponseBody>;


struct IsFilePlayingResponseBody {
BYTE filePlaying;
};
using IsFilePlayingResponse = Message<0x05, 0x05, 0x08, IsFilePlayingResponseBody>;
using StopFilePlayingRequest = Message<0x05, 0x05, 0x09>;
using StopFilePlayingResponse = Message<0x05, 0x05, 0x09, FilePlayingResponseBody>;


using ThermistorValuesRequest = Message<0x05, 0x05, 0x80>;
Expand All @@ -261,29 +260,3 @@ END_NAMESPACE_MW_BLACKROCK_LEDDRIVER


#endif /* !defined(BlackrockLEDDriver_BlackrockLEDDriverCommand_h) */


























102 changes: 51 additions & 51 deletions BlackrockLEDDriver/BlackrockLEDDriverDevice.cpp
Expand Up @@ -34,27 +34,16 @@ void Device::describeComponent(ComponentInfo &info) {

Device::Device(const ParameterValueMap &parameters) :
IODevice(parameters),
running(optionalVariable(parameters[RUNNING])),
tempA(optionalVariable(parameters[TEMP_A])),
tempB(optionalVariable(parameters[TEMP_B])),
tempC(optionalVariable(parameters[TEMP_C])),
tempD(optionalVariable(parameters[TEMP_D])),
handle(nullptr),
intensityChanged(true),
filePlaying(false),
lastRunDuration(0)
{
if (!(parameters[RUNNING].empty())) {
running = VariablePtr(parameters[RUNNING]);
}
if (!(parameters[TEMP_A].empty())) {
tempA = VariablePtr(parameters[TEMP_A]);
}
if (!(parameters[TEMP_B].empty())) {
tempB = VariablePtr(parameters[TEMP_B]);
}
if (!(parameters[TEMP_C].empty())) {
tempC = VariablePtr(parameters[TEMP_C]);
}
if (!(parameters[TEMP_D].empty())) {
tempD = VariablePtr(parameters[TEMP_D]);
}

intensity.fill(WordValue::zero());
}

Expand All @@ -67,6 +56,7 @@ Device::~Device() {
}

if (handle) {
stopFilePlaying();
FT_STATUS status = FT_Close(handle);
if (FT_OK != status) {
merror(M_IODEVICE_MESSAGE_DOMAIN, "Cannot close LED driver (status: %d)", status);
Expand Down Expand Up @@ -128,6 +118,13 @@ bool Device::initialize() {
}


bool Device::stopDeviceIO() {
lock_guard lock(mutex);
stopFilePlaying();
return true;
}


void Device::setIntensity(const std::set<int> &channels, double value) {
lock_guard lock(mutex);

Expand Down Expand Up @@ -158,19 +155,19 @@ void Device::prepare(MWTime duration) {

void Device::run(MWTime duration) {
lock_guard lock(mutex);

if (!(updateFile(duration) && startFilePlaying())) {
return;
}

filePlaying = true;
if (running && !running->getValue().getBool()) {
running->setValue(true);
if (updateFile(duration)) {
startFilePlaying();
}
}


static inline void announceTemp(VariablePtr &var, WORD value) {
void Device::stop() {
lock_guard lock(mutex);
stopFilePlaying();
}


static inline void announceTemp(const VariablePtr &var, WORD value) {
if (var) {
var->setValue(double(value) / 1000.0);
}
Expand Down Expand Up @@ -319,11 +316,16 @@ bool Device::startFilePlaying() {
return false;
}

if (!response.getBody().filePlayStarted) {
if (!response.getBody().filePlaying) {
merror(M_IODEVICE_MESSAGE_DOMAIN, "LED driver failed to start file play");
return false;
}

filePlaying = true;
if (running && !running->getValue().getBool()) {
running->setValue(true);
}

return true;
}

Expand All @@ -349,30 +351,28 @@ bool Device::checkIfFileStopped() {
}


END_NAMESPACE_MW_BLACKROCK_LEDDRIVER
























bool Device::stopFilePlaying() {
if (filePlaying) {
StopFilePlayingRequest request;
StopFilePlayingResponse response;

if (!perform(request, response)) {
return false;
}

if (response.getBody().filePlaying) {
merror(M_IODEVICE_MESSAGE_DOMAIN, "LED driver failed to stop file play");
return false;
}

filePlaying = false;
if (running && running->getValue().getBool()) {
running->setValue(false);
}
}

return true;
}


END_NAMESPACE_MW_BLACKROCK_LEDDRIVER
39 changes: 8 additions & 31 deletions BlackrockLEDDriver/BlackrockLEDDriverDevice.h
Expand Up @@ -30,10 +30,12 @@ class Device : public IODevice, boost::noncopyable {
~Device();

bool initialize() override;
bool stopDeviceIO() override;

void setIntensity(const std::set<int> &channels, double value);
void prepare(MWTime duration);
void run(MWTime duration);
void stop();
void readTemps();

private:
Expand All @@ -43,18 +45,19 @@ class Device : public IODevice, boost::noncopyable {
bool loadFile(std::size_t samplesUsed);
bool startFilePlaying();
bool checkIfFileStopped();
bool stopFilePlaying();

template<typename Request, typename Response>
bool perform(Request &request, Response &response) { return request.write(handle) && response.read(handle); }

template<typename Message>
bool perform(Message &message) { return perform(message, message); }

VariablePtr running;
VariablePtr tempA;
VariablePtr tempB;
VariablePtr tempC;
VariablePtr tempD;
const VariablePtr running;
const VariablePtr tempA;
const VariablePtr tempB;
const VariablePtr tempC;
const VariablePtr tempD;

FT_HANDLE handle;
std::array<WordValue, numChannels> intensity;
Expand All @@ -75,29 +78,3 @@ END_NAMESPACE_MW_BLACKROCK_LEDDRIVER


#endif /* !defined(__BlackrockLEDDriver__BlackrockLEDDriverDevice__) */


























2 changes: 2 additions & 0 deletions BlackrockLEDDriver/BlackrockLEDDriverPlugin.cpp
Expand Up @@ -10,6 +10,7 @@
#include "BlackrockLEDDriverSetIntensityAction.h"
#include "BlackrockLEDDriverPrepareAction.hpp"
#include "BlackrockLEDDriverRunAction.h"
#include "BlackrockLEDDriverStopAction.hpp"
#include "BlackrockLEDDriverReadTempsAction.hpp"


Expand All @@ -22,6 +23,7 @@ class Plugin : public mw::Plugin {
registry->registerFactory<StandardComponentFactory, SetIntensityAction>();
registry->registerFactory<StandardComponentFactory, PrepareAction>();
registry->registerFactory<StandardComponentFactory, RunAction>();
registry->registerFactory<StandardComponentFactory, StopAction>();
registry->registerFactory<StandardComponentFactory, ReadTempsAction>();
}
};
Expand Down

0 comments on commit c1f0b14

Please sign in to comment.