Example #1
0
def hotspot_setup(ssid,
                  channel,
                  hidden,
                  password,
                  authmode,
                  add_postfix=False):
    if not badgehelper.on_badge():
        return False

    sta_if = network.WLAN(network.STA_IF)
    ap_if = network.WLAN(network.AP_IF)

    if sta_if.active():
        sta_if.active(False)

    ap_if.active(True)

    if add_postfix:
        ssid = ssid + "_" + str(time.ticks_ms())

    print("Creating HOTSPOT " + ssid + "...")
    animate_wifi()
    ap_if.config(essid=ssid,
                 channel=channel,
                 hidden=hidden,
                 password=password,
                 authmode=authmode)
    animate_end()
    return True
Example #2
0
  def send_data(self, data):
    if (not self.is_running or self.client == None or not self.is_connected):
      return False 

    to_send = None
    if type(data) == str:
      to_send = data.encode("ascii")
    else:
      to_send = data

    #print("sending data to client: " + to_send.decode("ascii"))
    
    try:
      if badgehelper.on_badge():
        self.client.write(to_send)
      else:
        self.client.send(to_send)
      return True 
    except OSError as err: # Timeout
      print("Timeout exception when sending data to client:")
      print(type(err))
      print(err)
      self.is_connected = False
      return False 
    except Exception as err:
      print("exception when sending data to client:")
      print(type(err))
      print(err)
      self.is_connected = False
      return False 
Example #3
0
  def read_data(self):
    data = []

    if not self.is_connected or not self.is_running:
      return data

    if badgehelper.on_badge() and 1 == 0:
      try:
        data = self.sock.read()
      except OSError as err: # timeout 
        pass
      except Exception as err:
        print("exception when receiving data from host:")
        print(err)
        self.disconnect()
    else:
      try:
        data = self.sock.recv(self.buffer_size)
      except OSError as err: # timeout
        pass
      except Exception as err:
          print("exception when receiving data from host:")
          print(err)
          self.disconnect()
    
    if self.sock.fileno() == -1: 
      self.is_connected = False 

    if type(data) == bytes:
      return data.decode("ascii")
    else:
      return data
Example #4
0
def host_on_disconnect(addr):
    global connected
    connected = False
    print("(host) disconnected:" + addr)
    if badgehelper.on_badge():
        rgb.clear()
        rgb.scrolltext("(host) client disconnected: " + addr, (255, 255, 255))
Example #5
0
def host_on_row():
    global received_rows
    print("(host) row added")
    received_rows += received_rows
    if badgehelper.on_badge():
        rgb.clear()
        rgb.scrolltext("(host) received row " + str(received_rows),
                       (255, 255, 255))
Example #6
0
def animate_wifi():
    if not badgehelper.on_badge():
        return False

    from default_icons import animation_connecting_wifi

    rgb.clear()
    data, size, frames = animation_connecting_wifi
    rgb.framerate(3)
    rgb.gif(data, (12, 0), size, frames)
Example #7
0
def animate_no_wifi():
    if not badgehelper.on_badge():
        return False

    from default_icons import icon_no_wifi

    rgb.clear()
    data, frames = icon_no_wifi
    rgb.framerate(3)
    rgb.gif(data, (12, 0), (8, 8), frames)
    time.sleep(3)
Example #8
0
  def switch(self, type):
    if not badgehelper.on_badge():
      return True

    if type == GAME_HOST_NETWORK_TYPE_NORMAL or type == GAME_CLIENT_NETWORK_TYPE_NORMAL:
      if not wifi.status():
        print("Connecting to Wi-Fi...")
        return uinterface.connect_wifi()
    elif type == GAME_HOST_NETWORK_TYPE_HOTSPOT:
      # define unique hotspot
      return wifi_extended.hotspot_setup(GAME_NETWORK_TYPE_HOTSPOT_SSID, GAME_NETWORK_TYPE_HOTSPOT_CHANNEL, GAME_NETWORK_TYPE_HOTSPOT_HIDDEN, GAME_NETWORK_TYPE_HOTSPOT_PASSWD, GAME_NETWORK_TYPE_HOTSPOT_AUTHMODE, True)
    elif type == GAME_CLIENT_NETWORK_TYPE_HOTSPOT:
      return wifi_extended.hotspot_connect(GAME_NETWORK_TYPE_HOTSPOT_SSID, GAME_NETWORK_TYPE_HOTSPOT_PASSWD, True)

    pass
Example #9
0
  def start(self, ip_address):
    if (ip_address is None or ip_address == "" or len(ip_address) < 7):
      return False 

    switched = self.network_switcher.switch(self.network_type)
    if not switched:
      return False

    self.ip_address = ip_address
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.settimeout(.3)
    self.is_running = True 
    
    if self.use_listener:
      if badgehelper.on_badge():
        self.connect_thread = thread.start_new_thread("client_connect_thread", self._client_handler, ())
      else:
        self.connect_thread = thread.start_new_thread(self._client_handler, ())
