max5442.py 1.15 KB
Newer Older
1 2
#!   /usr/bin/env   python
#    coding: utf8
3

4 5 6 7 8 9 10
# Copyright CERN, 2011
# Author: Matthieu Cattin (CERN)
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Last modifications: 03/05/2012

# Import standard modules
11 12
import sys
import time
13 14

# Import specific modules
15
from spi import *
16

17 18
# Class to access the MAX5442 (DAC) chip.
# It uses the SPI class.
19

20 21 22 23 24 25 26
class MAX5442OperationError(Exception):
    def __init__(self, addr, msg):
        self.msg = msg
        self.addr = addr # addr corresponds to the SPI slave number
    def __str__(self):
        return ("MAX5442 [SPI slave number:%d]: %s" %(self.addr, self.msg))

27
class CMAX5442:
28 29 30 31 32

    def __init__(self, spi, slave):
        self.spi = spi
        self.slave = slave

33 34
    # value = DAC digital value (2 bytes)
    def set_value(self, value):
35 36 37 38 39 40 41 42
        try:
            tx = [(value & 0xFF), ((value & 0xFF00)>>8)]
            #print '[max5442] Value to set: %.4X' % value
            #for i in range(len(tx)):
            #    print '[max5442] tx[%d]: %.2X' %(i, tx[i])
            self.spi.transaction(self.slave, tx)
        except SPIDeviceOperationError as e:
            raise MAX5442OperationError(self.slave, e)
43