C++ FLTK displaying a sysctl value

those `result_buffer2[sizeof(result_buffer2)-4]=0;` don't work
sizeof(result_buffer) is 100 or whatever is defined
you need to use strlen(buf)-1 to kill the '\n'
 
It looks like I really should have used his newer work in this thread. I was working off the first code example..

SNIP
Code:
 pclose(fp);
result_buffer[sizeof(result_buffer)-1]='\0';
// To represent the actual control character in the code, use its ASCII value:
print_hex(result_buffer);

char char_to_remove = (char)0x0a;
printf("Original string: [%s] \n", result_buffer);
remove_char(result_buffer, char_to_remove);
printf("Filtered string: [%s] \n", result_buffer);
box1->value(result_buffer); // THE BOX HAS NOW AS TEXT THE RESULT OF THE SYSCTL COMMAND
 
Being a silly American stuck on the Imperial System I really enjoy Jose 's example. The (Fl::repeat_timeout) feature is divine.

So 4 different callbacks? That is my first attempt at multiple values displayed.. I know its the bonehead way but I am rubbing sticks together...
 
OK Assignment #2 Sysctl by Name. Please critique.

C++:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/errno.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Output.H>

void box0cb(void* output) {
  size_t size;
  int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
  char buf[128] = {""};
  size_t buflen = sizeof(buf);
  size = sizeof t_dK;

  if (0 == (sysctlbyname("dev.cpu.0.temperature", &t_dK, &size, NULL, 0))) {
    t_C = (t_dK - 2730) / 10;
    t_F = (t_dK * 1.8 / 10) - 459.67;
    printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
    snprintf(buf, buflen, "%d F", t_F);
  } else {
    strerror_r(errno, buf, buflen);
  }

  ((Fl_Output*)output)->value(buf);

  Fl::repeat_timeout(5.0, box0cb, output);
}


void box1cb(void* output) {
  size_t size;
  int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
  char buf[128] = {""};
  size_t buflen = sizeof(buf);
  size = sizeof t_dK;

  if (0 == (sysctlbyname("dev.cpu.1.temperature", &t_dK, &size, NULL, 0))) {
    t_C = (t_dK - 2730) / 10;
    t_F = (t_dK * 1.8 / 10) - 459.67;
    printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
    snprintf(buf, buflen, "%d F", t_F);
  } else {
    strerror_r(errno, buf, buflen);
  }

  ((Fl_Output*)output)->value(buf);
 
  Fl::repeat_timeout(5.0, box1cb, output);
}




void box2cb(void* output) {
  size_t size;
  int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
  char buf[128] = {""};
  size_t buflen = sizeof(buf);
  size = sizeof t_dK;

  if (0 == (sysctlbyname("dev.cpu.2.temperature", &t_dK, &size, NULL, 0))) {
    t_C = (t_dK - 2730) / 10;
    t_F = (t_dK * 1.8 / 10) - 459.67;
    printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
    snprintf(buf, buflen, "%d F", t_F);
  } else {
    strerror_r(errno, buf, buflen);
  }

  ((Fl_Output*)output)->value(buf);

  Fl::repeat_timeout(5.0, box2cb, output);
}


void box3cb(void* output) {
  size_t size;
  int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
  char buf[128] = {""};
  size_t buflen = sizeof(buf);
  size = sizeof t_dK;

  if (0 == (sysctlbyname("dev.cpu.3.temperature", &t_dK, &size, NULL, 0))) {
    t_C = (t_dK - 2730) / 10;
    t_F = (t_dK * 1.8 / 10) - 459.67;
    printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
    snprintf(buf, buflen, "%d F", t_F);
  } else {
    strerror_r(errno, buf, buflen);
  }

  ((Fl_Output*)output)->value(buf);

  Fl::repeat_timeout(5.0, box3cb, output);
}



