<reponame>chaitanya-ycr/arduino
#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# Support module generated by PAGE version 6.0.1
#  in conjunction with Tcl version 8.6
#    Apr 08, 2021 06:47:23 PM IST  platform: Windows NT
#    Apr 09, 2021 07:36:25 PM IST  platform: Windows NT

from time import sleep as wait
import serial.tools.list_ports as ls_ports
import serial
from tkinter import *

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk

    py3 = False
except ImportError:
    import tkinter.ttk as ttk

    py3 = True


class ArduinoAppSupportClass:
    def __init__(self):
        self.img_slide_on = PhotoImage(file=r'images\slide_on.png')
        self.img_slide_off = PhotoImage(file=r'images\slide_off.png')
        self.com_ports_list = []
        self.com_port = None
        self.serial_comm_app = serial.Serial(timeout=0.5)
        self.info_variable = StringVar()
        self.kmtronics_cmd = ''

    def update_buttons(self, obj):
        if 'off' in obj['text']:
            self.relay_control(relay_obj_text=obj['text'].replace('off', '1'))
            obj['text'] = obj['text'].replace('off', 'on')
            obj.configure(image=self.img_slide_on)
        else:
            self.relay_control(relay_obj_text=obj['text'].replace('on', '0'))
            obj['text'] = obj['text'].replace('on', 'off')
            obj.configure(image=self.img_slide_off)

    def relay_control(self, relay_obj_text='00'):
        if self.serial_comm_app.is_open:
            relay_obj_text = relay_obj_text.split('-')
            cmd = f'{relay_obj_text[0][0]}{relay_obj_text[1]}'.encode()
            self.serial_comm_app.write(cmd)
            wait(0.1)
            cmd = f'{relay_obj_text[0][1]}{relay_obj_text[1]}'.encode()
            self.serial_comm_app.write(cmd)
            wait(0.1)
            on_off_text = {'1': 'ON',
                           '0': 'OFF'}
            if relay_obj_text[0] == '12':
                self.info_variable.set(f'ECU Power Turned "{on_off_text[relay_obj_text[1]]}"')
            else:
                self.info_variable.set(f'Relay 3 and 4 Turned "{on_off_text[relay_obj_text[1]]}"')
        else:
            self.info_variable.set('Port not opened. failed to send command')

    def relay_status(self):
        self.serial_comm_app.write(f'STATUS'.encode())
        response = self.serial_comm_app.readline().decode()
        wait(0.1)
        self.info_variable.set(f'{response}')
        response = [status[-1] for status in response.split(',')]
        if (response[0], response[1]) == ('1', '1'):
            w.button_do1.configure(image=self.img_slide_on, text='12-on')
        else:
            w.button_do1.configure(image=self.img_slide_off, text='12-off')
        if (response[2], response[3]) == ('1', '1'):
            w.button_do2.configure(image=self.img_slide_on, text='34-on')
        else:
            w.button_do2.configure(image=self.img_slide_off, text='34-off')

    def open_close_comm_port(self, obj):
        self.com_port = w.combobox_com_port_list.get()
        if self.com_port not in [None, '']:
            if obj['text'] == 'OPEN':
                self.serial_comm_app.port = self.com_port
                if self.serial_comm_app.is_open:
                    self.info_variable.set('Port already opened...')
                else:
                    self.serial_comm_app.open()
                    obj.configure(text='CLOSE', background='#e1031e')
                    self.info_variable.set('Port opened successfully...')
                    wait(2)
                    self.relay_status()
            else:
                if self.serial_comm_app.is_open:
                    self.serial_comm_app.close()
                    obj.configure(text='OPEN', background='#008000')
                    self.info_variable.set('Port closed successfully...')
                else:
                    self.info_variable.set('Port already closed...')
        else:
            self.info_variable.set('select port and try OPEN or CLOSE com port.')

    def get_comports(self):
        self.com_ports_list = []
        for port, desc, hwid in sorted(ls_ports.comports()):
            self.com_ports_list.append(port)
        w.combobox_com_port_list.configure(values=self.com_ports_list)

    def initialize(self):
        w.button_do1.configure(command=lambda obj=w.button_do1: self.update_buttons(obj), image=self.img_slide_off, text='12-off')
        w.button_do2.configure(command=lambda obj=w.button_do2: self.update_buttons(obj), image=self.img_slide_off, text='34-off')
        w.entry_relay1.insert(INSERT, 'ECU Power ON/OFF')
        w.entry_relay2.insert(INSERT, 'Relay 3 and 4')
        w.button_com_port_open_close.configure(command=lambda obj=w.button_com_port_open_close: self.open_close_comm_port(obj))
        w.combobox_com_port_list.configure(postcommand=self.get_comports)
        self.info_variable.set('select port and try OPEN or CLOSE com port.')
        w.label_com_port_status_info.configure(textvariable=self.info_variable)


def set_Tk_var():
    global combobox
    combobox = tk.StringVar()


def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top
    ap = ArduinoAppSupportClass()
    ap.initialize()


def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None


if __name__ == '__main__':
    import arduino_uno_gui

    arduino_uno_gui.vp_start_gui()

#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# Support module generated by PAGE version 6.0.1
#  in conjunction with Tcl version 8.6
#    Apr 08, 2021 06:47:23 PM IST  platform: Windows NT
#    Apr 09, 2021 07:36:25 PM IST  platform: Windows NT

from time import sleep as wait
import serial.tools.list_ports as ls_ports
import serial
from tkinter import *

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk

    py3 = False
except ImportError:
    import tkinter.ttk as ttk

    py3 = True


class ArduinoAppSupportClass:
    def __init__(self):
        self.img_slide_on = PhotoImage(file=r'images\slide_on.png')
        self.img_slide_off = PhotoImage(file=r'images\slide_off.png')
        self.com_ports_list = []
        self.com_port = None
        self.serial_comm_app = serial.Serial(timeout=0.5)
        self.info_variable = StringVar()
        self.kmtronics_cmd = ''

    def update_buttons(self, obj):
        if 'off' in obj['text']:
            self.relay_control(relay_obj_text=obj['text'].replace('off', '1'))
            obj['text'] = obj['text'].replace('off', 'on')
            obj.configure(image=self.img_slide_on)
        else:
            self.relay_control(relay_obj_text=obj['text'].replace('on', '0'))
            obj['text'] = obj['text'].replace('on', 'off')
            obj.configure(image=self.img_slide_off)

    def relay_control(self, relay_obj_text='00'):
        if self.serial_comm_app.is_open:
            relay_obj_text = relay_obj_text.split('-')
            cmd = f'{relay_obj_text[0][0]}{relay_obj_text[1]}'.encode()
            self.serial_comm_app.write(cmd)
            wait(0.1)
            cmd = f'{relay_obj_text[0][1]}{relay_obj_text[1]}'.encode()
            self.serial_comm_app.write(cmd)
            wait(0.1)
            on_off_text = {'1': 'ON',
                           '0': 'OFF'}
            if relay_obj_text[0] == '12':
                self.info_variable.set(f'ECU Power Turned "{on_off_text[relay_obj_text[1]]}"')
            else:
                self.info_variable.set(f'Relay 3 and 4 Turned "{on_off_text[relay_obj_text[1]]}"')
        else:
            self.info_variable.set('Port not opened. failed to send command')

    def relay_status(self):
        self.serial_comm_app.write(f'STATUS'.encode())
        response = self.serial_comm_app.readline().decode()
        wait(0.1)
        self.info_variable.set(f'{response}')
        response = [status[-1] for status in response.split(',')]
        if (response[0], response[1]) == ('1', '1'):
            w.button_do1.configure(image=self.img_slide_on, text='12-on')
        else:
            w.button_do1.configure(image=self.img_slide_off, text='12-off')
        if (response[2], response[3]) == ('1', '1'):
            w.button_do2.configure(image=self.img_slide_on, text='34-on')
        else:
            w.button_do2.configure(image=self.img_slide_off, text='34-off')

    def open_close_comm_port(self, obj):
        self.com_port = w.combobox_com_port_list.get()
        if self.com_port not in [None, '']:
            if obj['text'] == 'OPEN':
                self.serial_comm_app.port = self.com_port
                if self.serial_comm_app.is_open:
                    self.info_variable.set('Port already opened...')
                else:
                    self.serial_comm_app.open()
                    obj.configure(text='CLOSE', background='#e1031e')
                    self.info_variable.set('Port opened successfully...')
                    wait(2)
                    self.relay_status()
            else:
                if self.serial_comm_app.is_open:
                    self.serial_comm_app.close()
                    obj.configure(text='OPEN', background='#008000')
                    self.info_variable.set('Port closed successfully...')
                else:
                    self.info_variable.set('Port already closed...')
        else:
            self.info_variable.set('select port and try OPEN or CLOSE com port.')

    def get_comports(self):
        self.com_ports_list = []
        for port, desc, hwid in sorted(ls_ports.comports()):
            self.com_ports_list.append(port)
        w.combobox_com_port_list.configure(values=self.com_ports_list)

    def initialize(self):
        w.button_do1.configure(command=lambda obj=w.button_do1: self.update_buttons(obj), image=self.img_slide_off, text='12-off')
        w.button_do2.configure(command=lambda obj=w.button_do2: self.update_buttons(obj), image=self.img_slide_off, text='34-off')
        w.entry_relay1.insert(INSERT, 'ECU Power ON/OFF')
        w.entry_relay2.insert(INSERT, 'Relay 3 and 4')
        w.button_com_port_open_close.configure(command=lambda obj=w.button_com_port_open_close: self.open_close_comm_port(obj))
        w.combobox_com_port_list.configure(postcommand=self.get_comports)
        self.info_variable.set('select port and try OPEN or CLOSE com port.')
        w.label_com_port_status_info.configure(textvariable=self.info_variable)


def set_Tk_var():
    global combobox
    combobox = tk.StringVar()


def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top
    ap = ArduinoAppSupportClass()
    ap.initialize()


def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None


if __name__ == '__main__':
    import arduino_uno_gui

    arduino_uno_gui.vp_start_gui()
#! /usr/bin/env python #  -*- coding: utf-8 -*- # # Support module generated by PAGE version 6.0.1 #  in conjunction with Tcl version 8.6 #    Apr 08, 2021 06:47:23 PM IST  platform: Windows NT #    Apr 09, 2021 07:36:25 PM IST  platform: Windows NT # Function which closes the window.