| 
									
										
										
										
											2019-11-07 19:23:20 -05:00
										 |  |  | # Utility for querying the current state of adc pins | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2019  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QueryADC: | 
					
						
							|  |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							|  |  |  |         self.adc = {} | 
					
						
							|  |  |  |         gcode = self.printer.lookup_object('gcode') | 
					
						
							|  |  |  |         gcode.register_command("QUERY_ADC", self.cmd_QUERY_ADC, | 
					
						
							|  |  |  |                                desc=self.cmd_QUERY_ADC_help) | 
					
						
							|  |  |  |     def register_adc(self, name, mcu_adc): | 
					
						
							|  |  |  |         self.adc[name] = mcu_adc | 
					
						
							|  |  |  |     cmd_QUERY_ADC_help = "Report the last value of an analog pin" | 
					
						
							| 
									
										
										
										
											2020-04-24 22:29:25 -04:00
										 |  |  |     def cmd_QUERY_ADC(self, gcmd): | 
					
						
							|  |  |  |         name = gcmd.get('NAME', None) | 
					
						
							| 
									
										
										
										
											2019-11-07 19:23:20 -05:00
										 |  |  |         if name not in self.adc: | 
					
						
							|  |  |  |             objs = ['"%s"' % (n,) for n in sorted(self.adc.keys())] | 
					
						
							|  |  |  |             msg = "Available ADC objects: %s" % (', '.join(objs),) | 
					
						
							| 
									
										
										
										
											2020-04-24 22:29:25 -04:00
										 |  |  |             gcmd.respond_info(msg) | 
					
						
							| 
									
										
										
										
											2019-11-07 19:23:20 -05:00
										 |  |  |             return | 
					
						
							|  |  |  |         value, timestamp = self.adc[name].get_last_value() | 
					
						
							|  |  |  |         msg = 'ADC object "%s" has value %.6f (timestamp %.3f)' % ( | 
					
						
							|  |  |  |             name, value, timestamp) | 
					
						
							| 
									
										
										
										
											2020-04-24 22:29:25 -04:00
										 |  |  |         pullup = gcmd.get_float('PULLUP', None, above=0.) | 
					
						
							| 
									
										
										
										
											2019-11-07 19:23:20 -05:00
										 |  |  |         if pullup is not None: | 
					
						
							|  |  |  |             v = max(.00001, min(.99999, value)) | 
					
						
							|  |  |  |             r = pullup * v / (1.0 - v) | 
					
						
							|  |  |  |             msg += "\n resistance %.3f (with %.0f pullup)" % (r, pullup) | 
					
						
							| 
									
										
										
										
											2020-04-24 22:29:25 -04:00
										 |  |  |         gcmd.respond_info(msg) | 
					
						
							| 
									
										
										
										
											2019-11-07 19:23:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | def load_config(config): | 
					
						
							|  |  |  |     return QueryADC(config) |