adding intergration with mcp3008 for analog inputs

This commit is contained in:
langolierz
2018-09-24 19:12:58 +00:00
parent d53d23661b
commit f1e92a01db
9 changed files with 130 additions and 29 deletions

View File

@@ -0,0 +1,68 @@
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
class AnalogInput(object):
def __init__(self, root, message_handler, display, actions, data):
self.root = root
self.message_handler = message_handler
self.display = display
self.actions = actions
self.data = data
self.analog_mappings = data.analog_mappings
self.midi_device = None
self.analog_delay = 50
SPI_PORT = 1
SPI_DEVICE = 2
self.analog_input = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
self.check_if_listening_enabled()
def check_if_listening_enabled(self):
if self.data.settings['other']['ANALOG_INPUT']['value'] == 'enabled':
self.poll_analog_inputs()
else:
self.root.after(1000, self.check_if_listening_enabled)
def poll_analog_inputs(self):
if self.data.settings['other']['ANALOG_INPUT']['value'] == 'enabled':
for i in range(0,8):
if str(i) in self.analog_mappings:
print('{} is in the mapping'.format(i))
reading = self.analog_input.read_adc(i)
self.run_action_for_mapped_channel(i, reading)
self.root.after(self.analog_delay, self.poll_analog_inputs)
else:
self.check_if_listening_enabled()
def run_action_for_mapped_channel(self, channel, channel_value):
this_mapping = self.analog_mappings[str(channel)]
if self.data.control_mode in this_mapping:
mode = self.data.control_mode
elif 'DEFAULT' in this_mapping:
mode = 'DEFAULT'
if self.data.function_on and len(this_mapping[mode]) > 1:
method_name = this_mapping[mode][1]
self.data.function_on = False
else:
method_name = this_mapping[mode][0]
if channel_value is not None:
norm_channel_value = channel_value/1023
else:
norm_channel_value = None
print('the action being called is {}'.format(method_name))
self.call_method_name(method_name, norm_channel_value)
## not sure whether we want to update the screen in general; here - probably not most of the time ...
#if 'cc' not in message_name:
# self.display.refresh_display()
def call_method_name(self, method_name, argument=None):
if argument is not None:
getattr(self.actions, method_name)(argument)
else:
getattr(self.actions, method_name)()

View File

@@ -91,9 +91,13 @@ class MidiInput(object):
method_name = this_mapping[mode][0]
print('the action being called is {}'.format(method_name))
self.call_method_name(method_name, mapped_message_value)
## only update screen if not cc - seeing if cc can respond faster if not refreshing screen on every action
if 'cc' not in message_name:
if mapped_message_value is not None:
norm_message_value = mapped_message_value/127
else:
norm_message_value = None
self.call_method_name(method_name, norm_message_value)
## only update screen if not continuous - seeing if cc can respond faster if not refreshing screen on every action
if 'continuous' not in message_name:
self.display.refresh_display()
def call_method_name(self, method_name, argument=None):