Skip to content

Commit 9296a49

Browse files
authored
Rpi#249 StateMachine and LED/OFPlayer update (#250)
* FSM#1 * FSM#2 * FSM#3 * FSM#4 * FSM#5 * FSM#6 * worked version 0225 * error message modified * ./scripts/fade added * scripts/fade revise * warning fixed
1 parent 3516e34 commit 9296a49

19 files changed

+735
-519
lines changed

controller/software/CMakeLists.txt

+10-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ include_directories(${INCLUDES} ${HARDWARE_INCLUDES})
5151

5252
add_library(playerLIB STATIC
5353
${SOURCE}/player/LEDPlayer.cpp ${SOURCE}/player/OFPlayer.cpp
54-
${SOURCE}/commands/player.cpp)
54+
${SOURCE}/commands/player.cpp )
55+
# ${SOURCE}/commands/player.cpp ${SOURCE}/player/StateMachine.cpp)
56+
add_library(StateMachineLIB STATIC ${SOURCE}/player/StateMachine.cpp ${SOURCE}/player/FSM_Common.cpp)
5557
# target_include_directories(playerLIB PUBLIC ${INCLUDES} ${HARDWARE_INCLUDES})
56-
target_link_libraries(playerLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})
58+
target_link_libraries(playerLIB StateMachineLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})
59+
target_link_libraries(StateMachineLIB playerLIB ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES})
5760

5861
# list
5962
add_executable(
@@ -97,7 +100,7 @@ target_link_libraries(playerctl ${Boost_LIBRARIES})
97100
add_executable(
98101
parttest ${SOURCE}/commands/partTest.cpp)
99102
# target_include_directories(parttest PUBLIC ${INCLUDES} ${HARDWARE_INCLUDES})
100-
target_link_libraries(parttest ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES} playerLIB)
103+
target_link_libraries(parttest ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES} playerLIB StateMachineLIB)
101104

102105
# color_palette
103106
# add_executable(
@@ -109,12 +112,14 @@ target_link_libraries(parttest ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES} playerLI
109112
# player
110113
add_executable(
111114
player ${SOURCE}/player/playLoop.cpp)
115+
112116
set_target_properties(player PROPERTIES COMPILE_FLAGS "-pthread"
113117
LINK_FLAGS "-pthread")
114118
# target_include_directories(player PUBLIC ${INCLUDES} ${HARDWARE_INCLUDES})
115119
target_link_libraries(player ${Boost_LIBRARIES} ${HARDWARE_LIBRARIES}
116-
${CMAKE_THREAD_LIBS_INIT} playerLIB Threads::Threads)
117-
120+
${CMAKE_THREAD_LIBS_INIT} playerLIB StateMachineLIB Threads::Threads pthread)
121+
# ${CMAKE_THREAD_LIBS_INIT} playerLIB StateMachineLIB Threads::Threads pthread)
122+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
118123
# socket
119124
# add_executable(socket ${SOURCE}/socket/client.cpp)
120125
# target_include_directories(socket PUBLIC ${INCLUDES})

controller/software/Makefile

-111
This file was deleted.

