//Called by executeOrder func updateStatus(currentFloor uint, status *driver.LiftStatus, motorDirectionCh chan<- driver.MotorDirection, statusCh chan<- driver.LiftStatus) { switch currentFloor { case 0: if status.Door { log.Fatal("lift should not be moving, door is open") } if !status.Running { log.Fatal("lift should not be moving, motor is off") } return case 1, 4: motorDirectionCh <- driver.MD_stop status.Floor = currentFloor status.Running = false statusCh <- *status case 2, 3: if currentFloor != status.Floor { status.Floor = currentFloor statusCh <- *status } default: log.Println("found unknown floor", currentFloor) } }
//Called by Init func executeOrder(orderedFloorCh <-chan uint, lightCh chan<- driver.Light, statusCh chan<- driver.LiftStatus, floorSensorCh <-chan uint, doorTimerCh chan bool, motorDirectionCh chan<- driver.MotorDirection, motorStopCh chan<- bool, quitCh <-chan bool) { var ( currentFloor uint stopFloor uint status driver.LiftStatus ) status.Direction = true // not in state, go up until floor motorDirectionCh <- driver.MD_up for { currentFloor = <-floorSensorCh if currentFloor != 0 { log.Println("found floor") break } } motorDirectionCh <- driver.MD_stop status.Floor = currentFloor log.Println(currentFloor) status.Running = false status.Door = false statusCh <- status for { select { case <-quitCh: return case stopFloor = <-orderedFloorCh: //Got new stopFloor case <-doorTimerCh: lightCh <- driver.Light{0, driver.Door, false} status.Door = false statusCh <- status case currentFloor = <-floorSensorCh: updateStatus(currentFloor, &status, motorDirectionCh, statusCh) default: time.Sleep(5 * time.Millisecond) if stopFloor != 0 { stopAtFloor(currentFloor, &status, &stopFloor, motorDirectionCh, lightCh, statusCh, doorTimerCh, motorStopCh) goToFloor(currentFloor, &status, &stopFloor, motorDirectionCh, statusCh) } } } }