Jump to content

[C/C++] Temperatur auf das LCD Bricklet


Guest Broken_Mind
 Share

Recommended Posts

Guest Broken_Mind

Hallo ich bin's wieder mal  :P

 

Ich wollte die Temperatur vom PTC Bricklet auslesen und aufs LCD Bricklet anzeigen lassen. Jetzt denkt ihr euch bestimmt ist doch easy.  ;D Nur ich versuche mir gerade C/C++ beizubringen und da habe ich leider noch nicht das Wissen  ;D Den Code habe ich aus den Examples zusammen geschnitten und funktioniert denke ich mal bis auf die LCD Ausgabe.

 

#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_ptc.h"
#include "bricklet_lcd_20x4.h"

#define HOST "192.168.2.105"
#define PORT 4223
#define UID_LCD20x4 "d4w" // UID vom LCD20x4
#define UID_PTC "fmJ" // UID vom PTC

///////////////////////////////////////////////////////////////////////////

// Callback function for temperature callback (parameter has unit °C/100)
void cb_temperature(int32_t temperature, void *user_data) {
(void)user_data; // avoid unused parameter warning

printf("Temperature: %f °C.\n", temperature/100.0);
}

int main() {
// Create IP connection
IPConnection ipcon;
ipcon_create(&ipcon);

// Create device object
PTC ptc;
ptc_create(&ptc, UID_PTC, &ipcon);

// Create device object LCD20x4
LCD20x4 lcd;
lcd_20x4_create(&lcd, UID_LCD20x4, &ipcon); 	

// Connect to brickd
if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
	fprintf(stderr, "Could not connect\n");
	exit(1);
}
// Don't use device before ipcon is connected

// Set Period for temperature callback to 1s (1000ms)
// Note: The callback is only called every second if the 
//       temperature has changed since the last call!
ptc_set_temperature_callback_period(&ptc, 1000);

// Register temperature callback to function cb_temperature
ptc_register_callback(&ptc,
                      PTC_CALLBACK_TEMPERATURE, 
                      (void *)cb_temperature,
                      NULL);

// Turn backlight on
lcd_20x4_backlight_on(&lcd);

// Write "Temperatur"
lcd_20x4_write_line(&lcd, 0, 0, "Temperature: %f °C.\n", temperature/100.0);
				  
printf("Press key to exit\n");
getchar();
lcd_20x4_backlight_off(&lcd); // Turn backlight on
ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally

}

Der Error lautet:

