CPU Temperature on Systray (Raspberry PI)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

CPU Temperature on Systray (Raspberry PI)

Post by mk-soft »

My first mini application for Raspberry (PB v6.00 Beta) :wink:

Update v1.01.3
- Position of PopupMenu

Update v1.02.1
- Linux System Info

CPUtemp.pb

Code: Select all

;-TOP

; Comment : Systray CPU Temperatur
; Author  : mk.soft
; Version : v1.01.4
; Create  : 19.12.2021
; Update  : 26.12.2022

; License : MIT

; ********************************

#ProgramTitle = "SysTray CPU Temperature"
#Version = "v1.02.1"

; ----

ImportC ""
  gtk_status_icon_get_geometry(status_icon, pp_screen, p_area, p_orientation)
EndImport

IncludeFile "systeminfo.pb"

; ----

Enumeration Windows
  #Main
EndEnumeration

Enumeration Menus
  #MainMenu
  #SystrayMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuExitApplication
  #MainMenuHideWindow
  #MainMenuShowWindow
  #MainMenuInfo
EndEnumeration

Enumeration Gadgets
  #MainInfo
  #MainInfoButton
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

Enumeration Images
  #ImageSystray
EndEnumeration

Enumeration Fonts
  #FontFreeMono
  #FontMonoSpace
EndEnumeration

Enumeration SystrayIcons
  #SystrayIcon
EndEnumeration

Global ExitApplication

; ----

#fileTemp = "/sys/class/thermal/thermal_zone0/temp"

