Error in the "Expression" window on STM32CubeIDE - function

I want to display the value of these two variables and also their position in memory.
I can see the green one on the right in the Expressions window ... the red one cannot.
char my_data_RAM[] = "I'm in RAM";
char const my_data_FLASH[] = "I'm in FLASH";
int main(void)
{
/* USER CODE BEGIN 1 */
char f = my_data_FLASH[1];
char r = my_data_RAM[0]; //var. locale
while(1) {
}
}
enter image description here
enter image description here
Any solutions?
I think the problem is due to 'char const' initialisation ?

Related

Parameter Passing By Value Result & Name

I am trying to understand how the concepts of parameter passing by Value Result and parameter passing by Name would apply to the following program:
// GLOBAL variables
int num[10] = {0,0,0,0,0,0,0,0,0,0}; // Subscripts start at 0 as in C++
int index = 1;
void somefun(int alpha, int beta)
{
alpha = 7;
num[index] = 33;
index = index - 6;
num[alpha] = 44;
beta = 55;
}
void main()
{
somefun( index, num[index + 4] ); // Function Call
}
This program is pseudo-code, not specific to any language, and the array subscripts start at 0.
If someone could help me understand how this program would work with parameter passing by Value Result and parameter passing by Name, I would greatly appreciate it. Thank you.
Unfortunately, I was not able to try anything on my own thus far.

principal = 10000, interest rate = 5% , 10000 → 20000 when will it double?

#include <stdio.h>
int main()
{
int x=5; //x = interest rate(5%)
int y=10000; //y = principal
int n = 0; //n = after years
while (1)
{
n++;
y += y*(x/100);
if(y == 20000)
break;
}
printf("%d years later, double.",n);
return 0;
}
When I run it, nothing happens.
Description Resource Path Location Type
cannot open output file mm.exe: Permission denied mm C/C++ Problem
I would appreciate it if you let me know what went wrong.
Since you have X as an integer and it's value is 5, at
y+= y*(x/100)
Is equivalent to
y+= 0
as (5/100) with integer division yields 0. This results in while(1) looping infinitely, and thus will never allow the program to terminate.
Additionally, the permission denied error looks like it can be fixed by changing your save file location. Here is my source and some extra info
Hope this helps!

Trying to get my own printf function to work with variables

I am programming a display and I am able to display characters on the display by using this function:
void printChar(char *tekst, uint16_t xPos, uint16_t yPos)
{
//calculate the position the first byte should be placed on
uint16_t startPos = yPos * width_disp_byte + (xPos/8);
int i;
//put all 16 bytes on the right place on the display based on the users' input
for(i=0;i<16;i++)
{
test_image[startPos]=convertChar(*tekst,i);
startPos += width_disp_byte;
}
}
Basically I get a character and find its location in an array that I build. Than I take 16 bytes of data and put this in the display.
The next step is to display integer variables on the display. I have written a code that looks like this:
void printVariable(uint16_t integer, uint16_t xPos, uint16_t yPos)
{
uint16_t value;
uint16_t remainder;
char printValue;
//max value is 9999 that can be displayed, caused by the '4' in the for loop
for(int i = 0; i < 4;i++)
{
value = integer;
//int_power calculates the divisor. going from 3 to 0. cq 1000,100,10,1.
value /= int_power(10,(3-i));
//remove highest number from integer value (for 312, remove the 3 by substracting 300)
integer -= (value) * (int_power(10,(3-i)));
// add '0' to value to get the correct ASCII value.
value += '0';
// convert uint16_t value into char
printValue = value;
printChar(printValue,xPos,yPos);
xPos += 8;
}
}
I take a variable, lets say 3164. The first step is to divide this by 1000. The answer will be 3, since it's an integer. I display this character using the printChar function.
the next step removes 3000 from 3164 and divides the value by 100, resulting in 1. Again this value is printed using the printf function. Then 100 is removed from from 164 and then gets divided by 10 etc etc.
This code is quite limited in its use, but it fits perfectly in what I want to achieve. There is no need to print variables within a string.
The problem here is that the printChar function does not work like I have written in the code. Normally I would use the printChar function like this:
printChar("F",0,0);
This would print the character F in the topleft corner. If I want to use the printChar function like this, it doesn't work:
printChar(printValue,xPos,yPos);
The warning message says:
incompatible integer to pointer conversion passing 'char' to parameter of type 'char *'; take the address with &
If I take the address with & I don't get the correct value displayed on my display.
How can I fix this?
You only want to print ONE character, so you do not need a pointer as parameter. Your function would work like this:
void printChar(char tekst, uint16_t xPos, uint16_t yPos){
...
//depending on the Parameters of your convertChar- function either
... = convertChar(tekst,i); // if you can modify this function too
... = convertChar(&tekst,i); // if the function is to use as it is
}
The difference is in char tekst instead of char * text

