# Untitled

unknown
plain_text
a year ago
1.6 kB
0
Indexable
Never
```import time
from datetime import date, datetime
import math

def generate_pwm_const_cycle(freq_list, duty_cycle):
timestamp = datetime.now()
for freq in freq_list:
cycle_time = 1/freq
time_on = duty_cycle * cycle_time
time_off = cycle_time - time_on

if time_on:
delta = datetime.now() - timestamp
print(f'{delta.total_seconds()}' +  "\t" + "1")
time.sleep(time_on)

if time_off:
delta = datetime.now() - timestamp
print(f'{delta.total_seconds()}' +  "\t" + "0")
time.sleep(time_off)

def generate_pwm_const_freq(freq, duty_cycles):
timestamp = datetime.now()
for duty_cycle in duty_cycles:
cycle_time = 1/freq
time_on = duty_cycle * cycle_time
time_off = cycle_time - time_on

if time_on:
delta = datetime.now() - timestamp
print(f'{delta.total_seconds()}' +  "\t" + "1")
time.sleep(time_on)

if time_off:
delta = datetime.now() - timestamp
print(f'{delta.total_seconds()}' +  "\t" + "0")
time.sleep(time_off)

if __name__ == "__main__":
generate_pwm_const_cycle([1, 2, 3, 4, 5, 4, 3, 2, 1], .5)   #w sumie 18 wygenerowanych sygnalow (9 okresow roznej dlugosci, ale kazdy z okresow zawiera 0.5T sygnalu nieskiego i 0.5T sygnalu wysokiego)
print("----------------------------------------------")
generate_pwm_const_freq(5, [0.1, 0.2, 0.3, 0.4, 0.5])       #w sumie 10 wygenerowanych sygnalow(5 okresow kazdy o stalej dlugosci ale roznym czasie sygnalu wysokiego i niskiego)```