-
Temperature ​meter code added
07/23/2017 at 23:21 • 0 commentsOur hackerspace had an old Pro Mini-based board with two DS18B20 sensors that used to measure temperature inside and outside our hackerspace's main room, mostly for shits&giggles. Now, I took the IMTAIDKW, attached those sensors to it and wrote some code:
def run(sleep_time=0.0001, cycle_counter=50000, update_counter=10000, format_counter=10010, trigger_counter=5000): #Good luck understanding this lol global thermometers, current_thermometer, is_error, digit_bytes print("Hello!") prev_i = 3 run_counter = 0 while True: for i, digit_byte in enumerate(digit_bytes): isr = machine.disable_irq() shiftOut(digit_byte) columns[prev_i].on() latch.on() columns[i].off() machine.enable_irq(isr) run_counter += 1 if run_counter >= cycle_counter: print("cycling through thermometers") current_thermometer += 1 if current_thermometer >= len(thermometers): current_thermometer = 0 print("current thermometer: {}".format(current_thermometer)) run_counter = 0 elif run_counter % update_counter == 0: print("getting temperature") try: temperature = ds.read_temp(thermometers[current_thermometer]) except (OneWireError, IndexError): print("sensor {} failed".format(current_thermometer)) is_error = True else: temperatures[current_thermometer] = temperature if is_error: thermometers = ds.scan() if not thermometers: print("no sensors found!") digit_bytes = [mapping[char] for char in " err"] else: is_error = False elif run_counter % format_counter == 0 and not is_error: print("formatting temperatures for display") temperature = temperatures[current_thermometer] if type(temperature) != float: print("wrong temperature {1} for sensor {0}!".format(current_thermometer, temp_str)) else: temp_str = "{:.1f}".format(temperature) print("sensor {} has temperature {}".format(current_thermometer, temp_str)) digit_bytes = generate_digit_bytes(temp_str, thermometers[current_thermometer]) elif run_counter % trigger_counter == 0: print("updating temperatures") try: ds.convert_temp() except OneWireError: is_error = True thermometers = ds.scan() if not thermometers: print("no sensors found!") digit_bytes = [mapping[char] for char in " err"] else: is_error = False sleep(sleep_time) prev_i = i
.. Yeah, good luck reading that. The full code is here: https://github.com/CRImier/IMTAIDKW/blob/master/software/temperature_meter.py
I also attached a light sensor to the analog port, but I don't feel like I can be arsed to write the code that does something useful with it. That's going to be it on IMTAIDKW for now!
I'll add some photos of this setup later - though, frankly, I don't care enough, so it's unlikely I actually will.
-
shiftOut ported to MicroPython Viper
07/09/2017 at 16:20 • 0 comments@micropython.viper def shiftOut(data: int): GPIO_OUT = ptr32(0x60000300) # GPIO base register GPIO_OUT[2] = 0x10 # clear pin 4 for i in range(8): value = data & 1<<i #Is bit set or cleared? reg = 2-(value >>i) #Selecting set or clear register - clear reg is 2, set reg is 1 GPIO_OUT[reg] = 0x8000 #set or clear data bit GPIO_OUT[1] = 0x20 # set bit 5 GPIO_OUT[2] = 0x20 # clear bit 5
instead ofdef shiftOut(byte): latch.off() for i in range(8): value = byte & 1<<i #Is bit set or cleared? data.value(value) clock.on() clock.off()
Is it going to be faster? IDK, I asked about it on MicroPython forums to figure out - and also to make sure there are more Google-able examples of toggling GPIOs from Viper code. New example is here: https://github.com/CRImier/IMTAIDKW/blob/master/software/test_viper.py -
Test code is up
07/09/2017 at 14:27 • 0 commentsI should probably add comments to it.
https://github.com/CRImier/IMTAIDKW/blob/master/software/test.py