/*************************main.py*******************************************************/

import time 
import graph

hor_pixel = 0 

while True:
    if(elapsed_time() >= 10):
   		temp_list = read_sensor_data()   #contain 4 values in a list
   		graph.print_val(temp_list , hor_pixel)
   		hor_pixel = hor_pixel+1;
   		if(hor_pixel >= 100):
            hor_pixel = 0
            graph.close();
            graph.init()
          
          
except KeyboardInterrupt: 
    graph.close();
    GPIO.cleanup()
    
    
graph.close();
GPIO.cleanup()    
            

/*************************graph.py*******************************************************/


import matplotlib.pyplot as plt
import time


def init():
    global fig,rect,ax1,ax2,ax3,ax4,plt

    fig = plt.figure()
    rect = fig.patch
    rect.set_facecolor('#31312e')
    ax1 = fig.add_subplot(2,2,1, axisbg='grey')
    #ax1.plot(x, y, 'c', linewidth=3.3)
    ax1.set_xlim([0,100])
    ax1.set_ylim([0,100])
    ax1.tick_params(axis='x', colors='c')
    ax1.tick_params(axis='y', colors='c')
    ax1.spines['bottom'].set_color('w')
    ax1.spines['top'].set_color('w')
    ax1.spines['left'].set_color('w')
    ax1.spines['right'].set_color('w')
    ax1.yaxis.label.set_color('c')
    ax1.xaxis.label.set_color('c')
    ax1.set_title('Windspeed', color = 'c')
    ax1.set_xlabel('Read')
    ax1.set_ylabel('Value')

    ax2 = fig.add_subplot(2,2,2, axisbg='grey')
    #ax2.plot(x, y, 'c', linewidth=3.3)
    ax2.set_xlim([0,100])
    ax2.set_ylim([0,100])
    ax2.tick_params(axis='x', colors='c')
    ax2.tick_params(axis='y', colors='c')
    ax2.spines['bottom'].set_color('w')
    ax2.spines['top'].set_color('w')
    ax2.spines['left'].set_color('w')
    ax2.spines['right'].set_color('w')
    ax2.yaxis.label.set_color('c')
    ax2.xaxis.label.set_color('c')
    ax2.set_title('Rainfall', color = 'c')
    ax2.set_xlabel('Read')
    ax2.set_ylabel('Value')

    ax3 = fig.add_subplot(2,2,3, axisbg='grey')
    #ax3.plot(x, y, 'c', linewidth=3.3)
    ax3.set_xlim([0,100])
    ax3.set_ylim([-100,100])
    ax3.tick_params(axis='x', colors='c')
    ax3.tick_params(axis='y', colors='c')
    ax3.spines['bottom'].set_color('w')
    ax3.spines['top'].set_color('w')
    ax3.spines['left'].set_color('w')
    ax3.spines['right'].set_color('w')
    ax3.yaxis.label.set_color('c')
    ax3.xaxis.label.set_color('c')
    ax3.set_title('Temperature', color = 'c')
    ax3.set_xlabel('Read')
    ax3.set_ylabel('C')


    ax4 = fig.add_subplot(2,2,4, axisbg='grey')
    #ax4.plot(x, y, 'c', linewidth=3.3)
    ax4.set_xlim([0,100])
    ax4.set_ylim([0,100])
    ax4.tick_params(axis='x', colors='c')
    ax4.tick_params(axis='y', colors='c')
    ax4.spines['bottom'].set_color('w')
    ax4.spines['top'].set_color('w')
    ax4.spines['left'].set_color('w')
    ax4.spines['right'].set_color('w')
    ax4.yaxis.label.set_color('c')
    ax4.xaxis.label.set_color('c')
    ax4.set_title('Humidity', color = 'c')
    ax4.set_xlabel('Read')
    ax4.set_ylabel('Humidity')

    plt.ion()
    plt.show()


def print_val(temp_list , hor_var):
    global ax1,ax2,ax3,ax4,plt
            
    ax1.scatter(hor_var, temp_list[0])
    ax2.scatter(hor_var, temp_list[1])
    ax3.scatter(hor_var, temp_list[2])
    ax4.scatter(hor_var, temp_list[3])
    plt.draw()
    time.sleep(0.05)

    
def close():
    global plt
    plt.close()