Advertisement
rr1024

Adafruit Tower Light

Jun 1st, 2025 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | Software | 0 0
  1. import threads_serailport_mgr
  2. import time
  3. RED_ON = 0x11
  4. RED_OFF = 0x21
  5. RED_BLINK = 0x41
  6.  
  7. YELLOW_ON= 0x12
  8. YELLOW_OFF = 0x22
  9. YELLOW_BLINK = 0x42
  10.  
  11. GREEN_ON = 0x14
  12. GREEN_OFF = 0x24
  13. GREEN_BLINK = 0x44
  14.  
  15. BUZZER_ON = 0x18
  16. BUZZER_OFF = 0x28
  17. BUZZER_BLINK = 0x48
  18. # Example usage
  19. if __name__ == "__main__":
  20.     # Define the settings for COM5 exactly as you specified.
  21.     port_settings = {
  22.         'COM5': {
  23.             'baudrate': 9600,
  24.             'bytesize': 8,          # 8 bits per byte.
  25.             'parity': 'N',          # No parity.
  26.             'stopbits': 1,          # 1 stop bit.
  27.             'timeout': None,        # Blocking mode (no timeout).
  28.             'xonxoff': False,       # No software flow control.
  29.             'rtscts': False,        # No hardware (RTS/CTS) flow control.
  30.             'dsrdtr': False         # No hardware (DSR/DTR) flow control.
  31.         }
  32.     }
  33.  
  34.     # Create an instance of the serial port manager with the defined settings.
  35.     mgr = threads_serailport_mgr.serial_port_mgr(port_settings)
  36.     mgr.start()  # Start the thread that opens the ports and monitors for incoming data.
  37.  
  38.     # Allow a short interval for the connection to be established.
  39.     time.sleep(1)
  40.  
  41.     # Prepare your bytes data. (Ensure it's a bytes object)
  42.     data_to_send = b'RED_ON, COM5!'
  43.  
  44.     # Send data in bytes to COM5.
  45.     mgr.send_data('COM5', data_to_send)
  46.  
  47.     # Keep the script running for a little while to allow data reception or further actions.
  48.     time.sleep(2)
  49.  
  50.     # When finished, stop the manager which disconnects all ports and stops the thread.
  51.     mgr.stop()
  52.  
  53.  
  54. ''' This code works but when implemented as is the amps looses connections and won't respond
  55. https://www.adafruit.com/product/5125
  56. import serial
  57. import time
  58.  
  59. serialPort = 'COM5'  # Change to the serial/COM port of the tower light
  60. #serialPort = '/dev/USBserial0'  # on mac/linux, it will be a /dev path
  61. baudRate = 9600
  62.  
  63. RED_ON = 0x11
  64. RED_OFF = 0x21
  65. RED_BLINK = 0x41
  66.  
  67. YELLOW_ON= 0x12
  68. YELLOW_OFF = 0x22
  69. YELLOW_BLINK = 0x42
  70.  
  71. GREEN_ON = 0x14
  72. GREEN_OFF = 0x24
  73. GREEN_BLINK = 0x44
  74.  
  75. BUZZER_ON = 0x18
  76. BUZZER_OFF = 0x28
  77. BUZZER_BLINK = 0x48
  78.  
  79. def sendCommand(serialport, cmd):
  80.    serialport.write(bytes([cmd]))
  81.  
  82. if __name__ == '__main__':
  83.    mSerial = serial.Serial(serialPort, baudRate)
  84.  
  85.    # Clean up any old state
  86.    sendCommand(mSerial, BUZZER_OFF)
  87.    sendCommand(mSerial, RED_OFF)
  88.    sendCommand(mSerial, YELLOW_OFF)
  89.    sendCommand(mSerial, GREEN_OFF)
  90.  
  91.    # turn on each LED set in order
  92.    sendCommand(mSerial, RED_ON)
  93.    time.sleep(0.5)
  94.    sendCommand(mSerial, RED_OFF)
  95.  
  96.    sendCommand(mSerial, YELLOW_ON)
  97.    time.sleep(0.5)
  98.    sendCommand(mSerial, YELLOW_OFF)
  99.  
  100.    sendCommand(mSerial, GREEN_ON)
  101.    time.sleep(0.5)
  102.    sendCommand(mSerial, GREEN_OFF)
  103.  
  104.    # beep!
  105.    sendCommand(mSerial, BUZZER_ON)
  106.    time.sleep(0.1)
  107.    sendCommand(mSerial, BUZZER_OFF)
  108.  
  109.    # Use the built-in blink modes
  110.    sendCommand(mSerial, RED_BLINK)
  111.    time.sleep(3)
  112.    sendCommand(mSerial, RED_OFF)
  113.  
  114.    sendCommand(mSerial, YELLOW_BLINK)
  115.    time.sleep(3)
  116.    sendCommand(mSerial, YELLOW_OFF)
  117.  
  118.    sendCommand(mSerial, GREEN_BLINK)
  119.    time.sleep(3)
  120.    sendCommand(mSerial, GREEN_OFF)
  121.  
  122.    # Please be kind, re-wind!
  123.    sendCommand(mSerial, BUZZER_OFF)
  124.    sendCommand(mSerial, RED_OFF)
  125.    sendCommand(mSerial, YELLOW_OFF)
  126.    sendCommand(mSerial, GREEN_OFF)
  127.    mSerial.close()
  128. '''
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement