Keep resources.py in sync with backend

philcrump-phil-add-ntp
Marek Ventur 2018-08-21 20:58:57 +01:00
parent ae7c18d062
commit 68e0c97cbf
6 changed files with 147 additions and 1 deletions

View File

@ -87,9 +87,10 @@ def get_resources(path):
if upip_lib.startswith(".") or upip_lib == "__pycache__":
continue
full_lib_path = os.path.join(full_path, upip_lib)
rel_lib_path = os.path.join(sub_path, upip_lib)
files = {}
if os.path.isfile(full_lib_path):
files = {full_lib_path: None}
files = {rel_lib_path: None}
upip_lib = upip_lib.rsplit('.', 1)[0]
else:
for rel_path in _scan_files(full_lib_path, os.path.join(sub_path, upip_lib)):

8
adhoc.py Normal file
View File

@ -0,0 +1,8 @@
# Fancy a quick experiment? No problem, just code it in here and run
# "./tilda_tools run adhoc.py"
import ugfx, ugfx_helper, dialogs
ugfx_helper.init()
ugfx.clear()

70
lib/speaker.py Normal file
View File

@ -0,0 +1,70 @@
"""Library for speaker related functions"""
from machine import Pin, PWM
from math import log, pow
from time import sleep_ms
_enabled = True
_amp_pin = Pin(Pin.GPIO_AMP_SHUTDOWN)
_speaker_pwm = None
def enabled(value=None):
"""Sets the internal amplifier. This is entirely independent from the PWM"""
global _enabled
if value == None:
return _enabled
else:
_enabled = value
_set_amp()
def _set_amp():
if enabled():
_amp_pin.on()
else:
_amp_pin.off()
def speaker_pwm():
global _speaker_pwm
if not _speaker_pwm:
_speaker_pwm = PWM(PWM.PWM_SPEAKER)
return _speaker_pwm
_frequency = None
def frequency(value = None):
global _frequency
if value == None:
return _frequency
_frequency = value
_set_amp()
speaker_pwm().init(duty=50, freq=_frequency)
def stop():
global _frequency
# todo: maybe we should deinit the PWM?
_frequency = None
speaker_pwm().duty(0)
# Music
NOTES = {"C":0, "C#":1, "D":2, "D#":3, "E":4, "F":5, "F#":6, "G":7, "G#":8, "A":9, "A#":10, "B":11}
def note_to_frequency(note):
if (len(note) == 2) and note[1].isdigit():
octave = int(note[1])
note = note[0].upper()
elif len(note) == 3:
octave = int(note[2])
note = note[0:2].upper()
else:
octave = 4
note = note.upper()
if note not in NOTES:
raise Exception("%s it not a valid note" % note)
halftones = NOTES[note] + 12 * (octave - 4)
freq = 440 * pow(1.059, halftones)
return round(freq)
def note(note):
frequency(note_to_frequency(note))

20
lib/test_buttons.py Normal file
View File

@ -0,0 +1,20 @@
"""Tests for icons lib"""
___license___ = "MIT"
___dependencies___ = ["upip:unittest", "buttons"]
import unittest, ugfx, time
from buttons import *
class TestButtons(unittest.TestCase):
def test_is_pressed(self):
self.assertFalse(is_pressed(Buttons.BTN_9))
def test_is_triggered(self):
self.assertFalse(is_triggered(Buttons.BTN_9))
if __name__ == '__main__':
unittest.main()

34
lib/test_speaker.py Normal file
View File

@ -0,0 +1,34 @@
"""Tests for http"""
___license___ = "MIT"
___dependencies___ = ["upip:unittest", "speaker", "sleep"]
import unittest, speaker
from sleep import *
class TestSpeaker(unittest.TestCase):
def tearDown(self):
speaker.stop()
def test_enabled(self):
self.assertEqual(speaker.enabled(), True)
speaker.enabled(False)
self.assertEqual(speaker.enabled(), False)
speaker.enabled(True)
self.assertEqual(speaker.enabled(), True)
def test_beep_and_stop(self):
for f in range(50, 1000):
speaker.frequency(f)
self.assertEqual(speaker.frequency(), f)
sleep_ms(1)
speaker.stop()
def test_note(self):
for n in ["c", "d", "e", "f", "g", "a", "b", "c5"]:
speaker.note(n)
sleep_ms(100)
if __name__ == '__main__':
unittest.main()

13
lib/ugfx_helper.py Normal file
View File

@ -0,0 +1,13 @@
"""Helper library for ugfx stuff"""
___license___ = "MIT"
import ugfx
from machine import Pin
def init():
ugfx.init()
Pin(Pin.PWM_LCD_BLIGHT).on()
def deinit():
Pin(Pin.PWM_LCD_BLIGHT).off()