Advertisement
PifyZ

Untitled

Dec 6th, 2016
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.84 KB | None | 0 0
  1. package main
  2.  
  3. // Importations
  4. import (
  5.   "fmt"
  6.   "time"
  7. )
  8.  
  9. // Constantes
  10. var DOpened = 0
  11. var DClosed = 1
  12. var DOpening = 2
  13. var DClosing = 3
  14. var DOpeningStopped = 4
  15. var DClosingStopped = 5
  16.  
  17. // Variable d'état
  18. var state = DClosed
  19.  
  20. // État au format chaine
  21. func stateToString(state int) string {
  22.   switch state {
  23.     case DOpened:
  24.       return "Ouvert"
  25.  
  26.     case DClosed:
  27.       return "Fermé"
  28.  
  29.     case DOpening:
  30.       return "En cours d'ouverture"
  31.  
  32.     case DClosing:
  33.       return "En cours de fermeture"
  34.  
  35.     case DOpeningStopped:
  36.       return "Ouverture arrêtée"
  37.  
  38.     case DClosingStopped:
  39.       return "Fermeture arrêtée"
  40.  
  41.     default:
  42.       return "État inconnu"
  43.   }
  44. }
  45.  
  46. // Afficher l'état au format chaine
  47. func displayState(state int) {
  48.   fmt.Printf("État : %s\n", stateToString(state))
  49. }
  50.  
  51. // Appuyer sur le bouton
  52. func pushButton(state *int) {
  53.   fmt.Printf("Appui sur le bouton\n")
  54.  
  55.   switch *state {
  56.     case DOpened, DOpeningStopped:
  57.       *state = DClosing
  58.  
  59.       displayState(*state)
  60.  
  61.       for i := 0 ; i < 5000 ; i++ {
  62.         if *state == DClosing {
  63.           time.Sleep(1 * time.Millisecond)
  64.         } else {
  65.           return
  66.         }
  67.       }
  68.  
  69.       *state = DClosed
  70.       displayState(*state)
  71.  
  72.     case DClosed, DClosingStopped:
  73.       *state = DOpening
  74.  
  75.       displayState(*state)
  76.  
  77.       for i := 0 ; i < 5000 ; i++ {
  78.         if *state == DOpening {
  79.           time.Sleep(1 * time.Millisecond)
  80.         } else {
  81.           return
  82.         }
  83.       }
  84.  
  85.       *state = DOpened
  86.       displayState(*state)
  87.  
  88.     case DOpening:
  89.       *state = DOpeningStopped
  90.  
  91.       displayState(*state)
  92.  
  93.     case DClosing:
  94.       *state = DClosingStopped
  95.  
  96.       displayState(*state)
  97.   }
  98. }
  99.  
  100. func main() {
  101.   var input string
  102.  
  103.   for {
  104.     fmt.Scanln(&input)
  105.  
  106.     go pushButton(&state)
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement