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

#import ADC module
from gpiozero import MCP3004

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

from time import sleep

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

#Initialize the ADC module and connect the sensor to CH1
adc = MCP3004(channel=1)

while True:

    V = adc.value*3.3  #Collection range: 0-1, converted into voltage value 0-3.3V

    #Light intensity: strong
    if 0 <= V <= 1.1:
        with canvas(device) as draw:
            draw.text((0, 0),  '01Studio', fill="white")
            draw.text((0, 15), 'Light test:', fill="white")
            draw.text((0, 40), str("%.2f"%V) + 'V'+' Bright', fill="white")
        print("%.2f"%V + 'V'+' Bright')

    #Light intensity: medium
    if 1.1 < V <= 2.2:
        with canvas(device) as draw:
            draw.text((0, 0),  '01Studio', fill="white")
            draw.text((0, 15), 'Light test:', fill="white")
            draw.text((0, 40), str("%.2f"%V) + 'V'+' Normal', fill="white")
        print("%.2f"%V + 'V'+' Normal')

    #Light intensity: weak
    if 2.2 < V <=3.3:
        with canvas(device) as draw:
            draw.text((0, 0),  '01Studio', fill="white")
            draw.text((0, 15), 'Light test:', fill="white")
            draw.text((0, 40), str("%.2f"%V) + 'V'+' Weak', fill="white")
        print("%.2f"%V + 'V'+' Weak')

    sleep(1) #Collection interval 1 second

