# 温度检测 io32
# 加温控制 io27
# pd功能使用硬件开关控制是否使用
# 蜂鸣器 io25
# 设置按钮 io4 ，io5，io16，io17
# 使用出料按钮 io18，io19
# 电源电压检测 io33
# 电机 io2，io26
# 传感器i2c引脚：SDA21，SCL22
# 当前开源pcb工程 ：

import time
import machine
import ssd1306
from framebuf import FrameBuffer as FB
import _thread
import AHT20

# 3D打印笔使用的字体模型
class _3D_text():
    
    def __init__(self):
        # 闹钟使用的oled模型
        self.tests = {
"1":[[0x00,0x00,0x00,0x08,0x38,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x3E,0x00,0x00],8,16],
"2":[[0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x02,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00],8,16],
"3":[[0x00,0x00,0x00,0x3C,0x42,0x42,0x02,0x04,0x18,0x04,0x02,0x42,0x42,0x3C,0x00,0x00],8,16],
"4":[[0x00,0x00,0x00,0x04,0x0C,0x0C,0x14,0x24,0x24,0x44,0x7F,0x04,0x04,0x1F,0x00,0x00],8,16],
"5":[[0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x78,0x44,0x02,0x02,0x42,0x44,0x38,0x00,0x00],8,16],
"6":[[0x00,0x00,0x00,0x18,0x24,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x22,0x1C,0x00,0x00],8,16],
"7":[[0x00,0x00,0x00,0x7E,0x42,0x04,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x00,0x00],8,16],
"8":[[0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00],8,16],
"9":[[0x00,0x00,0x00,0x38,0x44,0x42,0x42,0x42,0x46,0x3A,0x02,0x02,0x24,0x18,0x00,0x00],8,16],
"0":[[0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00],8,16],
"D":[[0x00,0x00,0x00,0xF8,0x44,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x44,0xF8,0x00,0x00],8,16],
".":[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00],8,16],
"<":[[0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00],8,16],
">":[[0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00],8,16],
"高":[[0x04,0x00,0x02,0x00,0xFF,0xF8,0x00,0x00,0x1F,0xC0,0x10,0x40,0x1F,0xC0,0x00,0x00,0x7F,0xF0,0x40,0x10,0x4F,0x90,0x48,0x90,0x4F,0x90,0x40,0x30],16,14],
"低":[[0x10,0x00,0x10,0x70,0x17,0xC0,0x24,0x40,0x24,0x40,0x64,0x40,0xA7,0xF8,0x24,0x40,0x24,0x40,0x24,0x40,0x25,0x20,0x26,0x28,0x24,0x98,0x20,0x48],16,14],
"℃":[[0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00],16,16],
"%":[[0x00,0x00,0x00,0x44,0xA4,0xA8,0xA8,0xB0,0x54,0x1A,0x2A,0x2A,0x4A,0x44,0x00,0x00],8,16],
"打":[[0x10,0x00,0x10,0x00,0x13,0xFE,0x10,0x20,0xFC,0x20,0x10,0x20,0x10,0x20,0x14,0x20,0x18,0x20,0x30,0x20,0xD0,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x50,0xA0,0x20,0x40],16,16],
"印":[[0x00,0x00,0x06,0x00,0x78,0xFC,0x40,0x84,0x40,0x84,0x40,0x84,0x7E,0x84,0x40,0x84,0x40,0x84,0x40,0x84,0x40,0x84,0x4E,0xA8,0x70,0x90,0x00,0x80,0x00,0x80,0x00,0x80],16,16],
"笔":[[0x10,0x40,0x10,0x40,0x3F,0x7E,0x48,0x90,0x85,0x08,0x00,0xF8,0x3F,0x00,0x01,0x00,0x01,0xF8,0x3F,0x00,0x01,0x00,0x01,0xFC,0x7F,0x02,0x01,0x02,0x01,0x02,0x00,0xFE],16,16],
"启":[[0x01,0x00,0x00,0x80,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x00,0x10,0x00,0x10,0x00,0x17,0xFC,0x24,0x04,0x24,0x04,0x44,0x04,0x87,0xFC,0x04,0x04],16,16],
"动":[[0x00,0x40,0x00,0x40,0x7C,0x40,0x00,0x40,0x01,0xFC,0x00,0x44,0xFE,0x44,0x20,0x44,0x20,0x44,0x20,0x84,0x48,0x84,0x44,0x84,0xFD,0x04,0x45,0x04,0x02,0x28,0x04,0x10],16,16],
"中":[[0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00],16,16],
"加":[[0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x7C,0xFE,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x22,0x44,0x22,0x7C,0x4A,0x44,0x84,0x00],16,16],
"热":[[0x10,0x40,0x10,0x40,0x10,0x40,0xFD,0xF8,0x10,0x48,0x10,0x48,0x1C,0xC8,0x30,0x48,0xD0,0xAA,0x10,0xAA,0x51,0x06,0x22,0x02,0x00,0x00,0x48,0x88,0x44,0x44,0x84,0x44],16,16],
"温":[[0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00],16,16],
"湿":[[0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00],16,16],
"度":[[0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E],16,16],
"电":[[0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x0A,0x01,0x02,0x01,0x02,0x00,0xFE],16,16],
"压":[[0x00,0x00,0x3F,0xFE,0x20,0x00,0x20,0x80,0x20,0x80,0x20,0x80,0x20,0x80,0x2F,0xFC,0x20,0x80,0x20,0x80,0x20,0x90,0x20,0x88,0x20,0x88,0x40,0x80,0x5F,0xFE,0x80,0x00],16,16],
"伏":[[0x08,0x40,0x08,0x50,0x08,0x48,0x10,0x48,0x10,0x40,0x37,0xFE,0x30,0x40,0x50,0x40,0x90,0xA0,0x10,0xA0,0x10,0xA0,0x11,0x10,0x11,0x10,0x12,0x08,0x14,0x04,0x18,0x02],16,16],
"：":[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00],16,16],
"指":[[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x00,0x25,0xFF,0x48,0x00,0x90,0x00,0x48,0x00,0x25,0xFF,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],16,16],
"速":[[0x00,0x40,0x20,0x40,0x17,0xFC,0x10,0x40,0x03,0xF8,0x02,0x48,0xF2,0x48,0x13,0xF8,0x10,0xE0,0x11,0x50,0x12,0x48,0x14,0x44,0x10,0x40,0x28,0x00,0x47,0xFE,0x00,0x00],16,16],
"烫":[[0x23,0xF0,0x10,0x20,0x80,0xC0,0x43,0xFC,0x11,0x24,0x21,0x24,0xE2,0x44,0x24,0x84,0x29,0x28,0x20,0x10,0x01,0x00,0x11,0x10,0x22,0xA0,0x04,0x40,0x18,0x30,0xE0,0x0E],16,16],
"请":[[0x00,0x40,0x40,0x40,0x27,0xFC,0x20,0x40,0x03,0xF8,0x00,0x40,0xE7,0xFE,0x20,0x00,0x23,0xF8,0x22,0x08,0x23,0xF8,0x22,0x08,0x2B,0xF8,0x32,0x08,0x22,0x28,0x02,0x10],16,16],
"拿":[[0x01,0x00,0x06,0xC0,0x18,0x30,0xEF,0xEE,0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x00,0x20,0x1F,0xC0,0x01,0x00,0x3F,0xF8,0x01,0x00,0x7F,0xFC,0x01,0x00,0x03,0x00],16,16],
"好":[[0x10,0x00,0x10,0xFC,0x10,0x04,0x10,0x08,0xFC,0x10,0x24,0x20,0x24,0x20,0x25,0xFE,0x24,0x20,0x48,0x20,0x28,0x20,0x10,0x20,0x28,0x20,0x44,0x20,0x84,0xA0,0x00,0x40],16,16],
"加":[[0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x7C,0xFE,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x12,0x44,0x22,0x44,0x22,0x7C,0x4A,0x44,0x84,0x00],16,16],
"error":[[0x00,0x00,0x0E,0x00,0x11,0x00,0x21,0x80,0x31,0x80,0x31,0x80,0x03,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x00,0x00,0x06,0x00,0x06,0x00,0x00,0x00,0x00,0x00],16,16],

}

    # 将数据转换为oled显示数据
        for item in self.tests :
            self.tests[item][0] = FB(bytearray([self.tests[item][0][x] for x in range(0,len(self.tests[item][0]),1)]), self.tests[item][1], self.tests[item][2], 3)   
            
    def get_test(self,d):
        try:
            return self.tests[d]
        except:
            return self.tests["error"]
        