controller/software/inc/FSM_Common.h

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#ifndef FSM_COMMON_H
2+
#define FSM_COMMON_H
3+
4+
#include <sys/time.h>
5+
#include <iostream>
6+
#include <thread>
7+
#include <const.h>
8+
#include <StateMachine.h>
9+
#include <LEDPlayer.h>
10+
#include <OFPlayer.h>
11+
#include <player.h>
12+
#include <string>
13+
14+
15+
enum CMD { C_PLAY, C_PAUSE, C_STOP, C_RESUME };
16+
extern const std::string cmds[10];
17+
extern std::thread led_loop, of_loop;
18+
extern Player player;
19+
extern LEDPlayer led_player;
20+
extern OFPlayer of_player;
21+
extern int dancer_fd;
22+
extern string path ;
23+
extern const char *rd_fifo;
24+
extern const char *wr_fifo;
25+
inline void write_fifo(bool success) {
26+
int wr_fd;
27+
std::string msg;
28+
29+
wr_fd = open(wr_fifo, O_WRONLY);
30+
if (success) {
31+
msg = "0";
32+
} else {
33+
msg = "1";
34+
}
35+
write(wr_fd, msg.c_str(), msg.length() + 1);
36+
close(wr_fd);
37+
}
38+
39+
inline const std::vector<std::string> split(const std::string &str, const std::string &pattern) {
40+
std::vector<std::string> result;
41+
std::string::size_type begin, end;
42+
43+
end = str.find(pattern);
44+
begin = 0;
45+
46+
while (end != std::string::npos) {
47+
if (end - begin != 0) {
48+
result.push_back(str.substr(begin, end - begin));
49+
}
50+
begin = end + pattern.size();
51+
end = str.find(pattern, begin);
52+
}
53+
54+
if (begin != str.length()) {
55+
result.push_back(str.substr(begin));
56+
}
57+
return result;
58+
}
59+
60+
inline timeval getCalculatedTime(timeval subtrahend) {
61+
timeval currentTime;
62+
gettimeofday(&currentTime, NULL);
63+
timeval time;
64+
time.tv_sec = currentTime.tv_sec - subtrahend.tv_sec;
65+
time.tv_usec = currentTime.tv_usec - subtrahend.tv_usec;
66+
if (time.tv_usec < 0) {
67+
time.tv_sec--;
68+
time.tv_usec += 1000000;
69+
}
70+
return time;
71+
}
72+
inline bool restart() {
73+
printf("restart\n");
74+
dancer_fd = tryGetLock(path.c_str());
75+
if (dancer_fd == -1) {
76+
cerr << "[Common] Dancer is playing! Please stop it first!\n";
77+
return 0;
78+
} else if (dancer_fd == -2) {
79+
cerr << "[Common] dancer.dat file not found!\n";
80+
return 0;
81+
}
82+
83+
if (!restorePlayer(player, path.c_str())) {
84+
// fprintf(stderr, "restorePlayer ERROR\n");
85+
cerr << "[Common] Can't restorePlayer!\n";
86+
return false;
87+
}
88+
led_player = player.myLEDPlayer;
89+
led_player.init();
90+
of_player = player.myOFPlayer;
91+
of_player.init();
92+
cerr << "[Common] Player loaded\n";
93+
return true;
94+
}
95+
inline void resume( StateMachine* fsm ){
96+
led_loop = std::thread(&LEDPlayer::loop, &led_player, fsm);
97+
of_loop = std::thread(&OFPlayer::loop, &of_player, fsm);
98+
//led_loop.detach();
99+
//of_loop.detach();
100+
// pthread_create(&led_loop, NULL,&LEDPlayer::loop_helper, &led_player, fsm);
101+
// pthread_create(&of_loop, NULL,&OFPlayer::loop_helper, &of_player , fsm);
102+
cerr << "[Common] thread running\n";
103+
return;
104+
}
105+
106+
inline int parse_command(StateMachine* fsm,std::string str) {
107+
if (str.length() == 1){
108+
write_fifo(false);
109+
return -1;
110+
}
111+
std::vector<std::string> cmd = split(str, " ");
112+
string cmds[3]= {"play", "pause", "stop"};
113+
int cmd_recv=-1;
114+
long startusec=0;
115+
for (int i = 0; i < 3; i++) {
116+
if (cmd[0] == cmds[i]) {
117+
if (i == C_PLAY) {
118+
if(fsm->getCurrentState()==S_PLAY) {
119+
write_fifo(false);
120+
return -1;
121+
}
122+
fsm->data.delayTime=0;
123+
gettimeofday(&fsm->data.baseTime, NULL);//for delay display
124+
if(fsm->getCurrentState()==S_PAUSE && cmd[1]=="0"&& cmd[2] == "-1" && cmd[4] == "0"){
125+
write_fifo(true);
126+
cmd_recv=C_RESUME;
127+
cerr<<"[Common] RESUME\n";
128+
return cmd_recv;
129+
} else if (cmd.size() >= 3 && cmd[cmd.size() - 2] == "-d") {
130+
fsm->data.delayTime = std::stoi(cmd[cmd.size() - 1]);//*1000;//saved as us
131+
if (cmd.size() > 3) {
132+
startusec = std::stoi(cmd[1])*1000;
133+
}
134+
if (cmd.size() > 4) {
135+
fsm->data.stopTime = std::stoi(cmd[2]);
136+
fsm->data.stopTimeAssigned = true;
137+
}
138+
} else {
139+
if (cmd.size()>1) {
140+
startusec = std::stoi(cmd[1])*1000;
141+
}
142+
if (cmd.size() > 2) {
143+
fsm->data.stopTime = std::stoi(cmd[2]);
144+
fsm->data.stopTimeAssigned = true;
145+
}
146+
}
147+
//fprintf(stderr,"[Common] startusec[%d]\n",startusec);
148+
fsm->data.playedTime.tv_sec = startusec / 1000000;
149+
fsm->data.playedTime.tv_usec = startusec % 1000000;
150+
}
151+
cmd_recv=i;
152+
printf("command parsed\n");
153+
write_fifo(true);
154+
return cmd_recv;
155+
}
156+
}
157+
write_fifo(false);
158+
return -1;
159+
}
160+
161+
#endif

controller/software/inc/LEDPlayer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <boost/serialization/version.hpp>
2727

2828
#include "LEDController.h"
29+
#include "StateMachine.h"
2930

3031
using namespace std;
3132

@@ -61,10 +62,10 @@ class LEDPlayer {
6162

6263
void init();
6364
// threading function
64-
void loop(atomic<bool> *playing, const timeval *baseTime, const atomic<bool> *toTerminate,
65-
atomic<bool> *finished);
65+
void* loop_helper(void *context, StateMachine* fsm);
66+
void loop(StateMachine *fsm);
6667
void delayDisplay(const bool *delayingDisplay);
67-
68+
void darkAll();
6869
template <class Archive>
6970
void serialize(Archive &archive, const unsigned int version);
7071
string list() const;

controller/software/inc/OFPlayer.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <boost/serialization/version.hpp>
2828

2929
#include "OFController.h"
30+
#include "StateMachine.h"
3031

3132
using namespace std;
3233

@@ -63,11 +64,11 @@ class OFPlayer {
6364
const int &_OFnum);
6465

6566
// threading function
66-
void loop(atomic<bool> *playing, const timeval *baseTime, const atomic<bool> *toTerminate,
67-
atomic<bool> *finished);
67+
// void* loop_helper(void* context, StateMachine *fsm);
68+
void loop(StateMachine *fsm);
6869
void delayDisplay(const bool *delayingDisplay);
6970
void init();
70-
71+
void darkAll();
7172
template <class Archive>
7273
void serialize(Archive &archive, const unsigned int version);
7374
string list() const;

0 commit comments

Comments
 (0)