'''
example name：Atmospheric pressure sensor
version：v1.0
date：2020.3
Author：01Studio
Description: Measure the Atmospheric pressure and display it in oled.
Forum: forum.01studio.org
'''

#bmp280 lib
import bmp280

#import luma,oled lib
from luma.core.render import canvas
from luma.oled.device import ssd1306

import time

#initialize oled，I2C1; the oled address is 0x3c
device = ssd1306(port=1, address=0x3c)

#creat BMP280 object，I2C1
b = bmp280.BMP280(port=1)

while True:

    #get temp value
    Temp  = b.getTemp()

    #get Atmospheric pressure value
    Pressure = b.getPressure()

    #Get altitude
    Altitude = b.getAltitude()

    with canvas(device) as draw:
        draw.text((0, 0),  '01Studio', fill="white")
        draw.text((0, 15), 'BMP280 test:', fill="white")
        draw.text((0, 35), str("%.2f"%Temp)+' C' , fill="white")
        draw.text((0, 45), str("%.2f"%Pressure)+' Pa' , fill="white")
        draw.text((0, 55), Altitude +' M', fill="white")

    #print values
    print("Temperature in Celsius : %.2f C" %Temp)
    print("Pressure : %.2f hPa " %Pressure)
    print('Altitude : ' + Altitude+' M')

    #sleep of 1s
    time.sleep(1)
