#!/usr/bin/python
# Banco di Test per Aria G25 rel 1.21
# Software lato Aria G25

import time
import ablib
import serial
import sys
import os

def get_adc(id):
	with open("/sys/bus/iio/devices/iio:device0/in_voltage%d_raw" % id) as f:
		value=int(f.read())*2.85/1024
		return value

#greenled PC20
#redled PC21

# http://domoticx.com/terminal-codes-ansivt100/

print "test start"

# LED test

LEDR = ablib.Pin('N23','LOW')
LEDG = ablib.Pin('N22','HIGH')
time.sleep(0.1)
LEDR = ablib.Pin('N23','HIGH')
LEDG = ablib.Pin('N22','LOW')
time.sleep(0.1)
LEDR = ablib.Pin('N23','LOW')
LEDG = ablib.Pin('N22','HIGH')
time.sleep(0.1)
LEDR = ablib.Pin('N23','HIGH')
LEDG = ablib.Pin('N22','LOW')
time.sleep(0.1)
LEDR = ablib.Pin('N23','LOW')
LEDG = ablib.Pin('N22','LOW')

# RAM test

if os.system("memtester 50K 1 > /dev/null")==0:
	print "test ram test ok"
else:
	print "test ram test ko"

# LAN test

if os.system("wget http://192.168.0.2 -O /dev/null")==0:
	print "test lan test ok"
else:
	print "test lan test ko"

# USB test

if os.system("mount -t vfat /dev/sda1 /mnt/usbkey1")==0:
	print "test usb1 test ok"
else:
	print "test usb1 test ko"

if os.system("mount -t vfat /dev/sdb1 /mnt/usbkey2")==0:
        print "test usb2 test ok"
else:
        print "test usb2 test ko"

if os.system("mount -t vfat /dev/sdc1 /mnt/usbkey3")==0:
        print "test usb3 test ok"
else:
        print "test usb3 test ko"

# GPIO test

test = {
	"N" : [
		["2","3"],
		["4","5"],
		["6","7"],
		["8","9"],
		["10","11"],
		["12","13"],
		["14","15"],
		["16","17"],
		["18","19"],
		["20","21"],
	],
	"E" : [
		["2","3"],
		["4","5"],
		["6","7"],
		["8","9"],
		["10","11"],
	],
	"S" : [
		["23","22"],
		["21","20"],
		["19","18"],
		["17","16"],
		["15","12"],
		["11","10"],
		["9","2"],
	],
	"W" : [
		["9","10"],
		["11","12"],
		["13","14"],
		["15","16"],
		["17","18"],
	],
}

pin_open = ablib.Pin('N20','INPUT')
pin_test = ablib.Pin('N21','INPUT')

print "if N21 is LOW do not test GPIOs"
time.sleep(0.1)

gpio_errors=0;
if pin_test.get_value()<>0:
	print "Test N21 (PC19) is HIGH"
	while True:
		for connector_index, connector_item in enumerate(test):
			#print connector_index, connector_item, test[connector_item]

			for test_index, test_item in enumerate(test[connector_item]):
				#for pin_index, pin_item in enumerate(test[connector_item][test_index]):
				#print "Test %s.%s - %s.%s" % (connector_item,test_item[0],connector_item,test_item[1])

				#TEST A pin su anodo alto
				#  Il pin sul catodo deve valere 1

				pin_out = ablib.Pin(connector_item+test_item[0],'HIGH')
				pin_in = ablib.Pin(connector_item+test_item[1],'INPUT')

				if pin_in.get_value()<>1:
					print "TEST A: Pin %s.%s o %s.%s in corto verso massa" % (connector_item,test_item[0],connector_item,test_item[1])
					gpio_errors+=1

				#TEST B pin su anodo basso
				#  Il pin sul catodo deve valere 1

				pin_out = ablib.Pin(connector_item+test_item[0],'LOW')
				pin_in = ablib.Pin(connector_item+test_item[1],'INPUT')

				if pin_in.get_value()<>1:
					print "TEST B: Pin %s.%s e %s.%s in corto" % (connector_item,test_item[0],connector_item,test_item[1])
					gpio_errors+=1

				#TEST C pin su catodo basso
				#  Il pin sul anodo deve valere 0

				pin_in = ablib.Pin(connector_item+test_item[0],'INPUT')
				pin_out = ablib.Pin(connector_item+test_item[1],'LOW')

				if pin_in.get_value()<>0:
					print "TEST C: Pin %s.%s o %s.%s staccati" % (connector_item,test_item[0],connector_item,test_item[1])
					gpio_errors+=1

				#TEST D pin su catodo alto
				#  Il pin sul anodo deve valere 1

				pin_in = ablib.Pin(connector_item+test_item[0],'INPUT')
				pin_out = ablib.Pin(connector_item+test_item[1],'HIGH')

				if pin_in.get_value()<>1:
					print "TEST D: Pin %s.%s o %s.%s in corto verso massa" % (connector_item,test_item[0],connector_item,test_item[1])
					gpio_errors+=1

				# ripristina il pin di uscita in ingresso
				pin_out = ablib.Pin(connector_item+test_item[1],'INPUT')


		if gpio_errors==0:
			print "test gpio test ok"
			break
		else:
			print "test gpio test ko"
			LEDR = ablib.Pin('N23','HIGH')
			LEDG = ablib.Pin('N22','LOW')
			break;
else:
	print "Test N21 (PC19) is LOW: no GPIO test done"

#Test ADC

print "adc(0) %.2f" % get_adc(0)
print "adc(1) %.2f" % get_adc(1)
print "adc(2) %.2f" % get_adc(2)
print "adc(3) %.2f" % get_adc(3)

print "test end"

