#!/usr/bin/env python
# -*- coding: latin-1 -*-

'''
Contrôle d'un moteur pas avec micro:bit

Plus d'infos: 
http://electroniqueamateur.blogspot.com/2020/06/microbit-et-moteur-pas-pas.html

'''

from microbit import *

#numéros des broches
pin_bobine_1_1 = pin0
pin_bobine_1_2 = pin1
pin_bobine_2_1 = pin2
pin_bobine_2_2 = pin8

plus = Image("00000:"
             "00900:"
             "09990:"
             "00900:"
             "00000")
             
moins = Image("00000:"
             "00000:"
             "09990:"
             "00000:"
             "00000")             

horaire = Image("00090:"
             "99999:"
             "90090:"
             "90000:"
             "99999")     
             
antihor = Image("99999:"
             "90000:"
             "90090:"
             "99999:"
             "00090")    

attente = 100
marcheavant = 1
etape = 1

def reglage_pins(pin1, pin2, pin3, pin4):
    pin_bobine_1_1.write_digital(pin1)
    pin_bobine_1_2.write_digital(pin2)
    pin_bobine_2_1.write_digital(pin3)
    pin_bobine_2_2.write_digital(pin4) 

def prochainStep():
    if (etape == 1):
        reglage_pins(1, 0, 1, 0)
    if (etape == 2):
        reglage_pins(0, 1, 1, 0)
    if (etape == 3):
        reglage_pins(0, 1, 0, 1)
    if (etape == 4):
        reglage_pins(1, 0, 0, 1)
     
while True:

    if button_a.is_pressed():
        if marcheavant:
            marcheavant = 0
            display.show(antihor)
        else:
            marcheavant = 1
            display.show(horaire)
        sleep(100)
    elif button_b.is_pressed():
        if attente == 100:
            attente = 20
            display.show(plus)
        else:
            attente = 100
            display.show(moins)
        sleep(100)        

    prochainStep()
    if marcheavant:
        etape = etape + 1
        if etape > 4:
            etape = 1
    else:
        etape = etape -1
        if etape < 1:
            etape = 4

    sleep(attente)