支援デバイス Ver6コード

支援デバイスVer6コード

# ODEKAKE Beacon 2018ver4
from microbit import *
#import microbit
import music
import radio
import os

display.scroll('RadV6')
radio.config(channel=10, power=5, length =64)#BLE init  length
radio.on()#BLE ON

btnChk = False
myID = 1
grCount  = 1
grChk = False
oneTime = False
missing = False
timerA = 0
timerB = 0
config = '0,0'#myID,grCount,
tune1 = ["D4:1"]
tune2 = ["E4:1"]
tune3 = ["F4:1"]
timB = 30000
interval = 3000#change to 1sec Feb.16
#IDlist = []
#IDlist2 = []
idDic = {}#ID + time dictionary

#File read
if 'config.txt' in os.listdir():
    with open('config.txt') as my_config:
        conftxt = my_config.read()
        conf = conftxt.split(',')#split by commma to list
        myID = int(conf[0])#myID read and set
        grCount  = int(conf[1])#my grCount read and set

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        ##setingMode
        if btnChk is False:
            btnChk = True
            display.scroll('SET')
            display.scroll('ID=' + str(myID))
        elif btnChk is True:
            btnChk = False #end set mode
            shakeChk = False #end shake mode
            display.show(Image.YES)
    elif button_a.is_pressed():
        if btnChk is True and grChk is False:#ID count mode
            if myID < 20:
                myID += 1
            else:
                myID = 0
            display.scroll('ID=' + str(myID) ,delay = 80)
            #sleep(200)
            #display.clear()
        elif btnChk is False and grChk is True:#group count mode
            if grCount < 20:
               grCount += 1
            else:
                grCount = 0
            display.scroll('GR=' + str(grCount) ,delay = 80)
        else:#normal mode
            if(idDic):
                mList2 = []#initialize and clear
                for k, v in idDic.items():
                    sabun2 = timA - v
                    if sabun2 < 10000:#if 20sec no access
                        mList2.append(k)
                nagasa = int(len(mList2)) + 1#own ID add
                display.scroll(str(nagasa), delay = 80)
                if nagasa == int(grCount):
                    display.scroll('OK!', delay = 80)
                    missing = False
                elif nagasa < int(grCount):
                    display.show(Image.SAD)
                else:
                    display.scroll('??', delay = 100)
                    ##music.play(tune2)
                radio.send(str(myID) + ',' +  str(grCount) + ',nohelp,noset')
            else:
                display.show(Image.DUCK)
            sleep(500)
            display.clear()
            
    elif button_b.is_pressed():
        if btnChk is False and grChk is False:#HELP SEND
            radio.config(power=7)#HelpMode
            radio.send(str(myID) + ',' +  str(grCount) + ',HELP' + ',noset')
            radio.config(power=5)#HelpMode
            display.scroll('HELP')
            sleep(800)
            display.clear()
        elif btnChk is False and grChk is True:#end gr set mode
            btnChk = False #end set mode
            grChk = False #end shake mode
            radio.send(str(myID) + ',' +  str(grCount) + ',nohelp' + ',set')
            display.show(Image.YES)
            sleep(500)
            display.clear()
            #File Save
            saveConfig = str(myID) + ',' + str(grCount)
            with open('config.txt', 'w') as my_conf:#save list my_conf
                my_conf.write(saveConfig)
            #with open('gr.txt', 'w') as gr_id:
            #    gr_id.write(str(grCount))
        elif btnChk is True and grChk is False:#end id set mode
            btnChk = False
            grChk = True#to Group set mode
            display.show(Image.YES)
            sleep(500)
            display.clear()
            display.scroll('GR=' + str(grCount))
    #else:
        #display.scroll('')
        
    #radio message 'myID,grCount,HELP'
    try:
        s = radio.receive()
        if(s):
            sList = s.split(',')#split by , to list
            if sList[2] == 'HELP':
                display.scroll('!')
                music.play(music.BADDY)
                display.show(Image.SAD)
                sleep(3000)
                display.clear()
            elif btnChk is False and grChk is False:
                if sList[3] == 'set':
                    grCount = sList[1]
                    saveConfig = str(myID) + ',' + str(grCount)
                    display.scroll('GR=' + str(grCount))
                    with open('config.txt', 'w') as gr_id:
                        gr_id.write(saveConfig)
                #elif sList[2] == 'nohelp' and sList[3] == 'noset':
                else:#recieve ID check
                    if(not myID == sList[0]):#without own device
                        #time check
                        nowTime = running_time()
                        if(not idDic.get(sList[0])):#not in dictionary add ID and time
                            updValue = {sList[0] : nowTime} 
                            idDic.update(updValue)
                            #music.play(tune3)
                        else:
                            updValue = {sList[0]:nowTime} 
                            idDic.update(updValue)
    except ValueError:#PacketError
        display.show(Image.DUCK)
        radio.reset()
        sleep(200)
        radio.config(channel=10, power=5, length =64)
        display.clear()
        
    #send Beacon interval time            
    timA = running_time()
    if timA > timB:
        #del IDlist[:]#initialaize list
        if btnChk is False and grChk is False:
            radio.send(str(myID) + ',' +  str(grCount) + ',nohelp,noset')
        #display.scroll(str(timA))
            timB = timA + interval
            #display.show(Image.PITCHFORK)
            #sleep(100)
            #display.clear()
            #music.play(tune1)
        
    if(idDic):
        mList = []#initialize and clear
        #mList.clear()#need()?
        for k, v in idDic.items():
            sabun2 = timA - v
            if sabun2 > 200000:#200sec
                mList.append(k)
        
        if(mList):
            
            ids  = ''
            for t in mList:
                ids += t + '_'
            #display.scroll('ID=' + ids + 'missing', delay = 200)
            if missing is False:
                music.play(music.POWER_DOWN)
                missing = True
            display.scroll('ID' + ids + 'missing')
            sleep(5000)

    #sleep(100)