First look
GY-85.py:
Copy codeThe Code is as follows:
#! /Usr/bin/python3
#-*-Coding: UTF-8 -*-
Import curses
From time import *
From i2clibraries import i2c_itg3205, i2c_adxl345, i2c_hmc5883l
#===================================================== ==============================
# GY-85 sensor monitoring
#===================================================== ==============================
Def displayITG3205 (screen, col, temp, x, y, z ):
"""
How to display ITG3205 readings
"""
Screen. addstr (1, col, "%. 1f ° C" % temp)
Screen. addstr (2, col, "%. 1f °/s" % x)
Screen. addstr (3, col, "%. 1f °/s" % y)
Screen. addstr (4, col, "%. 1f °/s" % z)
Def displayADXL345 (screen, col, x, y, z ):
"""
How to display the ADXL345 reading
"""
Screen. addstr (1, col, "%. 2fmg" % x)
Screen. addstr (2, col, "%. 2fmg" % y)
Screen. addstr (3, col, "%. 2fmg" % z)
Def displayHMC5883L (screen, col, heading, declination, x, y, z ):
"""
How to display MC5883L readings
"""
Screen. addstr (1, col, heading + "")
Screen. addstr (2, col, declination + "")
Screen. addstr (3, col, "%. 2f" % x)
Screen. addstr (4, col, "%. 2f" % y)
Screen. addstr (5, col, "%. 2f" % z)
Try:
Myscreen = curses. initscr () # initialize curses
Myscreen. border (0)
(Screen_h, screen_w) = myscreen. getmaxyx () # obtain the screen height and width.
Curses. start_color () # Set the color
Curses. init_pair (1, curses. COLOR_BLACK, curses. COLOR_GREEN) # Black text
Curses. init_pair (2, curses. COLOR_RED, curses. COLOR_BLACK) # White-bottom blue
Curses. init_pair (3, curses. COLOR_MAGENTA, curses. COLOR_BLACK) # What is the Black Bottom?
Myscreen. clear () # clear the canvas
# Calculate the coordinates of each piece. The screen is divided into three columns, and each column displays a sensor.
Col1 = screen_w/3*0
Col2 = screen_w/3*1
Col3 = screen_w/3*2
# The screen is divided into three parts horizontally, with the title in the middle of each part
Myscreen. addstr (0, int (col1 + screen_w/3/2-3), "IGT3205", curses. color_pair (1 ))
Myscreen. addstr (0, int (col2 + screen_w/3/2-4), "ADXL345", curses. color_pair (1 ))
Myscreen. addstr (0, int (col3 + screen_w/3/2-4), "HMC5883L", curses. color_pair (1 ))
# Draw a split line and divide the screen into three columns
For col in range (1, screen_h ):
Myscreen. addstr (col, int (col2), "│ ")
Myscreen. addstr (col, int (col3), "│ ")
# Print the names of IGT3205 values in advance
Myscreen. addstr (1, int (col1), "Temp:", curses. color_pair (2 ))
Myscreen. addstr (2, int (col1), "X:", curses. color_pair (2 ))
Myscreen. addstr (3, int (col1), "Y:", curses. color_pair (2 ))
Myscreen. addstr (4, int (col1), "z:", curses. color_pair (2 ))
# Print the name of each value of ADXL345 in advance
Myscreen. addstr (1, int (col2) + 1, "X:", curses. color_pair (2 ))
Myscreen. addstr (2, int (col2) + 1, "Y:", curses. color_pair (2 ))
Myscreen. addstr (3, int (col2) + 1, "z:", curses. color_pair (2 ))
# Print the names of HMC5883L values in advance
Myscreen. addstr (1, int (col3) + 1, "Heading:", curses. color_pair (2 ))
Myscreen. addstr (2, int (col3) + 1, "Declination:", curses. color_pair (2 ))
Myscreen. addstr (3, int (col3) + 1, "X:", curses. color_pair (2 ))
Myscreen. addstr (4, int (col3) + 1, "Y:", curses. color_pair (2 ))
Myscreen. addstr (5, int (col3) + 1, "z:", curses. color_pair (2 ))
# Initialize the Sensor
Itg3205 = i2c_itg3205.i2c_itg3205 (0)
Adxl345 = i2c_adxl345.i2c_adxl345 (0)
Hmc5883l = i2c_hmc5883l.i2c_hmc5883l (0)
Hmc5883l. setContinuousMode () # Set it to the continuous update mode.
Hmc5883l. setDeclination () # sets the True North Magnetic offset compensation.
While True:
# Reading itg3205 data
(Itgready, dataready) = itg3205.getInterruptStatus ()
If dataready:
Temp = itg3205.getDieTemperature ()
(X, y, z) = itg3205.getDegPerSecAxes ()
DisplayITG3205 (myscreen, 6, temp, x, y, z) # refresh the canvas
# Read adxl345 data
(X, y, z) = adxl345.getAxes ()
DisplayADXL345 (myscreen, int (col2) + 4, x, y, z) # refresh the canvas
# Reading hmc5883l data
(X, y, z) = hmc5883l. getAxes ()
Heading = hmc5883l. getHeadingString () # obtain the pointing Angle
Declination = hmc5883l. getDeclinationString () # obtain the magnetic offset Compensation information
DisplayHMC5883L (myscreen, int (col3) + 13, heading, declination, x, y, z) # refresh the canvas
Myscreen. refresh () # application canvas
Sleep (0.1) # Pause 0.1 seconds
Myscreen. getch ()
Finally:
Curses. endwin ()