/** * \fn bool DSA::sendMessage(byte opcode, byte parameter) * \brief Send a message. * * A DSA message contains a opcode and a parameter send as one 16bit command. * \param opcode The DSA opcode to send. * \param parameter The DSA parameter to send. * \return True on a successful transfer, False on a communication error */ bool DSA::sendMessage(byte opcode, byte parameter) { /* synchPhase */ int command = opcode; command = command << 8; command += parameter; digitalWrite(_DSApin, HIGH); resetPins(); pinMode(_DSApin, OUTPUT); startTimeoutTimer(DSA_TIMEOUT); digitalWrite(_DSApin, LOW); while(digitalRead(_ACKpin)) { if(timeOut()) { resetPins(); return false; } } digitalWrite(_DSApin, HIGH); while(!digitalRead(_ACKpin)) { if(timeOut()) { resetPins(); return false; } } /* data transfer phase */ digitalWrite(_STBpin, HIGH); pinMode(_STBpin, OUTPUT); startTimeoutTimer(DSA_TIMEOUT); unsigned int mask = 0x8000; int i; for (i = 0; i < 16; i++ ) { if(!(command & mask)) digitalWrite(_DSApin, LOW); digitalWrite(_STBpin, LOW); while(digitalRead(_ACKpin)) { if(timeOut()) { resetPins(); return false; } } digitalWrite(_STBpin, HIGH); digitalWrite(_DSApin, HIGH); while(!digitalRead(_ACKpin)) { if(timeOut()) { resetPins(); return false; } } mask = mask >> 1; } /* acknowledge phase */ digitalWrite(_ACKpin, HIGH); resetPins(); pinMode(_ACKpin, OUTPUT); startTimeoutTimer(DSA_TIMEOUT); digitalWrite(_ACKpin, LOW); while(digitalRead(_STBpin)) { if(timeOut()) { resetPins(); return false; } } if(!digitalRead(_DSApin)) { resetPins(); return false; } digitalWrite(_ACKpin, HIGH); while(!digitalRead(_STBpin)) { if(timeOut()) { resetPins(); return false; } } resetPins(); return true; }
/** * \fn bool DSA::receiveMessage(byte *opcode, byte *parameter) * \brief Receive a message. * * \param *opcode Pointer to the opcode variable that will updated after a successful transfer * \param *parameter Pointer to the parameter variable that will updated after a successful transfer. * \return True on a successful transfer, False on a communication error */ bool DSA::receiveMessage(byte *opcode, byte *parameter) { /* synchPhase */ word command = 0; digitalWrite(_ACKpin, HIGH); resetPins(); pinMode(_ACKpin, OUTPUT); startTimeoutTimer(DSA_TIMEOUT); digitalWrite(_ACKpin, LOW); while(!digitalRead(_DSApin)) { if(timeOut()) { resetPins(); return false; } } digitalWrite(_ACKpin, HIGH); /* data transmission phase */ startTimeoutTimer(DSA_TIMEOUT); unsigned int mask = 0x8000; int i; for (i = 0; i < 16; i++ ) { while(digitalRead(_STBpin)) { if(timeOut()) { resetPins(); return false; } } if (digitalRead(_DSApin)) command = command | mask; digitalWrite(_ACKpin, LOW); while(!digitalRead(_STBpin)) { if(timeOut()) { resetPins(); return false; } } digitalWrite(_ACKpin, HIGH); mask = mask >> 1; } /* acknowledge phase */ pinMode(_STBpin, OUTPUT); pinMode(_DSApin, OUTPUT); pinMode(_ACKpin, INPUT); startTimeoutTimer(DSA_TIMEOUT); while(digitalRead(_ACKpin)) { if(timeOut()) { resetPins(); return false; } } if(i != 16) digitalWrite(_DSApin, LOW); digitalWrite(_STBpin, LOW); while(!digitalRead(_ACKpin)) { if(timeOut()) { resetPins(); return false; } } digitalWrite(_DSApin, HIGH); digitalWrite(_STBpin, HIGH); *parameter = lowByte(command); *opcode = highByte(command); resetPins(); return true; }
void Window::timeOutSlot() { timeOut(); }
int main() { if (!elev_init()) { printf(__FILE__ ": Unable to initialize elevator hardware\n"); return 1; } slettAlleOrdre(); elev_set_speed(0); initHeisstyring(); int forgjeEtasje = 0; int etasje; bool forgjeStopp; bool stopp; bool forgjeObstruksjon; bool obstruksjon; bool forgjeOpp[4]; bool opp[4]; bool forgjeInne[4]; bool inne[4]; bool forgjeNed[4]; bool ned[4]; while(1) { stopp = elev_get_stop_signal(); obstruksjon = elev_get_obstruction_signal(); etasje = elev_get_floor_sensor_signal(); for(int i = 0; i < N_FLOORS; i++) // poller masse, fix { if(i != N_FLOORS-1) opp[i] = elev_get_button_signal(BUTTON_CALL_UP, i); if(i != 0) ned[i] = elev_get_button_signal(BUTTON_CALL_DOWN, i); inne[i] = elev_get_button_signal(BUTTON_COMMAND, i); } for(int i = 0; i < N_FLOORS; i++) { if(opp[i] && !forgjeOpp[i]) leggTilOrdre(i, false, true); if(ned[i] && !forgjeNed[i]) leggTilOrdre(i, false, false); if(inne[i] && !forgjeInne[i]) leggTilOrdre(i, true, true); forgjeOpp[i] = opp[i]; forgjeNed[i] = ned[i]; forgjeInne[i] = inne[i]; } if(harTimetUt()) timeOut(); if (etasje != -1 && etasje != forgjeEtasje) { elev_set_floor_indicator(etasje); etasjeAnkommet(etasje); forgjeEtasje = etasje; } if(obstruksjon && !forgjeObstruksjon) { forgjeObstruksjon = obstruksjon; forgjeEtasje = -1; if(obstruksjon) obstruksjonPa(); else obstruksjonAv(); } if(stopp && !forgjeStopp) { forgjeStopp = stopp; forgjeEtasje = -1; if(stopp) nodStopp(); } } return 0; }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), server(BUFSIZE + 1, this), client(BUFSIZE + 1, this) { setMenuBar(new QMenuBar(this)); QMenu *gameMenu = new QMenu("Game", menuBar()); createAction = new QAction("Create", gameMenu); createAction->setIcon(QIcon(":/create.icns")); joinAction = new QAction("Join in", gameMenu); joinAction->setIcon(QIcon(":/join.icns")); abortAction = new QAction("Abort", gameMenu); abortAction->setIcon(QIcon(":/abort.icns")); menuBar()->addMenu(gameMenu); gameMenu->addAction(createAction); gameMenu->addAction(joinAction); gameMenu->addAction(abortAction); QToolBar *toolbar = new QToolBar(this); toolbar->setMovable(0); addToolBar(Qt::LeftToolBarArea, toolbar); toolbar->addAction(createAction); toolbar->addAction(joinAction); toolbar->addAction(abortAction); abortAction->setEnabled(0); setStatusBar(new QStatusBar(this)); setCentralWidget(new QWidget(this)); QHBoxLayout *mainLayout = new QHBoxLayout(); centralWidget()->setLayout(mainLayout); chessBoard = new ChessBoard(centralWidget()); mainLayout->addWidget(chessBoard); opp = new UserWidget(this); loc = new UserWidget(this); oppName = new QLabel(this); locName = new QLabel(this); QVBoxLayout *rightLayout = new QVBoxLayout(); mainLayout->addLayout(rightLayout); rightLayout->addWidget(opp); rightLayout->addWidget(oppName); startButton = new QPushButton("Start", this); startButton->setEnabled(0); rightLayout->addWidget(startButton); tieButton = new QPushButton("Tie", this); tieButton->setEnabled(0); rightLayout->addWidget(tieButton); surButton = new QPushButton("Surrender", this); surButton->setEnabled(0); rightLayout->addWidget(surButton); rightLayout->addWidget(tieButton); number = new QLCDNumber(this); rightLayout->addWidget(number); rightLayout->addWidget(loc); rightLayout->addWidget(locName); tieTimer.setSingleShot(1); //TODO rightLayout for some information QObject::connect(createAction, SIGNAL(triggered()), this, SLOT(createGame())); QObject::connect(joinAction, SIGNAL(triggered()), this, SLOT(joinGame())); QObject::connect(abortAction, SIGNAL(triggered()), this, SLOT(abortLink())); QObject::connect(chessBoard, SIGNAL(clickPiece(QPair<int,int>)), this, SLOT(handleClick(QPair<int, int>))); QObject::connect(&server, SIGNAL(received(void*, int)), this, SLOT(handleReceive(void*, int))); QObject::connect(&server, SIGNAL(connected()), this, SLOT(handleConnected())); QObject::connect(&client, SIGNAL(received(void*,int)), this, SLOT(handleReceive(void*, int))); QObject::connect(startButton, SIGNAL(clicked()), this, SLOT(startGame())); QObject::connect(&tieTimer, SIGNAL(timeout()), this, SLOT(enableTie())); QObject::connect(tieButton, SIGNAL(clicked()), this, SLOT(handleTie())); QObject::connect(surButton, SIGNAL(clicked()), this, SLOT(abortLink())); QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(timeOut())); }