File: morse_code.py

package info (click to toggle)
pigpio 1.78-1.1
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 7,088 kB
  • sloc: ansic: 17,891; python: 4,232; sh: 741; cpp: 281; makefile: 135
file content (72 lines) | stat: -rwxr-xr-x 1,537 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python

import pigpio

morse={
'a':'.-'   , 'b':'-...' , 'c':'-.-.' , 'd':'-..'  , 'e':'.'    ,
'f':'..-.' , 'g':'--.'  , 'h':'....' , 'i':'..'   , 'j':'.---' ,
'k':'-.-'  , 'l':'.-..' , 'm':'--'   , 'n':'-.'   , 'o':'---'  ,
'p':'.--.' , 'q':'--.-' , 'r':'.-.'  , 's':'...'  , 't':'-'    ,
'u':'..-'  , 'v':'...-' , 'w':'.--'  , 'x':'-..-' , 'y':'-.--' ,
'z':'--..' , '1':'.----', '2':'..---', '3':'...--', '4':'....-',
'5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.',
'0':'-----'}

GPIO=22

MICROS=100000

NONE=0

DASH=3
DOT=1

GAP=1
LETTER_GAP=3-GAP
WORD_GAP=7-LETTER_GAP

def transmit_string(pi, gpio, str):

   pi.wave_clear() # start a new waveform

   wf=[]

   for C in str:
      c=C.lower()
      print(c)
      if c in morse:
         k = morse[c]
         for x in k:

            if x == '.':
               wf.append(pigpio.pulse(1<<gpio, NONE, DOT * MICROS))
            else:
               wf.append(pigpio.pulse(1<<gpio, NONE, DASH * MICROS))

            wf.append(pigpio.pulse(NONE, 1<<gpio, GAP * MICROS))

         wf.append(pigpio.pulse(NONE, 1<<gpio, LETTER_GAP * MICROS))

      elif c == ' ':
         wf.append(pigpio.pulse(NONE, 1<<gpio, WORD_GAP * MICROS))

   pi.wave_add_generic(wf)

   pi.wave_tx_start()

pi = pigpio.pi()

pi.set_mode(GPIO, pigpio.OUTPUT)

transmit_string(pi, GPIO, "Now is the winter of our discontent")

while pi.wave_tx_busy():
   pass

transmit_string(pi, GPIO, "made glorious summer by this sun of York")

while pi.wave_tx_busy():
   pass

pi.stop()