Getting a MySQL Query to save as a global Variable is C

I am having issues getting a function to run and having the results of a MySQL query to be saved as a variable that other functions can use and call upon. I know the results get read from the table as a string. I was able to do this fine when getting the results and converting it to a float and then passing the results to a pointer. But I can not seem to figure out how to get the results as a string, and compare it with another string to see if they match or do not. No matter what I have tried to do, I can not seem to get a value to be saved as a string to a variable outside the function.
Here is the code of how I got it to work as a float:
(Outside the main function)
float temperature_reading;
float *p_temperature_reading= &temperature_reading;
float humidity_reading;
float *p_humidity_reading= &humidity_reading;
The function I have working with the float, that I can save to a global variable
void MIA_get_current_temperature()
{
const char *query = "SELECT Temperature, Humidity FROM `temperature` WHERE Type='Current_Temperature'";
if (mysql_query(conn, query) != 0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(-1);
} else {
MYSQL_RES *query_results = mysql_store_result(conn);
if (query_results)
{ // make sure there *are* results..
MYSQL_ROW row;
while((row = mysql_fetch_row(query_results)) !=0)
{
float f = row[0] ? atof(row[0]) : 0.0f;
float h = row[1] ? atof(row[1]) : 0.0f;
*p_temperature_reading = f;
*p_humidity_reading = h;
printf("The Temp & Hum from DB is: %.1f & %.1f\n", *p_temperature_reading,*p_humidity_reading);
}
/* Free results when done */
mysql_free_result(query_results);
}
}
}
This is the function I can not get to work:
(Outside main Function)
char ac_mode[256];
char *p_ac_mode = &ac_mode[256];
Function:
void MIA_get_desired_temperature()
{
const char *query = "SELECT Mode, Desired_Temperature, Threshold FROM `ac_mode` WHERE Status='ON'";
if (mysql_query(conn, query) != 0)
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(-1);
} else {
MYSQL_RES *query_results = mysql_store_result(conn);
if (query_results)
{ // make sure there *are* results..
MYSQL_ROW row;
while((row = mysql_fetch_row(query_results)) !=0)
{
char *ac = row[0] ? row[0] : "NULL";
float desired_temperature = row[1] ? atof(row[1]) : 0.0f;
int threshold = row[2] ? atof(row[2]) : 0.0f;
*p_ac_mode = *ac;
*p_desired_temperature = desired_temperature;
*p_threshold=threshold;
}
/* Free results when done */
mysql_free_result(query_results);
}
}
}
char *ac is where I want the string to be stored.
This line:
char *p_ac_mode = &ac_mode[256];
..is incorrect. You're probably trying to declare a pointer to the array (or maybe to its contents)... what you're actually doing is declaring a char * that points at the first byte after the array ac_mode. The [256] here isn't indicating that ac_mode has 256 elements, it's indexing the array to get element 256 (which would be the 257th char in the array if it were big enough -- but it's not, so it's outside the array). You're then taking the address of that out-of-bounds char and assign it to p_ac_mode, so that p_ac_mode to points to it.
To point p_ac_mode at the array contents, you'd just use char *p_ac_mode = ac_mode; (which makes it a char * pointing at the first char in the array). To get a pointer to the array itself, you'd use char (*p_ac_mode)[256] = &ac_mode;, which makes it a pointer to a 256-element array of char. In any case there's no need for p_ac_mode at all, because you can access the array through ac_mode directly in all the same places, and the bare array name ac_mode will usually decay to a pointer to its first char anyway.
With this line:
*p_ac_mode = *ac;
..you're copying the first char from string ac to the first char after ac_mode (because that's what p_ac_mode points to, as explained above). I suspect you're actually trying to assign the whole ac string's contents to ac_mode via p_ac_mode -- but that won't work for a few reasons.
An array is actually a block of memory holding a series of values of the same type. Although in many situations the array name will decay to a pointer (the address of the array's first element), the array itself is the block of memory and not the pointer. You can't just assign a pointer (new address) to the array, and array contents aren't automatically copied this way either. The contents need to be copied over element by element, or using a function that copies the contents.
What you need to do is copy the contents of the string from your query results into the ac_mode array with strcpy() or similar. Just changing this line:
*p_ac_mode = *ac;
to this:
strcpy(ac_mode, ac);
...would do that.
You need to use strcpy in your MIA_get_desired_temperature function. Also, you don't need the pointer p_ac_mode. Just copy into ac_mode directly.
strcpy(ac_mode, ac);

NPP BoxFilters and binary data

I'm trying to create NPP example for BoxFiltering but insted of 8-bit greyscale image I have RGBA binary data. My code looks like:
#include "./common/ImagesCPU.h"
#include "./common/ImagesNPP.h"
#include "./common/ImageIO.h"
#include "./common/Exceptions.h"
#include <npp.h>
void boxfilter_transform( Npp8u *oHostSrc, int width, int height ){
size_t size = width * height * 4;
// declare a device image and copy construct from the host image,
// i.e. upload host to device
npp::ImageNPP_8u_C4 oDeviceSrc(oHostSrc);
// create struct with box-filter mask size
NppiSize oMaskSize = {5, 5};
// create struct with ROI size given the current mask
NppiSize oSizeROI = {oDeviceSrc.width() - oMaskSize.width + 1, oDeviceSrc.height() - oMaskSize.height + 1};
// allocate device image of appropriatedly reduced size
npp::ImageNPP_8u_C4 oDeviceDst(oSizeROI.width, oSizeROI.height);
// set anchor point inside the mask to (0, 0)
NppiPoint oAnchor = {0, 0};
// run box filter
NppStatus eStatusNPP;
eStatusNPP = nppiFilterBox_8u_C4R(oDeviceSrc.data(), oDeviceSrc.pitch(),
oDeviceDst.data(), oDeviceDst.pitch(),
oSizeROI, oMaskSize, oAnchor);
NPP_ASSERT(NPP_NO_ERROR == eStatusNPP);
// declare a host image for the result
npp::ImageCPU_8u_C4 oHostDst(oDeviceDst.size());
// and copy the device result data into it
oDeviceDst.copyTo(oHostDst.data(), oHostDst.pitch());
return 0;
}
I try to compile it and get:
npp_filters.cpp: In function ‘void boxfilter_transform(Npp8u*, int, int)’:
npp_filters.cpp:18:44: error: no matching function for call to ‘npp::ImageNPP<unsigned char, 4u>::ImageNPP(Npp8u*&)’
npp_filters.cpp:18:44: note: candidates are:
./common/ImagesNPP.h:52:13: note: template<class X> npp::ImageNPP::ImageNPP(const npp::ImageCPU<D, N, X>&, bool)
./common/ImagesNPP.h:45:13: note: npp::ImageNPP<D, N>::ImageNPP(const npp::ImageNPP<D, N>&) [with D = unsigned char, unsigned int N = 4u]
./common/ImagesNPP.h:45:13: note: no known conversion for argument 1 from ‘Npp8u* {aka unsigned char*}’ to ‘const npp::ImageNPP<unsigned char, 4u>&’
./common/ImagesNPP.h:40:13: note: npp::ImageNPP<D, N>::ImageNPP(const npp::Image::Size&) [with D = unsigned char, unsigned int N = 4u]
./common/ImagesNPP.h:40:13: note: no known conversion for argument 1 from ‘Npp8u* {aka unsigned char*}’ to ‘const npp::Image::Size&’
./common/ImagesNPP.h:35:13: note: npp::ImageNPP<D, N>::ImageNPP(unsigned int, unsigned int, bool) [with D = unsigned char, unsigned int N = 4u]
./common/ImagesNPP.h:35:13: note: candidate expects 3 arguments, 1 provided
./common/ImagesNPP.h:30:13: note: npp::ImageNPP<D, N>::ImageNPP() [with D = unsigned char, unsigned int N = 4u]
./common/ImagesNPP.h:30:13: note: candidate expects 0 arguments, 1 provided
npp_filters.cpp:39:12: error: return-statement with a value, in function returning 'void' [-fpermissive]
What is wrong? Can you give me the right way?
Now my piece of code looks like:
// declare a host image object for an 8-bit RGBA image
npp::ImageCPU_8u_C4 oHostSrc(width, height);
// copy data to oHostSrc.data()
Npp8u *nDstData = oHostSrc.data();
memcpy(nDstData, data, size * sizeof(Npp8u));
// declare a device image and copy construct from the host image,
// i.e. upload host to device
npp::ImageNPP_8u_C4 oDeviceSrc(oHostSrc);
but now I can't declare a device image and copy (last line), get such error: undefined symbol: nppiMalloc_8u_C4. What it can be?
There's no constructor in npp::ImageNPP_8u_C4 for a single host pointer, you need first to wrap the host array in a npp::ImageCPU_8u_C4 like it is done for example in the ImageIO.h you included.
The point is, that your NPP-image needs to get some dimension information, which you are currently missing for oDeviceSrc.