add files and projects

This commit is contained in:
Sem van der Hoeven
2025-09-02 20:05:48 +02:00
commit 4bb2769e37
66 changed files with 5591 additions and 0 deletions

View File

@@ -0,0 +1,458 @@
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TM1637Display.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
#define INITIALISING 0
#define GPU_READING 1
#define CPU_READING 2
#define TIME_READING 3
#define CORE_READING 4
#define CORE_NUMBER_READING 5
#define NOT_READING 99
#define GPU_START_ROW 0
#define GPU_START_COL 50
#define CPU_START_ROW 0
#define CPU_START_COL 50
#define WINDOW_SWITCH_DELAY 5000
// 7 segment defines
#define CLK 4
#define DIO 5
#define SEGMENT_DELAY_TIME 20
#define SEGMENT_SWITCH_DELAY 5000
#define SEGMENT_TIME_ANIM_DELAY 15000
// Create display object of type TM1637Display:
TM1637Display seg = TM1637Display(CLK, DIO);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
uint32_t window_timer; // 32 bit timer
uint32_t segment_timer; // timer to show 7 segment animation
uint32_t serial_timer; // timer to read serial input data and handle it
uint8_t segment_counter = 0;
int current_window = 0;
void (*fp[2]) (void); // array of function pointers, TODO expand
//uint8_t gpu_graph[128];
//static uint8_t gpu_index = 0;
int index = 0;
char received;
uint8_t state = INITIALISING;
char gpuTemp[3] = " ";
char cpuTemp[3] = " ";
char timeArr[4] = "0000";
size_t initted = 0;
size_t current_core = 0;
uint8_t cpuGraphIndex;
uint8_t gpuGraphIndex;
uint8_t cpuGraph[128] = {0};
uint8_t gpuGraph[128] = {0};
//------------------ 7 segment animations ------------------------
void streak()
{
seg.clear();
uint8_t arr[4] = {0x00, 0x00, 0x00, 0x00};
for (int i = 0; i < 4; i++)
{
arr[i] = SEG_F | SEG_E;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_F | SEG_E | SEG_A | SEG_G | SEG_D;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = 0xff;
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_A | SEG_G | SEG_D | SEG_B | SEG_C;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_B | SEG_C;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = 0x00;
seg.setSegments(arr);
}
for (int i = 3; i >= 0; i--)
{
arr[i] = SEG_B | SEG_C;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_A | SEG_G | SEG_D | SEG_B | SEG_C;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = 0xff;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_F | SEG_E | SEG_A | SEG_G | SEG_D;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_F | SEG_E;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = 0x00;
seg.setSegments(arr);
}
}
void blocks()
{
seg.clear();
uint8_t arr[4] = {0x00, 0x00, 0x00, 0x00};
for (int i = 0; i < 4; i++)
{
arr[i] = SEG_A | SEG_F | SEG_B | SEG_G;
seg.setSegments(arr);
delay(30);
}
for (int i = 0; i < 4; i++)
{
arr[i] = 0x00;
seg.setSegments(arr);
delay(30);
}
for (int i = 3; i >= 0; i--)
{
arr[i] = SEG_E | SEG_C | SEG_D | SEG_G;
seg.setSegments(arr);
delay(30);
}
for (int i = 3; i >= 0; i--)
{
arr[i] = 0x00;
seg.setSegments(arr);
delay(30);
}
}
void clear_animation()
{
uint8_t arr[4] = {0x00, 0x00, 0x00, 0x00};
for (int i = 0; i < 4; i++)
{
arr[i] = SEG_F | SEG_E;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_F | SEG_E | SEG_A | SEG_G | SEG_D;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = 0xff;
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_A | SEG_G | SEG_D | SEG_B | SEG_C;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = SEG_B | SEG_C;
seg.setSegments(arr);
delay(SEGMENT_DELAY_TIME);
arr[i] = 0x00;
seg.setSegments(arr);
}
}
void (*anim_ptrs[2])() = {streak, blocks};
// --------------------------------------------------------
void setup() {
Serial.begin(9600);
TXLED1; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF
fp[0] = display_gpu;
fp[1] = display_cpu;
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
//TODO handle error
Serial.println("error beginning display!");
}
display.display();
delay(100);
display.clearDisplay();
display.cp437(true); // Use full 256 char 'Code Page 437' font
seg.clear();
seg.setBrightness(7);
window_timer = millis();
segment_timer = millis();
serial_timer = millis();
}
void loop() {
// return;
// put your main code here, to run repeatedly:
received = Serial.read();
if (received == 'i')
{
initted = 1;
state = NOT_READING;
}
// char *res = sprintf("received: %d", received);
// Serial.println(res);
if (!initted)
{
if (segment_timer != 0)
{
if ((millis() - segment_timer) > SEGMENT_SWITCH_DELAY)
{
segment_counter++;
if (segment_counter > 1) segment_counter = 0;
segment_timer = millis();
}
}
(*anim_ptrs[segment_counter])();
// return;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE); // Draw 'inverse' text
display.setCursor(0, 0);
display.println("just a sec..");
Serial.println("not init");
display.display();
delay(300);
return;
}
if ((millis() - serial_timer) > 300)
handle_data();
}
void handle_data()
{
// Serial.println("initted");
display.clearDisplay();
if (received == ':')
{
Serial.println("switching to graphics reading");
state = GPU_READING;
index = 0;
}
else if (received == ';')
{
Serial.println("switching to cpu reading");
state = CPU_READING;
index = 0;
} else if (received == 't')
{
Serial.println("switching to time reading");
state = TIME_READING;
index = 0;
}
else if (received == 'e')
{
state = NOT_READING;
}
else
{
if (state != NOT_READING)
{
if (received == "i") return; // if we are already initted but the C# program only just initted
// we are receiving numbers
Serial.print("receiving numbers, received '");
Serial.print(received);
Serial.println("'");
set_data(received);
} else {
Serial.println("not reading");
}
}
// if (window_timer == 0) return;
if (millis() - window_timer > WINDOW_SWITCH_DELAY)
{
next_window();
display_animation();
window_timer = millis();
}
update_time();
(*fp[current_window]) ();
display.display();
}
void set_data(char ree)
{
if (state == CPU_READING)
{
cpuTemp[index] = ree;
if (index == 1) handle_graphs();
}
if (state == GPU_READING)
{
gpuTemp[index] = ree;
if (index == 1) handle_graphs();
// if (index == 1)
// {
// gpu_graph[gpu_index] = 10 * gpuTemp[0] + gpuTemp[1];
// gpu_index++;
// if (gpu_index > 127) gpu_index = 127;
// }
}
if (state == TIME_READING)
{
timeArr[index] = ree;
}
index++;
}
void handle_graphs()
{
if (state == CPU_READING)
{
cpuGraph[cpuGraphIndex] = atoi(cpuTemp);
if (cpuGraphIndex == 127) // we are at the end of the array, we need to shift everything 1 to the right
{
for (uint8_t i = 0; i < cpuGraphIndex; i++)
{
cpuGraph[i] = cpuGraph[i + 1];
}
}
cpuGraphIndex++;
if (cpuGraphIndex >= 127)
{
cpuGraphIndex = 127;
}
}
if (state == GPU_READING)
{
gpuGraph[gpuGraphIndex] = atoi(gpuTemp);
if (gpuGraphIndex == 127) // we are at the end of the array, we need to shift everything 1 to the right
{
for (uint8_t i = 0; i < gpuGraphIndex; i++)
{
gpuGraph[i] = gpuGraph[i + 1];
}
}
gpuGraphIndex++;
if (gpuGraphIndex >= 127)
{
gpuGraphIndex = 127;
}
}
}
void next_window()
{
current_window++;
if (current_window > 1)
{
current_window = 0;
}
}
void display_animation()
{
for (uint16_t i = 0; i < 10; i++)
{
display.fillRect(0, i * (display.height() / 10), display.width(), display.height() / 10, SSD1306_WHITE);
display.display();
delay(10);
}
for (uint16_t i = 0; i < 10; i++)
{
display.fillRect(0, i * (display.height() / 10), display.width(), display.height() / 10, SSD1306_BLACK);
display.display();
delay(10);
}
display.clearDisplay();
}
void display_gpu(void)
{
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.setCursor(0, GPU_START_ROW); // Start at top-left corner
display.print("GPU:");
display.setTextColor(SSD1306_WHITE);
display.setCursor(GPU_START_COL, GPU_START_ROW);
display.print(gpuTemp[0]);
display.print(gpuTemp[1]);
display.print(" C");
for (uint8_t i = 0; i < gpuGraphIndex; i++)
{
uint8_t newVal = ((float)gpuGraph[i] / 100.0f) * 40;
uint8_t yPos = (SCREEN_HEIGHT - newVal) - 1;
if (yPos >= 0 && yPos < 64)
{
display.drawPixel(i, yPos, SSD1306_WHITE);
}
}
}
void display_cpu(void)
{
display.setTextSize(2);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.setCursor(0, CPU_START_ROW);
display.print("CPU:");
display.setTextColor(SSD1306_WHITE);
display.setCursor(CPU_START_COL, CPU_START_ROW);
display.print(cpuTemp[0]);
display.print(cpuTemp[1]);
display.print(" C");
for (uint8_t i = 0; i < cpuGraphIndex; i++)
{
uint8_t newVal = ((float)cpuGraph[i] / 100.0f) * 40;
uint8_t yPos = (SCREEN_HEIGHT - newVal) - 1;
if (yPos >= 0 && yPos < 64)
{
display.drawPixel(i, yPos, SSD1306_WHITE);
}
}
}
void update_time()
{
if (millis() - segment_timer > SEGMENT_TIME_ANIM_DELAY)
{
streak();
segment_timer = millis();
}
int x = atoi(timeArr);
seg.showNumberDecEx(x, 0b11100000, false, 4, 0);
}