#! /usr/bin/env python
# This file is part of Project CoBolD.
#
# Project CoBolD is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, # either version 3 of the License, or (at your option) any later version.
#
# Project CoBolD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Project CoBolD.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Permondes
#
# Version: 0.0

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
from gpio import gpio

#   definition of GPIO5-Pins
#   key: pin-number
#   values: usable, class, tooltip
gpio5 = {'GPIO5_1':[False, 'gpio3v3', '+3.3V'],
         'GPIO5_2':[False, 'gpio5v', '+5V'],
         'GPIO5_3':[False, 'gpioDNU', 'UEXT2-SDA'],
         'GPIO5_4':[False, 'gpio5v', '+5V'],
         'GPIO5_5':[False, 'gpioDNU', 'UEXT2-SCL'],
         'GPIO5_6':[False, 'gpioGND', 'GND'],
         'GPIO5_7':[True, 'gpioPinNotInitialized', 'PI15'],
         'GPIO5_8':[False, 'gpioDNU', 'UART3-RX'],
         'GPIO5_9':[False, 'gpioGND', 'GND'],
         'GPIO5_10':[False, 'gpioDNU', 'UART3-TX'],
         'GPIO5_11':[True, 'gpioPinNotInitialized', 'PI11'],
         'GPIO5_12':[True, 'gpioPinNotInitialized', 'PI3'],
         'GPIO5_13':[True, 'gpioPinNotInitialized', 'PI10'],
         'GPIO5_14':[False, 'gpioGND', 'GND'],
         'GPIO5_15':[True, 'gpioPinNotInitialized', 'PI7'],
         'GPIO5_16':[True, 'gpioPinNotInitialized', 'PE6'],
         'GPIO5_17':[False, 'gpio3v3', '+3.3V'],
         'GPIO5_18':[True, 'gpioPinNotInitialized', 'PE5'],
         'GPIO5_19':[False, 'gpioDNU', 'SPI2-MOSI'],
         'GPIO5_20':[False, 'gpioGND', 'GND'],
         'GPIO5_21':[False, 'gpioDNU', 'SPI2-MISO'],
         'GPIO5_22':[True, 'gpioPinNotInitialized', 'PE4'],
         'GPIO5_23':[False, 'gpioDNU', 'SPI2-SCK'],
         'GPIO5_24':[False, 'gpioDNU', 'SPI2-CSO'],
         'GPIO5_25':[False, 'gpioGND', 'GND'],
         'GPIO5_26':[True, 'gpioPinNotInitialized', 'PH14'],
         'GPIO5_27':[False, 'gpioDNU', 'UEXT1-SDA'],
         'GPIO5_28':[False, 'gpioDNU', 'UEXT1-SCL'],
         'GPIO5_29':[True, 'gpioPinNotInitialized', 'PE1'],
         'GPIO5_30':[False, 'gpioGND', 'GND'],
         'GPIO5_31':[True, 'gpioPinNotInitialized', 'PE8'],
         'GPIO5_32':[True, 'gpioPinNotInitialized', 'PH13'],
         'GPIO5_33':[True, 'gpioPinNotInitialized', 'PE9'],
         'GPIO5_34':[False, 'gpioGND', 'GND'],
         'GPIO5_35':[True, 'gpioPinNotInitialized', 'PE10'],
         'GPIO5_36':[True, 'gpioPinNotInitialized', 'PH12'],
         'GPIO5_37':[True, 'gpioPinNotInitialized', 'PE11'],
         'GPIO5_38':[True, 'gpioPinNotInitialized', 'PH11'],
         'GPIO5_39':[False, 'gpioGND', 'GND'],
         'GPIO5_40':[True, 'gpioPinNotInitialized', 'PH10'] }

class Handler:
	def initGPIO5(self, *args):
		''' Initialize GPIO-5 '''
		result = gpio.init() #Initialize module. Always called first
		
		if result < 0: # error
			statusErrorMessage('Error Initializing GPIO-5: %d' %result)
			
		else: # init the GPIO buttons
			for button, characteristics in gpio5.items():
				button_object = builder.get_object(button)
				button_object.set_sensitive(characteristics[0])
	
				button_context = button_object.get_style_context()
				button_context.add_class(characteristics[1])
	
				button_object.set_tooltip_text(characteristics[2])

			# deactivate init button as it has served its purpose
			builder.get_object('Init_GPIO5').set_sensitive(False)
			# all done
			statusMessage('GPIO-5 initialized')
        
	def onDestroy(self, *args):
		''' close and clean up all ports '''
		Gtk.main_quit()


def initCoBolD():
    ''' Prepare the base panel'''
    # deactivate not yet implemented functions
    for button in ["Audio_In", "Audio_Out", "CAN", "UEXT1", "UEXT2"]:
        builder.get_object(button).set_sensitive(False)
        
# Statusbar Handling
#
def statusPush(message, statusclass):
	'''send Message to Statusline'''
	statusbar = builder.get_object('statusbar')
	context = statusbar.get_context_id("statusbar")
	style_context = statusbar.get_style_context()
	style_context.remove_class('statusbarMessage')
	style_context.remove_class('statusbarWarning')
	style_context.remove_class('statusbarError')
	style_context.add_class(statusclass)
	statusbar.push(context, message)
#	print(style_context.list_classes())
	
def statusMessage(message):
	'''send Message to Statusline'''
	statusPush(message,'statusbarMessage')
	
def statusWarning(message):
	'''send Warning to Statusline'''
	statusPush(message,'statusbarWarning')
	
def statusErrorMessage(message):
	''' send Errormessage to Statusline'''
	statusPush(message,'statusbarError')
	
def statusClear():
	''' Clear Statusline'''
	statusMessage('')

# Main program
builder = Gtk.Builder()
builder.add_from_file("CoBolD_Base.glade")
builder.connect_signals(Handler())

css_provider = Gtk.CssProvider()
css_provider.load_from_path('cobold.css')
Gtk.StyleContext().add_provider_for_screen(Gdk.Screen.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

window = builder.get_object("cobold_panel")
window.show_all()
initCoBolD()
Gtk.main()
