Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Connections:
- LED Bar to JP5
- LED Display to JP7
- Temperature Sensor to JP1
- Servo to Pin 35 (GPIO 19)
- """
- import spidev
- import time
- import sys
- import os
- import RPi.GPIO as IO
- t_soll = 25
- ##======================================================================
- ##======================================================================
- IO.setwarnings(False)
- IO.setmode(IO.BCM)
- HexDigits = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71]
- ADDR_AUTO = 0x40
- ADDR_FIXED = 0x44
- STARTADDR = 0xC0
- BRIGHT_DARKEST = 0
- BRIGHT_TYPICAL = 2
- BRIGHT_HIGHEST = 7
- OUTPUT = IO.OUT
- INPUT = IO.IN
- LOW = IO.LOW
- HIGH = IO.HIGH
- class TM1637:
- __doublePoint = False
- __Clkpin = 0
- __Datapin = 0
- __brightnes = BRIGHT_TYPICAL;
- __currentData = [0,0,0,0];
- def __init__( self, pinClock, pinData, brightnes ):
- self.__Clkpin = pinClock
- self.__Datapin = pinData
- self.__brightnes = brightnes;
- IO.setup(self.__Clkpin,OUTPUT)
- IO.setup(self.__Datapin,OUTPUT)
- # end __init__
- def Clear(self):
- b = self.__brightnes;
- point = self.__doublePoint;
- self.__brightnes = 0;
- self.__doublePoint = False;
- data = [0x7F,0x7F,0x7F,0x7F];
- self.Show(data);
- self.__brightnes = b; # restore saved brightnes
- self.__doublePoint = point;
- # end Clear
- def Show( self, data ):
- for i in range(0,4):
- self.__currentData[i] = data[i];
- self.start();
- self.writeByte(ADDR_AUTO);
- self.stop();
- self.start();
- self.writeByte(STARTADDR);
- for i in range(0,4):
- self.writeByte(self.coding(data[i]));
- self.stop();
- self.start();
- self.writeByte(0x88 + self.__brightnes);
- self.stop();
- # end Show
- def Show1(self, DigitNumber, data): # show one Digit (number 0...3)
- if( DigitNumber < 0 or DigitNumber > 3):
- return; # error
- self.__currentData[DigitNumber] = data;
- self.start();
- self.writeByte(ADDR_FIXED);
- self.stop();
- self.start();
- self.writeByte(STARTADDR | DigitNumber);
- self.writeByte(self.coding(data));
- self.stop();
- self.start();
- self.writeByte(0x88 + self.__brightnes);
- self.stop();
- # end Show1
- def SetBrightnes(self, brightnes): # brightnes 0...7
- if( brightnes > 7 ):
- brightnes = 7;
- elif( brightnes < 0 ):
- brightnes = 0;
- if( self.__brightnes != brightnes):
- self.__brightnes = brightnes;
- self.Show(self.__currentData);
- # end if
- # end SetBrightnes
- def ShowDoublepoint(self, on): # shows or hides the doublepoint
- if( self.__doublePoint != on):
- self.__doublePoint = on;
- self.Show(self.__currentData);
- # end if
- # end ShowDoublepoint
- def writeByte( self, data ):
- for i in range(0,8):
- IO.output( self.__Clkpin, LOW)
- if(data & 0x01):
- IO.output( self.__Datapin, HIGH)
- else:
- IO.output( self.__Datapin, LOW)
- data = data >> 1
- IO.output( self.__Clkpin, HIGH)
- #endfor
- # wait for ACK
- IO.output( self.__Clkpin, LOW)
- IO.output( self.__Datapin, HIGH)
- IO.output( self.__Clkpin, HIGH)
- IO.setup(self.__Datapin, INPUT)
- while(IO.input(self.__Datapin)):
- time.sleep(0.001)
- if( IO.input(self.__Datapin)):
- IO.setup(self.__Datapin, OUTPUT)
- IO.output( self.__Datapin, LOW)
- IO.setup(self.__Datapin, INPUT)
- #endif
- # endwhile
- IO.setup(self.__Datapin, OUTPUT)
- # end writeByte
- def start(self):
- IO.output( self.__Clkpin, HIGH) # send start signal to TM1637
- IO.output( self.__Datapin, HIGH)
- IO.output( self.__Datapin, LOW)
- IO.output( self.__Clkpin, LOW)
- # end start
- def stop(self):
- IO.output( self.__Clkpin, LOW)
- IO.output( self.__Datapin, LOW)
- IO.output( self.__Clkpin, HIGH)
- IO.output( self.__Datapin, HIGH)
- # end stop
- def coding(self, data):
- if( self.__doublePoint ):
- pointData = 0x80
- else:
- pointData = 0;
- if(data == 0x7F):
- data = 0
- else:
- data = HexDigits[data] + pointData;
- return data
- # end coding
- # end class TM1637
- Display = TM1637(23,24,BRIGHT_TYPICAL)
- ## =============================================================
- ## =============================================================
- d_pin_d = 17
- clk_pin_d = 18
- IO.setup([d_pin_d,clk_pin_d], IO.OUT, initial = 0)
- clk = False #Initialisieren der Clock-Variable
- brightness = 0x001f
- def send16bitData(data): #Control und LED-Block Daten
- global clk
- bit = 0x8000
- for b in range(16):
- if (data&bit):
- IO.output(d_pin_d,1)
- else:
- IO.output(d_pin_d,0)
- bit = (bit>>1)
- clk = not clk
- IO.output(clk_pin_d, clk)
- def sendleds(on, half = 0):
- send16bitData(0x0000)
- for n in range(on): #Full on
- send16bitData(brightness)
- for n in range(on, on+half): #between full on and half
- send16bitData(brightness >> 2) #Half brightness
- for n in range(on+half, 12):
- send16bitData(0x000)
- latch()
- def latch():
- toggle = False
- time.sleep(0.0005)
- for i in range(8):
- IO.output(d_pin_d,toggle)
- toggle = not toggle
- time.sleep(0.0005)
- def ledbar(nr_on_leds = 0, bar_mode = 0, max_brightness=0x001f):
- #nr_on_leds = How many LED's switched on
- #bar_mode = -1 = bar decreasing from max to half brightness
- # = 0 = steady on
- # = 1 = bar increasing from half to max brightness
- # send16bitData(0x0000)
- if (bar_mode == 0):
- sendleds(nr_on_leds)
- elif (bar_mode == 1):
- for i in range(nr_on_leds+1):
- sendleds(i, nr_on_leds-i)
- time.sleep(0.1)
- elif (bar_mode == -1):
- for i in range(nr_on_leds+1):
- sendleds(nr_on_leds-i, i)
- time.sleep(0.1)
- ledbar()
- ## =============================================================
- ## =============================================================
- # begin ADC
- spi = spidev.SpiDev()
- spi.open(0,0)
- def readadc(adcnum):
- # read SPI data from MCP3004 chip, 4 possible adc's (0 thru 3)
- if adcnum >3 or adcnum <0:
- return-1
- r = spi.xfer2([1,8+adcnum <<4,0])
- adcout = ((r[1] &3) <<8)+r[2]
- return adcout
- ## =============================================================
- ## =============================================================
- def read_temp():
- value_=readadc(0)
- volts_=(value_*3.3)/1024
- temp_ = (volts_-0.5)*100
- return temp_
- IO.setup(19, IO.OUT)
- try:
- while True:
- t1 = read_temp()
- time.sleep(0.01)
- t2 = read_temp()
- time.sleep(0.01)
- t3 = read_temp()
- temp = (t1 + t2 + t3) / 3
- t_list = []
- t_list += str(temp)
- del t_list[2]
- del t_list[4:]
- t_list = [int(i) for i in t_list]
- Display.Show(t_list)
- bars = int((temp-18)/(27-18)*10)
- ledbar(bars, 0)
- Servo = IO.PWM(19, 50)
- Servo.start(7.5)
- diff_t = temp - t_soll
- if diff_t > 1.5:
- Servo.ChangeDutyCycle(2.5)
- if diff_t == t_soll:
- Servo.ChangeDutyCycle(7.5)
- if diff_t < 1.5:
- Servo.ChangeDutyCycle(12.5)
- print(temp)
- time.sleep(5)
- except:
- ledbar(0,0)
- Display.Clear()
- spi.close()
- IO.cleanup()
- Servo.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement