# KO OVO PROCITA JE GLUP
from ili934xnew import ILI9341, color565
from machine import Pin, SPI, ADC
from micropython import const
from math import floor
import os
import glcdfont
import tt14
import tt24
import tt32
import utime
# Dimenzije displeja
SCR_WIDTH = const(320)
SCR_HEIGHT = const(240)
SCR_ROT = const(2)
CENTER_Y = int(SCR_WIDTH/2)
CENTER_X = int(SCR_HEIGHT/2)
LABELS_X = 215
VALUES_X = 265
U_Y = 10
T_Y = 25
V_Y = 40
O_X = 20
O_Y = 225
TOP_Y = 15
RIGHT_X = 300
TEMP_HIGH = 40
TEMP_LOW = 20
X_SAMPLE = 6
PIX_PER_DEG = (O_Y - TOP_Y) / (TEMP_HIGH - TEMP_LOW)
TIME_HIGH = (RIGHT_X - O_X) / X_SAMPLE
# Podešenja SPI komunikacije sa displejem
TFT_CLK_PIN = const(18)
TFT_MOSI_PIN = const(19)
TFT_MISO_PIN = const(16)
TFT_CS_PIN = const(17)
TFT_RST_PIN = const(20)
TFT_DC_PIN = const(15)
spi = SPI(
0,
baudrate=62500000,
miso=Pin(TFT_MISO_PIN),
mosi=Pin(TFT_MOSI_PIN),
sck=Pin(TFT_CLK_PIN))
display = ILI9341(
spi,
cs=Pin(TFT_CS_PIN),
dc=Pin(TFT_DC_PIN),
rst=Pin(TFT_RST_PIN),
w=SCR_WIDTH,
h=SCR_HEIGHT,
r=SCR_ROT)
def display_text(X, Y, text):
display.set_pos(X, Y)
display.print(text)
display.fill_rectangle(0, 0, 240, 320, color565(255, 255, 255))
display.rotation = 1
display.init()
display.set_color(color565(0, 0, 0), color565(255, 255, 255))
display_text(LABELS_X, U_Y, "Napon: ")
display_text(LABELS_X, T_Y, 'Temp: ')
display_text(LABELS_X, V_Y, 'Vrijeme: ')
display_text(VALUES_X + 35, U_Y, 'mV')
display_text(VALUES_X + 35, T_Y, 'C')
display_text(VALUES_X + 20, V_Y, 's')
display_text(4, O_Y - 4, "20")
display_text(4, TOP_Y + (O_Y - TOP_Y) // 2 - 4, "30")
display_text(4, TOP_Y - 4, "40")
for x in range(O_X, RIGHT_X):
display.pixel(x, O_Y, color565(0, 0, 0))
for y in range(TOP_Y, O_Y):
display.pixel(O_X, y, color565(0, 0, 0))
def draw_horizontal_line(x1, x2, y, color):
for x in range(x1, x2 + 1):
display.pixel(x, y, color)
def draw_dot(x0, y0, radius, color):
x = radius
y = 0
err = 0
while x >= y:
draw_horizontal_line(x0 - x, x0 + x, y0 + y, color)
draw_horizontal_line(x0 - x, x0 + x, y0 - y, color)
draw_horizontal_line(x0 - y, x0 + y, y0 + x, color)
draw_horizontal_line(x0 - y, x0 + y, y0 - x, color)
if err <= 0:
y += 1
err += 2 * y + 1
if err > 0:
x -= 1
err -= 2 * x + 1
def draw_line(x0, y0, x1, y1, color):
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = -1 if x0 > x1 else 1
sy = -1 if y0 > y1 else 1
err = dx - dy
while True:
display.pixel(x0, y0, color)
if x0 == x1 and y0 == y1:
break
e2 = 2 * err
if e2 > -dy:
err -= dy
x0 += sx
if e2 < dx:
err += dx
y0 += sy
sensor = ADC(28)
coef = 3300 / 65535
t = 0
old_coor = (O_X, O_Y)
while True:
start_time = utime.ticks_ms()
t += 1
milivolt = round(sensor.read_u16() * coef, 1)
temp = round(milivolt / 10, 2)
display_text(VALUES_X, U_Y, "{:.0f}".format(milivolt))
display_text(VALUES_X, T_Y, "{:.1f}".format(temp))
display_text(VALUES_X, V_Y, "{}".format(t))
y_coor = int(O_Y + (20 - temp) * PIX_PER_DEG)
x_coor = int(O_X + t * X_SAMPLE)
draw_line(old_coor[0], old_coor[1], x_coor, y_coor, color565(255, 0, 0))
draw_dot(old_coor[0], old_coor[1], 3, color565(0, 0, 0))
draw_dot(x_coor, y_coor, 3, color565(255, 0, 0))
old_coor = (x_coor, y_coor)
end_time = utime.ticks_ms()
elapsed_time = utime.ticks_diff(end_time, start_time)
print(elapsed_time)
utime.sleep_ms(1000 - elapsed_time)