Example #10
0
  def start(self):
    switched = self.network_switcher.switch(self.network_type)
    if not switched:
      return False 

    addr = socket.getaddrinfo("0.0.0.0", self.port)[0][-1]
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    self.sock.bind(addr)
    
    self.sock.listen(1) # Only allow one single connection at a time
    self.sock.settimeout(None)
    
    if self.use_listener:
      if badgehelper.on_badge():
        self.listen_thread = thread.start_new_thread("listen_thread", self._listen, ())
      else:
        self.listen_thread = thread.start_new_thread(self._listen, ())

    self.is_running = True 
Example #11
0
def hotspot_connect(ssid, password, ssid_is_prefix=False):
    if not badgehelper.on_badge():
        return False

    sta_if = network.WLAN(network.STA_IF)
    ap_if = network.WLAN(network.AP_IF)

    if ap_if.active():
        ap_if.active(False)

    sta_if.active(True)

    if not sta_if.isconnected():
        if ssid_is_prefix:
            # we need to scan first to get all latest ssid's
            ssids = sta_if.scan()
            ssids_left = []
            for ssid_item in ssids:
                ssid_name = ssid_item[0].decode("ascii")
                if ssid_name.startswith(ssid):
                    ssids_left.append(ssid_name)

            if len(ssids_left) == 0:
                animate_no_wifi()
                animate_end()
                return False

            # get latest
            ssids_left.sort(reverse=True)
            ssid = ssids_left[0]

        print("Connecting to HOTSPOT network " + ssid + "...")
        animate_wifi()
        sta_if.connect(ssid, password)
        wifi.wait()

        if not wifi.status():
            animate_no_wifi()

        animate_end()

    return wifi.status()
Example #12
0
BASE_MODULE = "clienttest"

import sys, time
# We would like to import modules that are added to his folder.
sys.path.append("/apps/" + BASE_MODULE)
sys.path.append("/apps/tetris_cz19")
sys.path.append("H:\\CZ2019\\badge\\tetris_cz19")

import badgehelper

DEBUG = False
connected = False 
received_rows = 0

if badgehelper.on_badge():
  import rgb

import gameservices
import tetrisgameservices

def show_scrolltext(text):
  if badgehelper.on_badge():
    rgb.clear()
    rgb.scrolltext(text, (255,255,255))

def client_on_connect(addr):
  global connected
  connected = True
  print("(client) connected:" + addr)
  show_scrolltext("(client) host connected: " + addr)
Example #13
0
def animate_end():
    if badgehelper.on_badge():
        rgb.clear()
        rgb.framerate(20)
Example #14
0
BASE_MODULE = "servertest"

import sys, time
# We would like to import modules that are added to his folder.
sys.path.append("/apps/" + BASE_MODULE)
sys.path.append("/apps/tetris_cz19")
sys.path.append("H:\\CZ2019\\badge\\tetris_cz19")

import badgehelper
import gameservices, tetrisgameservices

if badgehelper.on_badge():
    import rgb

DEBUG = False
connected = False
received_rows = 0


def host_on_connect(addr):
    global connected
    connected = True
    print("(host) connected:" + addr)
    if badgehelper.on_badge():
        rgb.clear()
        rgb.scrolltext("(host) client connected: " + addr, (255, 255, 255))


def host_on_disconnect(addr):
    global connected
    connected = False
Example #15
0
def show_scrolltext(text):
  if badgehelper.on_badge():
    rgb.clear()
    rgb.scrolltext(text, (255,255,255))
Example #16
0
try:
  import thread
except ImportError:
  import _thread as thread

try:
  import socket
except ImportError:
  import _socket as socket

try:
  import random
except ImportError:
  import urandom as random

if badgehelper.on_badge():
  import wifi, consts, network, uinterface

PORT = 55667        # Port to listen on (non-privileged ports are > 1023)

GAME_HOST_NETWORK_TYPE_NORMAL = 0
GAME_HOST_NETWORK_TYPE_HOTSPOT = 1
GAME_CLIENT_NETWORK_TYPE_NORMAL = 10
GAME_CLIENT_NETWORK_TYPE_HOTSPOT = 11

GAME_NETWORK_TYPE_HOTSPOT_SSID = "AbC_MPCZ19"
GAME_NETWORK_TYPE_HOTSPOT_PASSWD = "Ajk39128asdaD"
GAME_NETWORK_TYPE_HOTSPOT_CHANNEL = 11 
GAME_NETWORK_TYPE_HOTSPOT_HIDDEN = False
GAME_NETWORK_TYPE_HOTSPOT_AUTHMODE = 2
GAME_NETWORK_TYPE_HOTSPOT_SERVERIP = "192.168.4.1"
Example #17
0
def host_on_gameover():
    print("(host) gameover")
    if badgehelper.on_badge():
        rgb.clear()
        rgb.scrolltext("(host) received gameover", (255, 255, 255))