int main(int argc, char ** argv) {
  Fl_Window *window = new Fl_Window(250,420, "CPU Temperature");

  Fl_Output *box0 = new Fl_Output(100, 050, 70, 30, "CPU0");
  box0->labelsize(20);

  Fl_Output *box1 = new Fl_Output(100, 150, 70, 30, "CPU1");
  box1->labelsize(20);

  Fl_Output *box2 = new Fl_Output(100, 250, 70, 30, "CPU2");
  box2->labelsize(20);

  Fl_Output *box3 = new Fl_Output(100, 350, 70, 30, "CPU3");
  box3->labelsize(20);


  window->end();
  window->show(argc, argv);
  Fl::add_timeout(5.0, box0cb, box0);
  Fl::add_timeout(5.0, box1cb, box1);
  Fl::add_timeout(5.0, box2cb, box2);
  Fl::add_timeout(5.0, box3cb, box3);
  return Fl::run();
}
scbn.jpg
 
I thought I was going to have to add multiple buf() but it works fine with just one buf.

So multiple values in one buffer?
 
try to move the window after outside of the screen and see if all values reset to the last one after dragging it back in view
if the become equal you need more buffers
or obscure it with another window and then unobscure it
 
char buf[128] = "";

double quotes specify a string which is already an array of chars;
char buf[128] = { '\0'}; would be the equivalent with curly braces
 
[128]
That is just characters including (C)/(F) ?
No Labels included in that number right?

Should I use multiples of 8 for increasing?
[128] Seems big to me for a single value like original code. Am I wrong or is there minimal value? No offenses to any fine contributors here. Just learning.
 
I don't know if this is a legitimate concern but I don't like the idea of combining things on one buffer from different sysctl classes.
For example:
sysctl cpu temps
sysctl dev.adc.0.values

Is that valid?
 
well your buf is just a temp storage between popen/sysctlbyname/other_request to the fltk control
so you don't need separate buffers
as you read them one by one even with multiple buffers you will only have the last read in its "class" in a buffer which is not very useful
 
4 functions that differ only by 1 letter are also bogus. figure out how to pack that parameter into the callback somehow, most gui toolkits let you do this somehow with extra user data.
 
Come on coach I have started on a libgpio example. Put me in the game. I have an example to sponge off of now.
I was really expecting to need "ifdef cplusplus" or "extern C" for using C-Code.
 
Once I realized FILE was a "C" construct used in C++ it dawned on me that they have many commonalities. The books and writings say C++ is no longer C- with classes but it own language.
But really it is still a "C" language. You need to have an understanding of "C" for C++ usage.
 
I have been thrown quite the twist for the callback.
From:
size_t_size
to
gpio_handle_t handle;

I will start a new thread FreeBSD libgpio with FLTK to preserve this threads topic.
 
Once I realized FILE was a "C" construct used in C++ it dawned on me that they have many commonalities. The books and writings say C++ is no longer C- with classes but it own language.
But really it is still a "C" language. You need to have an understanding of "C" for C++ usage.
This is one of the reasons why C++ is so popular and Rust is struggling to dislodge it. Direct interop with C is critical.
 
Well on to MinnowboardMax with amd64 using the gpio driver bytgpio.

Look what sysyctl we have here for a FLTK Window....

dev.ow_temp.0.temperature: 21.000C

Daisy Chaining a few together to make it worthwhile project.. Messing around with ssh-forwarding for headless Xorg.

ow.jpg
 
I have been struggling to figure out the difference between Fl_Output and Fl_Value_Output and when I should be using it. It is designed for numbers so should I be using that?
 
I have been struggling to figure out the difference between Fl_Output and Fl_Value_Output and when I should be using it. It is designed for numbers so should I be using that?
It looks like you use just a naked Fl_Box when you want to display a text value. I found this code in the ancient trove:
C++:
Fl_Box *yzone=new Fl_Box(20,370,200,20,"Yellow Zone Info");
yzone->label("Inside the Red Line Area");
yzone->label("Outside of the Red Line Area");

Didn't try it, 'cause I like the gauge so much better. Yes, I do go on sometimes.
 
That big gauge combined with some smaller ones will make a good cockpit starter kit. I have it unachieved but yet to look.

What I am thinking about is whether I could make a generic 'sysctlbyname variable' like Alain did with popen and COMMAND variable.
The problem becomes sysctl values are different data types. So a variable approach would not work. That is just with my limited knowledge.
 
Back
Top