src/eric7/MicroPython/Devices/RP2040Devices.py

branch
mpy_network
changeset 9775
c6806d24468b
parent 9772
06ef28082c4d
child 9776
210bf87ae5c7
equal deleted inserted replaced
9774:c7b712056146 9775:c6806d24468b
5 5
6 """ 6 """
7 Module implementing the device interface class for RP2040 based boards 7 Module implementing the device interface class for RP2040 based boards
8 (e.g. Raspberry Pi Pico). 8 (e.g. Raspberry Pi Pico).
9 """ 9 """
10
11 import ast
12 import json
10 13
11 from PyQt6.QtCore import QUrl, pyqtSlot 14 from PyQt6.QtCore import QUrl, pyqtSlot
12 from PyQt6.QtNetwork import QNetworkRequest 15 from PyQt6.QtNetwork import QNetworkRequest
13 from PyQt6.QtWidgets import QMenu 16 from PyQt6.QtWidgets import QMenu
14 17
38 @type QObject 41 @type QObject
39 """ 42 """
40 super().__init__(microPythonWidget, deviceType, parent) 43 super().__init__(microPythonWidget, deviceType, parent)
41 44
42 self.__createRP2040Menu() 45 self.__createRP2040Menu()
46
47 self.__statusTranslations = {
48 "picow": {
49 -3: self.tr('wrong password'),
50 -2: self.tr('no access point found'),
51 -1: self.tr('connection failed'),
52 0: self.tr('idle'),
53 1: self.tr('connecting'),
54 3: self.tr('connection successful'),
55 },
56 "pimoroni": {
57 # TODO: not yet implemented
58 },
59 }
43 60
44 def setButtons(self): 61 def setButtons(self):
45 """ 62 """
46 Public method to enable the supported action buttons. 63 Public method to enable the supported action buttons.
47 """ 64 """
338 import machine 355 import machine
339 rtc = machine.RTC() 356 rtc = machine.RTC()
340 rtc.datetime(rtc_time[:7] + (0,)) 357 rtc.datetime(rtc_time[:7] + (0,))
341 """ 358 """
342 359
360 ##################################################################
361 ## Methods below implement network related methods
362 ##################################################################
363
364 def hasWifi(self):
365 """
366 Public method to check the availability of WiFi.
367
368 @return tuple containing a flag indicating the availability of WiFi
369 and the WiFi type (picow or pimoroni)
370 @rtype tuple of (bool, str)
371 @exception OSError raised to indicate an issue with the device
372 """
373 command = """
374 def has_wifi():
375 try:
376 import network
377 if hasattr(network, 'WLAN'):
378 return True, 'picow'
379 except ImportError:
380 try:
381 import picowireless
382 if picowireless.get_fw_version() != '':
383 return True, 'pimoroni'
384 except ImportError:
385 pass
386
387 return False, ''
388
389 print(has_wifi())
390 del has_wifi
391 """
392 out, err = self._interface.execute(command, timeout=10000)
393 if err:
394 raise OSError(self._shortError(err))
395 return ast.literal_eval(out.decode("utf-8"))
396
397 def getWifiData(self):
398 """
399 Public method to get data related to the current WiFi status
400
401 @return tuple of two dictionaries containing the WiFi status data
402 for the WiFi client and access point
403 @rtype tuple of (dict, dict)
404 """
405 if self._deviceData["wifi_type"] == "picow":
406 command = """
407 def wifi_status():
408 import ubinascii
409 import ujson
410 import network
411
412 wifi = network.WLAN(network.STA_IF)
413 station = {
414 'active': wifi.active(),
415 'connected': wifi.isconnected(),
416 'status': wifi.status(),
417 'ifconfig': wifi.ifconfig(),
418 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
419 'channel': wifi.config('channel'),
420 'txpower': wifi.config('txpower'),
421 }
422 print(ujson.dumps(station))
423
424 wifi = network.WLAN(network.AP_IF)
425 ap = {
426 'active': wifi.active(),
427 'connected': wifi.isconnected(),
428 'status': wifi.status(),
429 'ifconfig': wifi.ifconfig(),
430 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(),
431 'channel': wifi.config('channel'),
432 'txpower': wifi.config('txpower'),
433 'essid': wifi.config('essid'),
434 }
435 print(ujson.dumps(ap))
436
437 wifi_status()
438 del wifi_status
439 """
440 elif self._deviceData["wifi_type"] == "pimoroni":
441 # TODO: not yet implemented
442 pass
443 else:
444 return super().getWifiData()
445
446 out, err = self._interface.execute(command, timeout=10000)
447 if err:
448 raise OSError(self._shortError(err))
449
450 stationStr, apStr = out.decode("utf-8").splitlines()
451 station = json.loads(stationStr)
452 ap = json.loads(apStr)
453 try:
454 station["status"] = self.__statusTranslations[
455 self._deviceData["wifi_type"]
456 ][station["status"]]
457 except KeyError:
458 station["status"] = str(station["status"])
459 try:
460 ap["status"] = self.__statusTranslations[
461 self._deviceData["wifi_type"]
462 ][ap["status"]]
463 except KeyError:
464 ap["status"] = str(ap["status"])
465 return station, ap
466
343 467
344 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): 468 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
345 """ 469 """
346 Function to instantiate a MicroPython device object. 470 Function to instantiate a MicroPython device object.
347 471

eric ide

mercurial