#!   /usr/bin/env   python
#    coding: utf8

# Copyright CERN, 2011
# Author: Samuel Iglesias Gonsalvez <siglesia@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org

import sys
import rr
import time
import os
from ctypes import *
from ptsexcept import *

"""
test09: reads serial number of the temperature sensor.
"""

class COne_wire :

	def __init__ (self,bus, base):
        	self.base = base;
        	self.bus = bus;
		self.serial = self.bus.iread(0, self.base, 4);
		self.serial |= (self.bus.iread(0, self.base + 4, 4) << 32);
		


def main (default_directory='.'):

	path_fpga_loader = '../firmwares/fpga_loader';
	path_firmware = '../firmwares/test09.bin';
    	
	firmware_loader = os.path.join(default_directory, path_fpga_loader)
    	bitstream = os.path.join(default_directory, path_firmware)
    	os.system( firmware_loader + ' ' + bitstream)

    	time.sleep(2);
    
	gennum = rr.Gennum();
    	one_wire = COne_wire(gennum, 0x40000);
	print "Termometer sensor serial number: " + hex(one_wire.serial)

if __name__ == '__main__' :
	main();
