# Simple demo of continuous ADC conversion mode for channel 0 of the ADS1x15 ADC. # Author: Tony DiCola # License: Public Domain import time # Import the ADS1x15 module. import Adafruit_ADS1x15 import threading import socket MCAST_GRP = '224.1.1.22' MCAST_PORT = 1234 MULTICAST_TTL = 2 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL) QUE_MAXLEN = 16 SAMPLES = 400 TIMEINTERVAL = 0.01 FRAMELEN = 8 + SAMPLES*2 + 2 # time 8 bytes, signal 400*2, checksun 2 bytes queue = [] for i in range(QUE_MAXLEN): queue.append(bytearray(FRAMELEN)) queue_front = 0 queue_back = -1 queue_len = 0 def queue_pop(): global queue_front global queue_len global queue queue_front += 1 if queue_front >= QUE_MAXLEN: queue_front = 0 queue_len -= 1 def queue_push(): global queue_back global queue_len global queue queue_back += 1 if queue_back >= QUE_MAXLEN: queue_back = 0 queue_len += 1 cv = threading.Condition() def sending(): global queue_front global queue_back global queue_len global queue while True: cv.acquire() if queue_len == 0: cv.wait() if queue_len == 0: cv.release() continue frame = queue[queue_front] cv.release() # print("Frame ", frame[0]) sock.sendto(frame, (MCAST_GRP, MCAST_PORT)) cv.acquire() queue_pop() cv.release() sendingThread = threading.Thread(target=sending) sendingThread.start() # Create an ADS1115 ADC (16-bit) instance. adc = Adafruit_ADS1x15.ADS1115() # Or create an ADS1015 ADC (12-bit) instance. #adc = Adafruit_ADS1x15.ADS1015() # Note you can change the I2C address from its default (0x48), and/or the I2C # bus by passing in these optional parameters: #adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1) # Choose a gain of 1 for reading voltages from 0 to 4.09V. # Or pick a different gain to change the range of voltages that are read: # - 2/3 = +/-6.144V # - 1 = +/-4.096V # - 2 = +/-2.048V # - 4 = +/-1.024V # - 8 = +/-0.512V # - 16 = +/-0.256V # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain. GAIN = 1 # Start continuous ADC conversions on channel 0 using the previously set gain # value. Note you can also pass an optional data_rate parameter, see the simpletest.py # example and read_adc function for more infromation. adc.start_adc(0, gain=GAIN) # Once continuous ADC conversions are started you can call get_last_result() to # retrieve the latest result, or stop_adc() to stop conversions. # Note you can also call start_adc_difference() to take continuous differential # readings. See the read_adc_difference() function in differential.py for more # information and parameter description. # Read channel 0 for 5 seconds and print out its values. print('Reading ADS1x15 channel 0 for 5 seconds...') start = time.time() while True: # Read the last ADC conversion value and print it out. cv.acquire() queue_push() frame = queue[queue_back] cv.release() t0 = time.time() t = int(t0*1000.) for i in range(8): frame[i] = (t & 255) t >>= 8 p = 8 checksum = ((frame[0] + frame[1]*256) & 65535) checksum = ((checksum*3 + (frame[2] + frame[3]*256)) & 65535) checksum = ((checksum*3 + (frame[4] + frame[5]*256)) & 65535) checksum = ((checksum*3 + (frame[6] + frame[7]*256)) & 65535) for i in range(0, SAMPLES): t0 = time.time() value = adc.get_last_result() checksum = ((checksum*3 + value) & 65535) frame[p] = (value & 255) frame[p+1] = ((value>>8) & 255) p += 2 # time.sleep(TIMEINTERVAL) dt = TIMEINTERVAL - (time.time() - t0) # print(dt) if dt > 0.: time.sleep(dt) frame[p] = (checksum & 255) frame[p+1] = ((checksum>>8) & 255) cv.acquire() cv.notify() cv.release()