# coding: utf-8 import os import time from datetime import datetime, date import math import Adafruit_GPIO.SPI as SPI import Adafruit_MAX31855.MAX31855 as MAX31855 import sys from numpy import nan as NA import numpy as np import webbrowser import tkinter as tk import tkinter.scrolledtext as st CLK = 25 CS = 24 DO = 18 # https://docs.python.org/ja/3.5/library/tkinter.html class Application(tk.Frame): def __init__(self): self.init_tk() self.dir_path = os.path.dirname(os.path.abspath(__file__)) self.dir_path = self.dir_path + ('' if self.dir_path[-1:] == '/' else '/') self.sensor = MAX31855.MAX31855(CLK, CS, DO) self.stack_temp = np.array([]) self.pk = 0 def init_tk(self): root = tk.Tk() root.title('Thermocouple Logger(サーモカップルロガー)v1.0') super().__init__(root) self.pack() control = tk.Frame(self, relief="ridge") self.int_logging = tk.IntVar() self.btn_start = tk.Radiobutton(control, text='計測', padx=8, indicatoron=False, variable = self.int_logging, value = 1, command=self.change_state) self.btn_stop = tk.Radiobutton(control, text="停止", padx=8, indicatoron=False, variable = self.int_logging, value = 0, command=self.change_state) self.flag_scroll = tk.BooleanVar() self.chk_scroll = tk.Checkbutton(control, text='最新値へ自動スクロール', variable=self.flag_scroll) self.label_err = tk.Label(control, text='') btn_graph = tk.Button(control, text='グラフ描画ページを開く', command=self.open_graph) self.board = st.ScrolledText(self, width=145, height=30) control.pack(padx=5, pady=5, anchor=tk.W, fill="x") self.btn_start.pack(side="left") self.btn_stop.pack(side="left") self.chk_scroll.pack(side="left") self.label_err.pack(side="left") btn_graph.pack(side="right") self.board.pack(side="top") self.flag_scroll.set(True) self.int_logging.set(0) def open_graph(self): try: webbrowser.open('http://www-ict.dent.agu.ac.jp/graph/viewer.html') #webbrowser.open('http://www.yahoo.co.jp/') except webbrowser.Error: print('webbrowser.Error!') def output_val(self, temp_float): int_temp = int(temp_float) grid = int_temp // 2 str_diff = str(datetime.now() - self.start_datetime) txt_val = " " * grid + '| {0:0.2F}*C'.format(temp_float) + ' ' + str_diff[0:7] #txt_val = " " * grid + '| ' + str(self.pk) + ' {0:0.2F}*C'.format(temp_float) + ' ' + str_diff[0:7] self.board.insert(tk.END, txt_val + '\n') if self.flag_scroll.get() == True: self.board.see(tk.END) def write_csv(self, temp_float): file_path = self.dir_path + str(self.start_datetime) + '.csv' now = datetime.now() str_diff = str(now - self.start_datetime) txt_row = str(now) + ',' + str_diff + ',' + str(round(temp_float, 2)) + "\n" with open(file_path, 'a') as f: f.write(txt_row) f.flush() os.fdatasync(f.fileno()) def normalize_val(self, temp_str): temp_float = float(temp_str) self.stack_temp = np.append(self.stack_temp, temp_float) if len(self.stack_temp) > 9: self.stack_temp = np.delete(self.stack_temp, self.stack_temp.argmax(0), 0) self.stack_temp = np.delete(self.stack_temp, self.stack_temp.argmin(0), 0) temp = np.mean(self.stack_temp) self.stack_temp = np.array([]) self.pk += 1 else: temp = NA return temp def frame(self): if self.status_run == False: return temp = self.sensor.readTempC() #temp = NA if not math.isnan(temp): print(temp) #print(temp, self.pk-1) temp_float = self.normalize_val(temp) if not math.isnan(temp_float): self.output_val(temp_float) self.write_csv(temp_float) self.nan_cnt = 0 #警告解除 self.label_err.configure(text='') if self.status_run == True: self.after(100, self.frame) else: self.nan_cnt += 1 #警告 self.label_err.configure(text='retry: ' + str(self.nan_cnt)) if self.status_run == True: self.after(1, self.frame) def change_state(self): if self.int_logging.get() == 1: self.board.delete('1.0', tk.END) self.start_datetime = datetime.now() self.nan_cnt = 0 self.status_run = True self.after(100, self.frame) else: self.status_run = False app = Application() app.mainloop()