Skip to content

Commit

Permalink
Merge pull request #12 from brainelectronics/feature/remove-neopixel-…
Browse files Browse the repository at this point in the history
…usage-remove-latest-scan-logging

Remove neopixel usage and remove latest scan logging
  • Loading branch information
brainelectronics authored Mar 20, 2022
2 parents 9b0bf83 + 81acc28 commit b5d355c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
17 changes: 16 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
-->

## [Unreleased]
## [1.4.0] - 2022-03-20
### Added
- Virtual oneshot timer is created and started on `latest_scan` property
access to stop the scanning thread again after 10.5x of `scan_interval`.
This reduces CPU load and avoids unused scans.

### Changed
- Scanning thread is started on `latest_scan` property access
- Scan data is no logger logged with info level on `latest_scan` property
access to reduce time before data return, see [#11][ref-issue-11]
- Neopixel is no longer used to allow user of lib to use it as desired by its
higher level application

## [1.3.0] - 2022-03-11
### Changed
- Index page uses cards instead of list to show available pages
Expand Down Expand Up @@ -129,15 +142,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `sendfile` function implemented in same way as on Micropythons PicoWeb

<!-- Links -->
[Unreleased]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager/compare/1.3.0...develop
[Unreleased]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager/compare/1.4.0...develop

[1.4.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.4.0
[1.3.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.3.0
[1.2.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.2.0
[1.1.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.1.0
[1.0.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.0.0
[0.1.1]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/0.1.1
[0.1.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/0.1.0

[ref-issue-11]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager/issues/11
[ref-pypi]: https://pypi.org/
[ref-pfalcon-picoweb-sdist-upip]: https://github.com/pfalcon/picoweb/blob/b74428ebdde97ed1795338c13a3bdf05d71366a0/sdist_upip.py
[ref-be-micropython-module]: https://github.com/brainelectronics/micropython-modules/tree/1.1.0
2 changes: 1 addition & 1 deletion wifi_manager/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version_info__ = ('1', '3', '0')
__version_info__ = ('1', '4', '0')
__version__ = '.'.join(__version_info__)
__author__ = 'brainelectronics'
48 changes: 33 additions & 15 deletions wifi_manager/wifi_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
# custom packages
from be_helpers.generic_helper import GenericHelper
from be_helpers.message import Message
from be_helpers.led_helper import Neopixel
from be_helpers.path_helper import PathHelper
from be_helpers.wifi_helper import WifiHelper

Expand All @@ -58,9 +57,6 @@ def __init__(self, logger=None, quiet=False, name=__name__):

self.app = picoweb.WebApp(pkg='/lib')
self.wh = WifiHelper()
self.pixel = Neopixel()
self.pixel.color = 'yellow'
self.pixel.intensity = 20

self.event_sinks = set()

Expand All @@ -86,6 +82,7 @@ def __init__(self, logger=None, quiet=False, name=__name__):
self._scan_net_msg = Message()
self._scan_net_msg.set([]) # empty list, required by save_wifi_config
self._latest_scan = None
self._stop_scanning_timer = None

# start the WiFi scanning thread as soon as "start_config" is called
self.scanning = False
Expand Down Expand Up @@ -438,16 +435,13 @@ def configured_networks(self) -> List[str]:
return self._configured_networks

def _scan(self,
pixel: Neopixel,
wh: WifiHelper,
msg: Message,
scan_interval: int,
lock: int) -> None:
"""
Scan for available networks.
:param pixel: Neopixel helper object
:type pixel: Neopixel
:param wh: Wifi helper object
:type wh: WifiHelper
:param msg: The shared message from this thread
Expand All @@ -457,8 +451,6 @@ def _scan(self,
:param lock: The lock object
:type lock: _thread.lock
"""
# pixel.fading = True

while lock.locked():
try:
# rescan for available networks
Expand All @@ -472,7 +464,6 @@ def _scan(self,
except KeyboardInterrupt:
break

# pixel.fading = False
print('Finished scanning')

@property
Expand Down Expand Up @@ -525,7 +516,6 @@ def scanning(self, value: int) -> None:

# parameters of the _scan function
params = (
self.pixel,
self.wh,
self._scan_net_msg,
self._scan_interval,
Expand All @@ -537,13 +527,41 @@ def scanning(self, value: int) -> None:
# stop scanning if not already stopped
self._scan_lock.release()
self.logger.info('Scanning stoppped')
if isinstance(self._stop_scanning_timer, machine.Timer):
self._stop_scanning_timer.deinit()

def _stop_scanning_cb(self, tim: machine.Timer) -> None:
"""
Timer callback function to stop the WiFi scan thread.
:param tim: The timer calling this function.
:type tim: machine.Timer
"""
self.scanning = False

@property
def latest_scan(self) -> Union[List[dict], str]:
latest_scan_result = self._scan_net_msg.value()
self.logger.info('Requested latest scan result: {}'.
format(latest_scan_result))
return latest_scan_result
"""
Get lastest scanned networks.
Start scanning if not already scanning, set a oneshot timer to 10.5x
of @see scan_interval to stop scanning again in order to reduce CPU
load and avoid unused scans
:returns: Dictionary of available networks
:rtype: Union[List[dict], str]
"""
if not self.scanning:
self.scanning = True

if self._stop_scanning_timer is None:
self._stop_scanning_timer = machine.Timer(-1)
cb_period = int(self.scan_interval * 10 + self.scan_interval / 2)
self._stop_scanning_timer.init(mode=machine.Timer.ONE_SHOT,
period=cb_period,
callback=self._stop_scanning_cb)

return self._scan_net_msg.value()

def _render_index_page(self, available_pages: dict) -> str:
"""
Expand Down

0 comments on commit b5d355c

Please sign in to comment.