class PID_Inc:
    """增量式PID"""
    def __init__(self, KP=20, KI=8, KD=10):
        self.Kp = KP
        self.Ki = KI
        self.Kd = KD

        self.PreError = 0   
        self.PreDerror = 0
        self.error = 0
        self.d_drror = 0
        self.dd_error = 0
        self.dcalc = 0
        
    def setKp(self,KP):
        self.Kp=KP
        
    def setKi(self,KI):
        self.Ki=KI
        
    def setKd(self,KD):
        self.Kd=KD
    
    def update(self, Target, Current):
        self.error = Target - Current
        self.d_error = self.error - self.PreError
        self.dd_error = self.d_error - self.PreDerror
        self.PreError = self.error
        self.PreDerror = self.d_error
        self.dcalc = (
            self.Kp * self.d_error + self.Ki * self.error + self.Kd * self.dd_error
        )
        return self.dcalc
        




# 3D打印笔实现程序
class Write3D():

    def __init__(self):
        # 电机控制引脚
        self.Mu = machine.PWM(machine.Pin(2))
        self.Mu.freq(2000)
        self.Mu.duty(0)
        self.Md = machine.PWM(machine.Pin(26))
        self.Md.freq(2000)
        self.Md.duty(0)
        
        # 加热控制引脚
        self.warmingadd = machine.PWM(machine.Pin(27))
        self.warmingadd.freq(2000)
        self.warmingadd.duty(0)# pwm用来控制加热
        
        # oled屏幕初始化
        i2c = machine.I2C(scl = machine.Pin(22), sda = machine.Pin(21), freq = 400000)
        print(i2c.scan())
        # 程序模式
        self.main_state = 0
        
        # 初始化蜂鸣器
        self.pwm25 = machine.PWM(machine.Pin(25))
        self.pwm25.duty(0)
        self.fengmingqicishu = 0
        
        # 初始化触摸引脚
        self.tc12 = machine.TouchPad(machine.Pin(12))
        
        # 初始化字库
        self._3D_text = _3D_text()
        self.oled = None
        try:
            self.oled = ssd1306.SSD1306_I2C(128,32,i2c)
            
            wenzitest = "3D打印笔启动中"
            weizhijianjv = 0# x起始点
            wenziy = 10 # y起始点
            for i in range(0,len(wenzitest),1):
                bitdata = self._3D_text.get_test(str(wenzitest[i]))
                self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
                weizhijianjv = weizhijianjv + bitdata[1] + 2
            
            self.oled.show()
            print("oled屏幕初始化成功")
        except: 
            print("### oled屏幕初始化失败")
        
        # 初始化电源电压检测
        self.battery = machine.ADC(machine.Pin(33))
        self.battery.atten(machine.ADC.ATTN_11DB)
        # 初始化笔头温度检测
        self.Ttemp = machine.ADC(machine.Pin(32))
        self.Ttemp.atten(machine.ADC.ATTN_11DB)
        
        # 控制按键初始化
        self.Tu = machine.Pin(4, machine.Pin.IN) # 温度加按钮
        self.Td = machine.Pin(5, machine.Pin.IN) # 温度减按钮
        self.Su = machine.Pin(16, machine.Pin.IN) # 速度加按钮
        self.Sd = machine.Pin(17, machine.Pin.IN) # 速度减按钮
        self.Kd = machine.Pin(18, machine.Pin.IN) # 出料按钮
        self.Ku = machine.Pin(19, machine.Pin.IN) # 退料按钮
        
        self.Kd_k = 0
        self.Ku_k = 0
        self.Td_k = 0
        self.Tu_k = 0
        self.Sd_k = 0
        self.Su_k = 0
        
        self.Tu_ks = 0
        self.Td_ks = 0
        
        self.KD_s = 0
        self.state_Kd = 0
        self.state_Ks = 50
        
        # 创建文件,有俩种温度设置，一种是5伏供电，一种6伏以上供电。
        self.data_set = "set5"
        self.datelist = self.get_data_list()# {"set5":{"temp":66,"speed":6},"set12":{"temp",:150,"speed":3}}
        #print(self.datelist)
        # 程序变量初始化
        self.speed = self.datelist[self.data_set]["speed"] # 设置的速度值，值为0-10
        self.temp = self.datelist[self.data_set]["temp"] # 读取的温度值，初始化
        self.issave = 0
        
        self.qidong_xweihzi = 0
        self.pid = PID_Inc()# 进行控制温度的设置
        self.pwm = 0# 程序pwm加热控制只
        self.pwmmax = 400# 设置最大的pwm值
        self.is_start = 4000
        self.get_temp_data = []
        for i in range(0,50,1):
            self.get_temp_data.append(0)
        self.get_temp_data_i = 0
        
        
        self.Ahttemp = 0
        self.Ahthum = 0
        self.jiaodustate = 300
        self.Ahtb = None
        self.temp_now = 0
        try:# 初始化温湿度传感器
            self.Ahtb = AHT20.AHT20(i2c) 
            print("板载Sht20初始化成功")
        except:
            print("板载Sht20初始化错误")
            self.Ahtb = None
        # 使用线程进行执行其他功能
        _thread.start_new_thread(self.testThread, ())
        for i in range(1000):
            self.get_temp()
            time.sleep_ms(1)
    
        
        
    # 多线程获取传感器值
    def testThread(self):
        while True:
            
            try:
                # 进行获取传感器数据
                if self.Ahtb:
                    self.Ahttemp = self.Ahtb.getTemperature()
                    self.Ahttemp = int(self.Ahttemp*10)/10
                if self.Ahtb:
                    self.Ahthum = self.Ahtb.getHumidity()
                    self.Ahthum = int(self.Ahthum*10)/10
                
                
            except:
                print("error__")
            
            time.sleep_ms(500)
            
            
    # 启动后获取打印笔资料
    def get_data_list(self,state = []):
        if state == []:
            try:
                file = open("data_list.py",'r')
                
                data_list = file.read()
                data_list = eval(data_list)
                #print(data_list)
                return data_list
                
            except:
                data_list = {"set5":{"temp":66,"speed":6},"set12":{"temp":150,"speed":3}}
                file = open("data_list.py",'w')
                file.write(str(data_list))
                file.close()
                return data_list
        else:
            file = open("data_list.py",'w')
            file.write(str(state))
            file.close()
    
    # 进入获取触摸按键的状态
    def get_tkey(self):
        try:
            return self.tc12.read()
        except:
            return 512
        
        return 0   
        
    

    # 开始程序
    def start(self):
        
        if self.main_state == 0:
            self.qidong()
        
        elif self.main_state == 1:
            self.jiare()
        
    
    # 程序启动中的确认状态，进行显示供电电压和温湿度等
    def qidong(self):
        
        self.oled.fill(0)
        wenzitest = "电压：" + str(self.get_battery()) + "伏"
        weizhijianjv = 0# x起始点
        wenziy = 0 # y起始点
        for i in range(0,len(wenzitest),1):
            bitdata = self._3D_text.get_test(str(wenzitest[i]))
            self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
            weizhijianjv = weizhijianjv + bitdata[1] + 2
        
        self.qidong_xweihzi -= 1
        if self.qidong_xweihzi < 0:
            self.qidong_xweihzi = 10
            
        self.oled.framebuf.blit(self._3D_text.get_test("<")[0],10,16)
        self.oled.framebuf.blit(self._3D_text.get_test(">")[0],60,16)
        self.oled.framebuf.blit(self._3D_text.get_test("指")[0],17 + self.qidong_xweihzi,16)
        
        if self.data_set == "set5":#低模式
            self.oled.framebuf.blit(self._3D_text.get_test("低")[0],80,18)
        elif self.data_set == "set12":# 高模式
            self.oled.framebuf.blit(self._3D_text.get_test("高")[0],80,18)
            
        tach = self.get_tkey()
        #print(tach)
        self.oled.line(128, 22, int((tach/14)) + 80, 22, 1)
        
        self.oled.show()
        
        if self.Kd.value() == 0 and self.Kd_k == 0:
            self.Kd_k = 1
            
        elif self.Kd.value() == 1 and self.Kd_k == 1:
            self.Kd_k = 0
            print("按下按钮，开始程序")
            if tach < 500:
                self.main_state = 1
            else:
                self.oled.fill(0)
                print("请正确抓住设备。。")
                self.state_Kd = 0
                wenzitest = "请拿好打印笔"
                weizhijianjv = 0# x起始点
                wenziy = 16 # y起始点
                for i in range(0,len(wenzitest),1):
                    bitdata = self._3D_text.get_test(str(wenzitest[i]))
                    self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
                    weizhijianjv = weizhijianjv + bitdata[1] + 2
                self.oled.show()    
                time.sleep(1)
        
        # 通过电压进行设置总的最大的pwm值
        dianliang = self.get_battery()
        self.pwmmax = ((20 - dianliang)/1.8)*100
        
        
        #print(dianliang)
        # 通过电压来设置使用5伏还是12伏供电模式
        if dianliang < 6:
            self.data_set = "set5"
        elif dianliang > 6:
            self.data_set = "set12"
        self.speed = self.datelist[self.data_set]["speed"] # 设置的速度值，值为0-10
        self.temp = self.datelist[self.data_set]["temp"] # 读取的温度值，初始化
            
    # 进行加热使用的方法
    def jiare(self):
        
        # oled屏幕显示
        self.oled.fill(0)
        # 控制是否停止，长时间未操作，就停止加热功能
        
        # 如果温度小于预定值，就加热
        self.set_temp(self.temp)
            
        # 按下出料按钮
        
        self.state_Ks -= 1
        if self.state_Ks < 0:
            self.state_Ks = -1
        #print(self.state_Ks)
        if self.Kd.value() == 1 and self.KD_s == 1:
            self.KD_s = 0
            self.state_Kd = 1
            
        if self.Kd.value() == 0 and self.KD_s == 0:
            self.KD_s = 1
            self.state_Kd = 0
            if self.state_Ks > 0:
                print("双击按键")
                self.state_Ks = -1
                self.state_Kd = 1
            else:
                self.state_Kd = 0
            
            if self.state_Ks == -1:
                self.state_Ks = 10
        
        if self.state_Kd == 1:
            if self.temp_now + 10 > self.temp:
                self.Mu.duty(int(self.speed * 50) + 523)
                self.Md.duty(0)
            else:
                self.oled.fill(0)
                self.state_Kd = 0
                print("正在加热中。。。。")
                wenzitest = "加热中..."
                weizhijianjv = 0# x起始点
                wenziy = 0 # y起始点
                for i in range(0,len(wenzitest),1):
                    bitdata = self._3D_text.get_test(str(wenzitest[i]))
                    self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
                    weizhijianjv = weizhijianjv + bitdata[1] + 2
                self.oled.show()    
                time.sleep_ms(200)
        else:
            self.Mu.duty(0)
            self.Md.duty(0)
            
            
        if self.Ku.value() == 1:
            self.state_Kd = 0
            if self.temp_now + 10 > self.temp:
                self.Mu.duty(0)
                self.Md.duty(int(self.speed * 50) + 523)
                
            else:
                self.oled.fill(0)
                print("正在加热中。。。。")
                wenzitest = "加热中..."
                weizhijianjv = 0# x起始点
                wenziy = 0 # y起始点
                for i in range(0,len(wenzitest),1):
                    bitdata = self._3D_text.get_test(str(wenzitest[i]))
                    self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
                    weizhijianjv = weizhijianjv + bitdata[1] + 2
                self.oled.show()    
                time.sleep(1)
                
        
            
        # 速度设置按钮
        if self.Su.value() == 1 and self.Su_k == 1:
            self.Su_k = 0
            if self.speed < 9:
                self.speed = self.speed + 1
                self.issave = 0
        if self.Su.value() == 0 and self.Su_k == 0:
            self.Su_k = 1
        
        if self.Sd.value() == 1 and self.Sd_k == 1:
            self.Sd_k = 0
            if self.speed > 1:
                self.speed = self.speed - 1
                self.issave = 0
        if self.Sd.value() == 0 and self.Sd_k == 0:
            self.Sd_k = 1
            
        # 温度设置按钮
        if self.Tu.value() == 1 and self.Tu_k == 1:
            self.Tu_k = 0
            if self.temp < 222:
                self.temp = self.temp + 1
                self.issave = 0
        if self.Tu.value() == 0 and self.Tu_k == 0:
            self.Tu_k = 1
            
        if self.Td.value() == 1 and self.Td_k == 1:
            self.Td_k = 0
            if self.temp > 40:
                self.temp = self.temp - 1
                self.issave = 0
        if self.Td.value() == 0 and self.Td_k == 0:
            self.Td_k = 1
        
        
        if self.Tu.value() == 1:
            self.Tu_ks += 1
        else:
            self.Tu_ks = 0
        if self.Tu_ks > 10:
            if self.temp < 222:
                self.temp = self.temp + 1
                self.issave = 0
            
        if self.Td.value() == 1:
            self.Td_ks += 1
        else:
            self.Td_ks = 0
        if self.Td_ks > 10:
            if self.temp < 222:
                self.temp = self.temp - 1
                self.issave = 0
        
        
        
        self.issave += 1
        #print(self.issave)
        if self.issave == 60:# 设置完成后，大概6秒后进行保存数据
            self.issave = 66
            # 进行保存
            print("保存数据")
            self.datelist[self.data_set]["speed"] = self.speed
            self.datelist[self.data_set]["temp"] = self.temp
            self.get_data_list(self.datelist)
            # 保存后再进行读取
            self.datelist = self.get_data_list()# {"set5":{"temp":66,"speed":6},"set12":{"temp",:150,"speed":3}}
            self.speed = self.datelist[self.data_set]["speed"]
            self.temp = self.datelist[self.data_set]["temp"]
        
        
        wenzitest = "温" + str(int(self.temp_now)) + "℃"
        weizhijianjv = 0# x起始点
        wenziy = 0 # y起始点
        for i in range(0,len(wenzitest),1):
            bitdata = self._3D_text.get_test(str(wenzitest[i]))
            self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
            weizhijianjv = weizhijianjv + bitdata[1] + 2
        self.oled.text("/" + str(self.temp),weizhijianjv ,0)
        
        wenzitest = "速" + str(self.speed)
        weizhijianjv = 0# x起始点
        wenziy = 16 # y起始点
        for i in range(0,len(wenzitest),1):
            bitdata = self._3D_text.get_test(str(wenzitest[i]))
            self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
            weizhijianjv = weizhijianjv + bitdata[1] + 2
        
        if self.fengmingqicishu < 0:
            self.fengmingqicishu = 0
        self.fengmingqicishu -= 1
        #print(self.fengmingqicishu)
        if self.fengmingqicishu < 20:
            self.pwm25.duty(0)
        
        
        #print(self.Ahttemp)
        if self.Ahttemp > 66:
            wenzitest = "烫烫"
            weizhijianjv = 40# x起始点
            wenziy = 16 # y起始点
            for i in range(0,len(wenzitest),1):
                bitdata = self._3D_text.get_test(str(wenzitest[i]))
                self.oled.framebuf.blit(bitdata[0],weizhijianjv,wenziy)
                weizhijianjv = weizhijianjv + bitdata[1] + 2
            self.oled.text("T:" + str(self.Ahttemp),weizhijianjv ,24)
            if self.fengmingqicishu < 0:
                self.fengmingqicishu = 20
            
            if self.fengmingqicishu > 10:
                self.pwm25.duty(500)
                print("烫手")
            
        else:
            self.oled.text("T:" + str(self.Ahttemp),32 ,24)
            self.oled.text("H:" + str(self.Ahthum),82 ,24)
        
        tach = self.get_tkey()
        #print(tach)
        self.oled.line(128, 21, int((tach/14)) + 80, 21, 1)
        
        if tach > 500:
            self.is_start -= 1
        else:
            self.is_start += 100
        if self.is_start > 666:
            self.is_start = 666
        
        print(self.is_start)
        if self.is_start < 1:# 如果长时间没有操作就关闭了
            self.is_start = 0
            self.main_state = 0
            self.set_temp(0)
            self.state_Kd = 0
            self.Mu.duty(0)
            self.Md.duty(0)
        self.oled.show()
    
    # 设置探头温度值
    def set_temp(self,temp=80):
        # 读取温度检测的模拟值
        self.temp_now  = self.get_temp()
        
        if temp == 0:
            self.warmingadd.duty(0)
        else:
            d = self.pid.update(temp, self.temp_now)
            self.pwm = int(self.pwm + d)
            if self.pwm > self.pwmmax:
                self.pwm = self.pwmmax
            if self.pwm < 0:
                self.pwm = 0
            
            if self.temp_now + 2 > self.temp:# 如果温度接近，就减少duty，防止电机卡死电源
                if self.temp_now - temp < 3:
                    self.pwm = int(self.pwm * 0.8 + (self.state_Kd*20))
                else:
                    self.pwm = int(self.pwm * 0.8 )
                    
            #print(self.pwm)
            #print(self.temp_now)
            self.warmingadd.duty(int(self.pwm))
                
    
    # 获取笔头温度值
    def get_temp(self):
        t = self.Ttemp.read()
        # 计算平均电压值
        self.get_temp_data[self.get_temp_data_i] = t
        self.get_temp_data_i += 1
        if self.get_temp_data_i >= len(self.get_temp_data):
            self.get_temp_data_i = 0
        t = 0
        for i in range(0,len(self.get_temp_data),1):
            t += self.get_temp_data[i]
        t = int(t /len(self.get_temp_data))
        t = t/18
        return t
    
    # 获取电压值
    def get_battery(self):
        dianliang = 0
        for i in range(10):
            dianliang += (self.battery.read()/10.21) + 7
        dianliang = dianliang/10
        
        return int(dianliang)/10
        
        


if __name__ == "__main__":
    
    Write = Write3D()
    time.sleep_ms(2000) # 进行延时，防止上电后电路有问题的误操作
    
    while True:
        time.sleep_ms(10)
        Write.start()
        
        