57 ...Temperaturanzeige.c `temperature' undeclared (first use this function)

 

Hoffe jemand findet den Fehler. Komme seit 1h nicht drauf  :-\

 

Grüße

Link to comment
Share on other sites

Du muss die Temperatur auch abfragen, sonst kannst du sie nicht anzeigen. Dir fehlen diese beiden Zeilen vor dem lcd_20x4_write_line Aufruf:

 

int32_t temperature;
ptc_get_temperature(&ptc, &temperature);

 

Oder wenn du die Temperatur per Callback auf's LCD schreiben willst dann muss du deine lcd_20x4_write_line Aufruf in die cb_temperature Funktion verschieben. Dazu muss du dann auch das lcd Objekt an die Callback Funktion geben, was du so tun kannst:

 

ptc_register_callback(&ptc,
                      PTC_CALLBACK_TEMPERATURE, 
                      (void *)cb_temperature,
                      &lcd);

 

void cb_temperature(int32_t temperature, void *user_data) {
LCD20x4 *lcd = user_data;

lcd_20x4_write_line(lcd, ...
}

 

Dann kannst du lcd_20x4_write_line nicht wie printf verwenden:

 

lcd_20x4_write_line(&lcd, 0, 0, "Temperature: %f °C.\n", temperature/100.0);

 

Das Formatieren des Textes muss du vorher selber machen, z.B. so:

 

char line[21]; // 20 Zeichen + NUL-Terminator

snprintf(line, sizeof(line), "Temperature: %.2f \xDFC", temperature/100.0);

lcd_20x4_write_line(&lcd, 0, 0, line);

 

Mittels snprintf den String formatieren und ihn dann an lcd_20x4_write_line übergeben.

 

Ich habe hier auch %.2f statt %f verwendet, da %f fix 6 Nachkommastellen verwendet, es reichen aber 2 aus (spart auch Platz). Das .2 drückt aus, dass nur 2 Nachkommastellen ausgegeben werden sollen.

 

Auch funktioniert "°C" nicht, da das Grad Zeichen kein ASCII Zeichen ist und abhängig von deinem Editor verschieden kodiert gespeichert werden kann und keine diese Kodierungen passend für das LCD ist. Das LCD hat seinen eigenen speziellen Zeichensatz und das Gradzeichen steht darin an Position 223 (0xDF). Dies kann man in C/C++ in einem Stringliteral mit "\xDF" hinschreiben.

Link to comment
Share on other sites

Guest Broken_Mind

Danke dir, darauf wäre ich nicht gekommen.  ;D

Ich habe den Code mal nun so abgeändert:

#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_ptc.h"
#include "bricklet_lcd_20x4.h"

#define HOST "192.168.2.105"
#define PORT 4223
#define UID_LCD20x4 "d4w" // UID vom LCD20x4
#define UID_PTC "fmJ" // UID vom PTC

///////////////////////////////////////////////////////////////////////////

// Callback function for temperature callback (parameter has unit °C/100)
void cb_temperature(int32_t temperature, void *user_data) {
(void)user_data; // avoid unused parameter warning

printf("Temperature: %f °C\n", temperature/100.0);
}

void cb_temperature(int32_t temperature, void *user_data) {
LCD20x4 *lcd = user_data;

lcd_20x4_write_line(lcd, ...
}

int main() {
// Create IP connection
IPConnection ipcon;
ipcon_create(&ipcon);

// Create device object
PTC ptc;
ptc_create(&ptc, UID_PTC, &ipcon);

// Create device object LCD20x4
LCD20x4 lcd;
lcd_20x4_create(&lcd, UID_LCD20x4, &ipcon); 	

// Connect to brickd
if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
	fprintf(stderr, "Could not connect\n");
	exit(1);
}
// Don't use device before ipcon is connected

// Set Period for temperature callback to 1s (1000ms)
// Note: The callback is only called every second if the 
//       temperature has changed since the last call!
ptc_set_temperature_callback_period(&ptc, 1000);

// Register temperature callback to function cb_temperature
ptc_register_callback(&ptc,
                      PTC_CALLBACK_TEMPERATURE, 
                      (void *)cb_temperature,
                      &lcd);

// Turn backlight on
lcd_20x4_backlight_on(&lcd);

// Write "Temperatur" to LCD
char line[21]; // 20 Zeichen + NUL-Terminator
    snprintf(line, sizeof(line), "Temperature: %.2f \xDFC", temperature/100.0);
    lcd_20x4_write_line(&lcd, 0, 0, line);
				  
printf("Press key to exit\n");
getchar();
lcd_20x4_backlight_off(&lcd); // Turn backlight off
ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally

}

 

Nun kommt das void cb_temperature(int32_t temperature, void*) schon definiert ist. Was ja eigentlich logisch ist, da weiter oben schon definiert wird. Nur wie fasse ich die beiden zusammen?

Link to comment
Share on other sites

Wir wär's damit:

- der Callback gibt die Temperatur auf der Console und dem LCD aus

- der String darf aber nicht länger als 20 Zeichen sein

- das LCD Sonderzeichen 0xDF habe ich als printf-Paremeter übergeben,

  weil der gcc sonst meckert, dass der Hexcode zu lang ist (weil ein 'C' direkt danach folgt).

 

Achtung:

wenn die Anzahl Stellen der Temperatur sich ändert, überschreibt der String nicht immer alles vom vorigen Wert.

 

#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_ptc.h"
#include "bricklet_lcd_20x4.h"

#define HOST "192.168.2.105"
#define PORT 4223
#define UID_LCD20x4 "d4w" // UID vom LCD20x4
#define UID_PTC "fmJ" // UID vom PTC

///////////////////////////////////////////////////////////////////////////

// Callback function for temperature callback (parameter has unit °C/100)
void cb_temperature(int32_t temperature, void *user_data) {
LCD20x4 *lcd = (LCD20x4*) user_data;
printf("Temperature: %f °C\n", temperature/100.0);

// Write "Temperatur" to LCD
char line[21]; // 20 Zeichen + NUL-Terminator
snprintf(line, sizeof(line), "Temp: %.2f %cC", temperature/100.0, 0xDF);
line[20] = 0;
lcd_20x4_write_line(lcd, 0, 0, line);
}

int main() {
// Create IP connection
IPConnection ipcon;
ipcon_create(&ipcon);

// Create device object
PTC ptc;
ptc_create(&ptc, UID_PTC, &ipcon);

// Create device object LCD20x4
LCD20x4 lcd;
lcd_20x4_create(&lcd, UID_LCD20x4, &ipcon); 	

// Connect to brickd
if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
	fprintf(stderr, "Could not connect\n");
	exit(1);
}
// Don't use device before ipcon is connected

// Set Period for temperature callback to 1s (1000ms)
// Note: The callback is only called every second if the 
//       temperature has changed since the last call!
ptc_set_temperature_callback_period(&ptc, 1000);

// Register temperature callback to function cb_temperature
ptc_register_callback(&ptc,
                      PTC_CALLBACK_TEMPERATURE, 
                      (void *)cb_temperature,
                      &lcd);

// Turn backlight on
lcd_20x4_backlight_on(&lcd);
				  
printf("Press key to exit\n");
getchar();
lcd_20x4_backlight_off(&lcd); // Turn backlight off
ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally

}

Link to comment
Share on other sites

Guest Broken_Mind

Hallo! Ich schon wieder. Ich wollte das Temperatur Bricklet hinzufügen aber habe es mir wohl leichter vorgestellt als es ist.

 

#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_temperature.h"
#include "bricklet_ptc.h"
#include "bricklet_lcd_20x4.h"

#define HOST "192.168.2.105"
#define PORT 4223
#define UID_temperature "dFu" // UID von temperature
#define UID_PTC "fmJ" // UID vom PTC
#define UID_LCD20x4 "d4w" // UID vom LCD20x4

///////////////////////////////////////////////////////////////////////////

// Callback function for temperature callback (parameter has unit °C/100) PTC
void cb_temperature(int32_t temperature, void *user_data) {
LCD20x4 *lcd = (LCD20x4*) user_data;
//	printf("Temperature: %f °C\n", temperature/100.0);

// Write "Temperatur Temp" to LCD
char line[21]; // 20 Zeichen + NUL-Terminator
snprintf(line, sizeof(line), "Temp: %.2f %cC", temperature/100.0, 0xDF);
line[20] = 0;
lcd_20x4_write_line(lcd, 0, 0, line);	
}

// Callback function for temperature callback (parameter has unit °C/100) Temp
void cb_temperature(int16_t temperature, void *user_data) {
LCD20x4 *lcd = (LCD20x4*) user_data;
//	printf("Temperature: %f °C\n", temperature/100.0);



// Write "Temperatur PTC" to LCD
char line[21]; // 20 Zeichen + NUL-Terminator
snprintf(line, sizeof(line), "TempPTC: %.2f %cC", temperature/100.0, 0xDF);
line[20] = 0;
lcd_20x4_write_line(lcd, 1, 0, line);
}

int main() {
// Create IP connection
IPConnection ipcon;
ipcon_create(&ipcon);

// Create device object
Temperature t;
temperature_create(&t, UID_temperature, &ipcon); 

// Create device object
PTC ptc;
ptc_create(&ptc, UID_PTC, &ipcon);

// Create device object LCD20x4
LCD20x4 lcd;
lcd_20x4_create(&lcd, UID_LCD20x4, &ipcon); 	

// Connect to brickd
if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
	fprintf(stderr, "Could not connect\n");
	exit(1);
}
// Don't use device before ipcon is connected

// Set Period for temperature callback to 1s (1000ms)
// Note: The callback is only called every second if the 
//       temperature has changed since the last call!

ptc_set_temperature_callback_period(&ptc, 1000);
temperature_set_temperature_callback_period(&t, 1000);

// Register temperature callback to function cb_temperature
temperature_register_callback(&t,
                              TEMPERATURE_CALLBACK_TEMPERATURE, 
                              (void *)cb_temperature,
                              &lcd);

// Register temperature callback to function cb_temperature
ptc_register_callback(&ptc,
                      PTC_CALLBACK_TEMPERATURE, 
                      (void *)cb_temperature,
                      &lcd);

// Turn backlight on
lcd_20x4_backlight_on(&lcd);
				  
printf("Press key to exit\n");
getchar();
lcd_20x4_backlight_off(&lcd); // Turn backlight off
ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally

}

 

Dachte mir es reicht den vorhandenen Code zu kopieren und abzuändern aber dann kommt folgender Fehler:

cannot resolve overloaded function `cb_temperature' based on conversion to type `void*'
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...