|
| 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(¤tTime, 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 |
0 commit comments