more examples and bug fixes

doublebuf
Ondřej Hruška 7 years ago
parent 85058fcf29
commit 634093507f
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 1
      gex/__init__.py
  2. 43
      gex/units/PWMDim.py
  3. 66
      owtest.py
  4. 45
      pwmbeep.py
  5. 15
      pwmbeep2.py

@ -20,6 +20,7 @@ from gex.units.ADC import ADC
from gex.units.SIPO import SIPO from gex.units.SIPO import SIPO
from gex.units.FCAP import FCAP from gex.units.FCAP import FCAP
from gex.units.TOUCH import TOUCH from gex.units.TOUCH import TOUCH
from gex.units.PWMDim import PWMDim
# General, low level # General, low level

@ -0,0 +1,43 @@
import gex
CMD_SET_FREQUENCY = 0
CMD_SET_DUTY = 1
CMD_STOP = 2
CMD_START = 3
class PWMDim(gex.Unit):
"""
Simple 4-channel PWM output with a common frequency
"""
def _type(self):
return 'PWMDIM'
def set_frequency(self, hertz:int, confirm=True):
""" Set freq """
pb = gex.PayloadBuilder()
pb.u32(hertz)
self._send(CMD_SET_FREQUENCY, pb.close(), confirm=confirm)
def set_duty(self, duty_dict, confirm=True):
""" Set duty (dict - number1234 -> duty 0-1000) """
pb = gex.PayloadBuilder()
for (k,v) in enumerate(duty_dict):
pb.u8(k-1)
pb.u16(v)
self._send(CMD_SET_DUTY, pb.close(), confirm=confirm)
def set_duty_single(self, ch1234, duty1000, confirm=True):
""" Set duty of a single channel """
pb = gex.PayloadBuilder()
pb.u8(ch1234-1)
pb.u16(duty1000)
self._send(CMD_SET_DUTY, pb.close(), confirm=confirm)
def stop(self, confirm=True):
self._send(CMD_STOP, confirm=confirm)
def start(self, confirm=True):
self._send(CMD_START, confirm=confirm)

@ -0,0 +1,66 @@
#!/bin/env python3
import time
import gex
with gex.Client(gex.TrxRawUSB()) as client:
ow = gex.OneWire(client, 'ow')
# print("Presence: ", ow.test_presence())
print("Devices:", ow.search())
def meas(addr):
ow.write([0x44], addr=addr)
ow.wait_ready()
data = ow.query([0xBE], 9, addr=addr)
pp = gex.PayloadParser(data)
return pp.i16() * 0.0625
while True:
a = meas(6558392391241695016)
b = meas(1802309978572980008)
print("in: %.2f °C, out: %f °C" % (a, b))
# # search the bus for alarm
# if False:
# ow = gex.OneWire(client, 'ow')
# print("Presence: ", ow.test_presence())
# print("Devices w alarm:", ow.search(alarm=True))
#
# # simple 1w check
# if False:
# ow = gex.OneWire(client, 'ow')
# print("Presence: ", ow.test_presence())
# print("ROM: 0x%016x" % ow.read_address())
# print("Scratch:", ow.query([0xBE], rcount=9, addr=0x7100080104c77610, as_array=True))
#
# # testing ds1820 temp meas without polling
# if False:
# ow = gex.OneWire(client, 'ow')
# print("Presence: ", ow.test_presence())
# print("Starting measure...")
# ow.write([0x44])
# time.sleep(1)
# print("Scratch:", ow.query([0xBE], 9))
#
# # testing ds1820 temp meas with polling
# if False:
# ow = gex.OneWire(client, 'ow')
# print("Presence: ", ow.test_presence())
# print("Starting measure...")
# ow.write([0x44])
# ow.wait_ready()
# data = ow.query([0xBE], 9)
#
# pp = gex.PayloadParser(data)
#
# temp = pp.i16()/2.0
# th = pp.i8()
# tl = pp.i8()
# reserved = pp.i16()
# remain = float(pp.u8())
# perc = float(pp.u8())
#
# realtemp = temp - 0.25+(perc-remain)/perc
# print("Temperature = %f °C (th %d, tl %d)" % (realtemp, th, tl))

@ -0,0 +1,45 @@
import time
import gex
C3 = 130.81; Cx3 = 138.59; D3 = 146.83; Dx3 = 155.56; E3 = 164.81; F3 = 174.61
Fx3 = 185.00; G3 = 196.00; Gx3 = 207.65; A3 = 220.00; Ax3 = 233.08; B3 = 246.94
C4 = 261.63; Cx4 = 277.18; D4 = 293.66; Dx4 = 311.13; E4 = 329.63; F4 = 349.23
Fx4 = 369.99; G4 = 392.00; Gx4 = 415.30; A4 = 440.00; Ax4 = 466.16; B4 = 493.88
C5 = 523.25; Cx5 = 554.37; D5 = 587.33; Dx5 = 622.25; E5 = 659.25; F5 = 698.46
Fx5 = 739.99; G5 = 783.99; Gx5 = 830.61; A5 = 880.00; Ax5 = 932.33; B5 = 987.77
with gex.Client(gex.TrxRawUSB()) as client:
pwm = gex.PWMDim(client, 'dim')
# O O/ #/ #~ #=
# 16, 8, 4, 2, 1
notes = [
(G3, 2),
(G4, 2), (E4, 6), (G4, 2), (E4, 2), (A4, 6), (0, 2), (B4, 2),
(G4, 2), (E4, 6), (A4, 2), (G4, 2), (D4, 6), (0, 2), (G3, 2),
(G4, 2), (E4, 6), (D4, 2), (C4, 2), (C5, 6), (0, 2), (A4, 2),
(G4, 2), (E4, 4), (0, 2), (D4, 2), (A3, 2), (C4, 6), (0, 2), (G3, 2),
#rep
(G4, 2), (E4, 6), (G4, 2), (E4, 2), (A4, 6), (0, 2), (B4, 2),
(G4, 2), (E4, 6), (A4, 2), (G4, 2), (D4, 6), (0, 2), (G3, 2),
(G4, 2), (E4, 6), (D4, 2), (C4, 2), (C5, 6), (0, 2), (A4, 2),
(G4, 2), (E4, 6), (D4, 2), (A3, 2), (C4, 6), (0, 2), #(C4, 2),
]
for p in notes:
pwm.stop()
time.sleep(0.01)
f = round(p[0])
print(f)
if f > 0:
pwm.set_frequency(f)
pwm.start()
time.sleep(0.1*p[1])
pwm.stop()

@ -0,0 +1,15 @@
import time
import gex
with gex.Client(gex.TrxRawUSB()) as client:
pwm = gex.PWMDim(client, 'dim')
pwm.start()
pwm.set_duty_single(1, 500)
for i in range(2000, 200, -15):
pwm.set_frequency(i)
time.sleep(0.05)
pwm.stop()
Loading…
Cancel
Save