Procedure.f get_cpu_temp()
  Protected r1.f, file, temp.s
  
  file = ReadFile(#PB_Any, #fileTemp)
  If file
    temp = ReadString(file, #PB_UTF8)
    CloseFile(file)
    r1 = 0.001 * Val(temp)
  Else
    r1 = 99.0
  EndIf
  ProcedureReturn r1
EndProcedure

; ----

Procedure InitResources()
  
  If Not CreateImage(#ImageSystray, 32, 32)
    Debug "Error: Create Images"
  EndIf
  
  If Not LoadFont(#FontFreeMono, "FreeMono.ttf", 8)
    Debug "Error: Load Font FreeMono"
  EndIf
  
  If Not LoadFont(#FontMonoSpace, "Monospace", 10)
    Debug "Error: Load Font FreeMono"
  EndIf
  
  If CreatePopupMenu(#SystrayMenu)
    MenuItem(#MainMenuShowWindow, "Show Window")
    MenuItem(#MainMenuHideWindow, "Hide Window")
    MenuBar()
    MenuItem(#MainMenuExitApplication, "Exit Program")
  Else
    Debug "Error: Create Popup Menu"
  EndIf
    
EndProcedure

; ----

Procedure DrawSystrayImage()
  Protected temp.f, backgroundcolor
  
  temp = get_cpu_temp()
  If StartDrawing(ImageOutput(#ImageSystray))
    If temp < 70.0
      backgroundcolor = $DCDCDC
    ElseIf temp < 80.0
      backgroundcolor = $00D7FF
    Else
      backgroundcolor = $0C0CEC
    EndIf
    Box(0, 0, 32, 32, $FFB863)
    Box(2, 2, 28, 28, backgroundcolor)
    DrawingFont(FontID(#FontFreeMono))
    DrawText(5, 4, "CPU", #Black, backgroundcolor)
    DrawText(4, 16, StrF(temp, 1), #Black, backgroundcolor)
    StopDrawing()
  EndIf
  
EndProcedure

; ----

Procedure DoEventTimer()
  DrawSystrayImage()
  ChangeSysTrayIcon(#SystrayIcon, ImageID(#ImageSystray))
EndProcedure

; ----

Procedure UpdateSystemInfo()
  Protected NewList SystemInfo.s()
  
  run_pb_systeminfo(SystemInfo())
  run_uname(SystemInfo())
  run_hostnamectl(SystemInfo())
  run_lscput(SystemInfo())
  run_free(SystemInfo())
  run_df(SystemInfo())
  run_lsblk(SystemInfo())
  
  ClearGadgetItems(#MainInfo)
  ForEach SystemInfo()
    AddGadgetItem(#MainInfo, -1, SystemInfo())
  Next
EndProcedure

; ----
Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainInfo, 0, 0, dx, dy - 40)
  ResizeGadget(#MainInfoButton, 5, dy - 35, 120, 30)
EndProcedure

; ----

Procedure Main()
  Protected systray, r1, rect.GdkRectangle, orientation
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_Invisible
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 600, 400, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("File")
    MenuItem(#MainMenuInfo, "System Info")
    MenuBar()
    MenuItem(#MainMenuExitApplication, "E&xit")
    
    ; StatusBar  
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    StatusBarText(#MainStatusBar, 0, " " + #Version)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - MenuHeight() - StatusBarHeight(#MainStatusBar)
    EditorGadget(#MainInfo, 0, 0, dx, dy - 40, #PB_Editor_ReadOnly)
    ButtonGadget(#MainInfoButton, 5, dy - 35, 120, 30, "System Info")
    
    ; Startup
    InitResources()
    
    ; Set font
    SetGadgetFont(#MainInfo, FontID(#FontMonoSpace))
    
    ; Create Systray
    DrawSystrayImage()
    systray = AddSysTrayIcon(#SystrayIcon, WindowID(#Main), ImageID(#ImageSystray))
    SysTrayIconToolTip(#SystrayIcon, "CPU Temperature")
    
    ; Bind events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    AddWindowTimer(#Main, 1, 5000)
    BindEvent(#PB_Event_Timer, @DoEventTimer(), #Main, 1)
    
    ; Main loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          HideWindow(#Main, #True)
          ;ExitApplication = #True
          
        Case #PB_Event_Menu
          Select EventMenu()
            Case #MainMenuExitApplication
              ExitApplication = #True
              
            Case #MainMenuHideWindow
              HideWindow(#Main, #True)
              
            Case #MainMenuShowWindow
              HideWindow(#Main, #False)
              
            Case #MainMenuInfo
              UpdateSystemInfo()
              
          EndSelect
          
        Case #PB_Event_SysTray
          Select EventType()
            Case #PB_EventType_LeftClick
              r1 = gtk_status_icon_get_geometry(systray, 0, @rect, @orientation)
              DisplayPopupMenu(#SystrayMenu, WindowID(#Main), rect\x, rect\y)
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainInfoButton
              UpdateSystemInfo()
              
          EndSelect
          
      EndSelect
      
    Until ExitApplication
    
  EndIf
  
EndProcedure : Main()
Autostart File: "/home/pi/.config/autostart/CPUtemp.desktop
[Desktop Entry]
Type=Application
Name=CPU Temperature Autostart
Comment=Systay
NoDisplay=false
# Change to app
Exec=/home/pi/Apps/CPUtemp
Last edited by mk-soft on Mon Dec 26, 2022 4:53 pm, edited 3 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5888
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: CPU Temperature on Systray (Raspberry PI)

Post by idle »

Great thanks for sharing
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: CPU Temperature on Systray (Raspberry PI)

Post by mk-soft »

Update v1.01.3
- Position of PopupMenu

Unfortunately, the function SysTrayID is still missing, but when creating the systray without #PB_Any you get the ID.
With 'gtk_status_icon_get_geometry' you can get the position and size of the systray and set the position of the PopupMenu better. Looks better ;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5888
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: CPU Temperature on Systray (Raspberry PI)

Post by idle »

that's much better thanks
User avatar
mk-soft
Always Here
Always Here
Posts: 6242
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: CPU Temperature on Systray (Raspberry PI)

Post by mk-soft »

Update v1.02.1
- Linux System Info
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply