<commit_before>#!/usr/bin/python3
#   MCP3424 driver written for webopi
#	written by <NAME>
#	example by ABElectronics, ABE_ADCPi.py used as a template.
#	tested on the ADCPi (qty2)
"""
Device notes
* When you read from the device over I2C, the device returns the data bytes 
  followed by the configuration byte.
* 18 bit resolution - 3 data bytes, followed by the config byte.
* 12 to 16 bit resolution, 2 data bytes, followed by the config byte.
* unused bits to the left of the MSB mirror the MSB(sign bit) and can be ignored.
* twos compliment is used for negative values. (not implemented here)
* 18 bit format byte_1(6 bits to ignore, 1 sign bit, 1 data bit) byte_2(8 data bits) 
  byte_3(8 data bits) byte_4(config byte)
* 16 bit format byte_1(1 sign bit, 7 data bits) byte_2(8 data bits) byte_3(config byte)
* 14 bit format byte_1(2 bits to ignore, 1 sign bit, 5 data bits), byte_2(8 data bits), 
  byte_3(config byte)
* 12 bit format byte_1(4 bits to ignore, 1 sign bit, 3 data bits), byte_2(8 data bits), 
  byte_3(config byte)
 
* Since this chip reads +/- voltage, the MSB is actually a sign bit.  This breaks the 
  -analogMax calculation in /devices/analog/__init__.py.  To fix it, send (resolution-1)
  rather than resolution to __init__.py { so that max = 2**(resolution-1)-1 }
  
  /etc/webiopi/config notes:
  example config to be placed in [DEVICES]
  mcp1 = MCP3424 slave:0x68 resolution:12 name:ADC1 gain:1   
  mcp2 = MCP3424 slave:0x69 resolution:16 name:ADC2 gain:1  

  In webiopi/devices/analog/__init__.py
  Add the following line
  DRIVERS["mcp3424"] = ["MCP3424"]
"""


from webiopi.utils.types import toint
from webiopi.devices.i2c import I2C
from webiopi.devices.analog import ADC


class MCP3424(ADC, I2C):
    # internal variables
	__address = 0x68  # default address for adc 1 on ADCPi and delta-sigma pi
	__channel = 0  # current channel 
	__config = 0x10  # PGAx1, 12 bit, one-shot conversion, channel 1 (0001 0000)

    # create byte array and fill with initial values to define size
	__adcreading = bytearray()
	__adcreading.append(0x00)
	__adcreading.append(0x00)
	__adcreading.append(0x00)
	__adcreading.append(0x00)

    # local methods
	def __str__(self):
		return "MCP3424(__address=0x%02X)" % self.__address	

	def __updatebyte(self, byte, bit, value):
        # internal method for setting the value of a single bit within a byte
		# send the byte to modify, which bit, and the value to set it to.
		if value == 0:
			return byte & ~(1 << bit)<commit_msg>Modified ___str___ function, coding style update.<commit_after>#!/usr/bin/python3
#   MCP3424 driver written for webopi
#	written by <NAME>
#	example by ABElectronics, ABE_ADCPi.py used as a template.
#	tested on the ADCPi (qty2)
"""
Device notes
* When you read from the device over I2C, the device returns the data bytes 
  followed by the configuration byte.
* 18 bit resolution - 3 data bytes, followed by the config byte.
* 12 to 16 bit resolution, 2 data bytes, followed by the config byte.
* unused bits to the left of the MSB mirror the MSB(sign bit) and can be ignored.
* twos compliment is used for negative values. (not implemented here)
* 18 bit format byte_1(6 bits to ignore, 1 sign bit, 1 data bit) byte_2(8 data bits) 
  byte_3(8 data bits) byte_4(config byte)
* 16 bit format byte_1(1 sign bit, 7 data bits) byte_2(8 data bits) byte_3(config byte)
* 14 bit format byte_1(2 bits to ignore, 1 sign bit, 5 data bits), byte_2(8 data bits), 
  byte_3(config byte)
* 12 bit format byte_1(4 bits to ignore, 1 sign bit, 3 data bits), byte_2(8 data bits), 
  byte_3(config byte)
 
* Since this chip reads +/- voltage, the MSB is actually a sign bit.  This breaks the 
  -analogMax calculation in /devices/analog/__init__.py.  To fix it, send (resolution-1)
  rather than resolution to __init__.py { so that max = 2**(resolution-1)-1 }
  
  /etc/webiopi/config notes:
  example config to be placed in [DEVICES]
  mcp1 = MCP3424 slave:0x68 resolution:12 name:ADC1 gain:1   
  mcp2 = MCP3424 slave:0x69 resolution:16 name:ADC2 gain:1  

  In webiopi/devices/analog/__init__.py
  Add the following line
  DRIVERS["mcp3424"] = ["MCP3424"]
"""


from webiopi.utils.types import toint
from webiopi.devices.i2c import I2C
from webiopi.devices.analog import ADC


class MCP3424(ADC, I2C):
    # internal variables
	__address = 0x68  # default address for adc 1 on ADCPi and delta-sigma pi
	__channel = 0  # current channel 
	__config = 0x10  # PGAx1, 12 bit, one-shot conversion, channel 1 (0001 0000)

    # create byte array and fill with initial values to define size
	__adcreading = bytearray()
	__adcreading.append(0x00)
	__adcreading.append(0x00)
	__adcreading.append(0x00)
	__adcreading.append(0x00)

    # local methods
	def __str__(self):
		return "MCP3424(slave=0x%02X)" % self.__address	

	def __updatebyte(self, byte, bit, value):
        # internal method for setting the value of a single bit within a byte
		# send the byte to modify, which bit, and the value to set it to.
		if value == 0:
			return byte & ~(1 << bit)
{'fraction_non_alphanumeric': 0.08462455303933254, 'fraction_numerical': 0.05403257846642829, 'mean_word_length': 3.864734299516908, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1367526', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1687, 'n_words': 670}