#!/usr/bin/env python3
from eye import *

# Ping-Pong radio communication program
# T. Braunl, May 2017

#include "eyebot.h"
MAX = 10

def main():
    id = []*256
    buf = []
    for i in range(MAX + 1): buf.append("00000")

    LCDMenu("MASTER", "SLAVE", "", "END")
    RADIOInit()
    myid = RADIOGetID()
    LCDPrintf("my id %d\n", myid)

    k = KEYGet()
    if k==KEY4: return 0 # exit
    if k==KEY1: # master only
        LCDPrintf("scanning (takes time ...)\n")
        ret = RADIOStatus(id) 
        LCDPrintf("Total of %d robots: ")
        for i in range(ret): LCDPrintf(" %d", id[i])
        LCDPrintf("\n")

        # select partner robot
        if ret<2: 
            LCDPrintf("Not enough partners - Bye\n") 
            return 0
        p=0
        partnerid = id[p]
        if myid==id[p]:
            p += 1 
            partnerid = id[p]
        LCDPrintf("partner is %d\n", partnerid)

        LCDPrintf("I will start\n")
        RADIOSend(partnerid, buf)

    LCDPrintf("I am waiting for partner\n")

    for i in range(10):
        RADIOReceive(partnerid, buf, MAX)
        LCDPrintf("received from %d text %s\n", partnerid, buf)

        num = input()
        num += 1
        print("%d" % num)
        RADIOSend(partnerid, buf)
    KEYWait(KEY4)


if __name__ == "__main__":
    main()



