func main() { driver.IOInit() time.Sleep(time.Second * 1) hardware.ResetLights() hardware.SetMotorDirection(0) time.Sleep(time.Second * 1) }
func EventOrderReceived() { for { button := <-ch_order_received var target string if button.Button_type == config.BUTTON_COMMAND { target = config.Laddr } else { target = queue.GetOptimalElev(button) } queue.AddOrder(button, target, ch_order_received) if target == config.Laddr { if config.Local_elev.Door_open { if queue.ShouldStopOnFloor(config.Local_elev.Last_floor) { ch_door_open <- true time.Sleep(time.Millisecond) queue.DeleteOrder(config.Local_elev.Last_floor, ch_outgoing, true) } } else if config.Local_elev.Is_idle { if queue.ShouldStopOnFloor(config.Local_elev.Last_floor) { ch_door_open <- true time.Sleep(time.Millisecond) queue.DeleteOrder(config.Local_elev.Last_floor, ch_outgoing, true) } else { dir := ChooseNewDirection() config.Local_elev.Is_idle = false config.Local_elev.Direction = dir hardware.SetMotorDirection(dir) } } } } }
func CloseDoor() { config.Local_elev.Door_open = false hardware.SetDoorOpenLamp(0) dir := ChooseNewDirection() config.Local_elev.Is_idle = (dir == config.DIR_STOP) config.Local_elev.Direction = dir hardware.SetMotorDirection(dir) }
func EventReachedFloor(floor int) { config.Local_elev.Last_floor = floor hardware.SetFloorIndicator(floor) if queue.ShouldStopOnFloor(floor) { config.Local_elev.Is_idle = true hardware.SetMotorDirection(config.DIR_STOP) queue.DeleteOrder(floor, ch_outgoing, true) ch_door_open <- true } }
func main() { if _, err := os.Open(config.QUEUE_FILENAME); err == nil { BackupHold() queue.FileRead(config.QUEUE_FILENAME) network.Init(ch_outgoing_msg, ch_incoming_msg, ch_new_elev, ch_main_alive) } else { network.Init(ch_outgoing_msg, ch_incoming_msg, ch_new_elev, ch_main_alive) time.Sleep(time.Millisecond) if _, err := os.Create(config.QUEUE_FILENAME); err != nil { log.Fatal("FATAL: Could not create queue file!") } } backup := exec.Command("gnome-terminal", "-x", "sh", "-c", "go run main/main.go") backup.Run() if !hardware.Init() { log.Fatal("Unable to initialize elevator hardware!") } hardware.SetMotorDirection(fsm.ChooseNewDirection()) go MessageServer() go hardware.ReadButtons(ch_button_pressed) go hardware.SetLights() go hardware.FloorPoller(ch_floor_poll) go StateSpammer() go fsm.EventOrderReceived() fsm.Init(ch_outgoing_msg, ch_new_order) log.Printf("Elev addr: %s", config.Laddr) for { select { case button := <-ch_button_pressed: ch_new_order <- button if button.Button_type != config.BUTTON_COMMAND { ch_outgoing_msg <- config.Message{Msg_type: config.ADD_ORDER, Button: button} } case floor := <-ch_floor_poll: fsm.EventReachedFloor(floor) } } }