STM32CubeF7 BSP LCD attempt - c

I'm trying to display a string on stm32f769i-disco's LED with BSP libraries from STM32CubeF7. However, nothing happens. Here is the code:
#include "stm32f7xx_hal.h"
#include "stm32f769i_discovery.h"
#include "stm32f769i_discovery_lcd.h"
#include "stm32f7xx.h"
#include <stdio.h>
char str[] = "Hello from BSP LCD demo!";
void LCDInit() {
// Initialize the LCD using the BSP_LCD_Init() function.
BSP_LCD_Init();
// Select the LCD layer to be used using the BSP_LCD_SelectLayer() function.
//BSP_LCD_SelectLayer(0);
BSP_LCD_LayerDefaultInit(LTDC_DEFAULT_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
BSP_LCD_SelectLayer(LTDC_DEFAULT_ACTIVE_LAYER);
// Enable the LCD display using the BSP_LCD_DisplayOn() function.
BSP_LCD_DisplayOn();
// Clear the whole LCD using BSP_LCD_Clear() function or only one specified string line using the BSP_LCD_ClearStringLine() function.
BSP_LCD_Clear(LCD_COLOR_LIGHTGRAY);
HAL_Delay(1000);
BSP_LCD_SetBackColor(LCD_COLOR_LIGHTGRAY);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
// Display a character on the specified line and column using the BSP_LCD_DisplayChar() function or a complete string line using the BSP_LCD_DisplayStringAtLine() function.
BSP_LCD_DisplayStringAt(100, 100, str, CENTER_MODE);
}
int main(void) {
LCDInit();
BSP_LED_Init(LED_GREEN);
while(1) {
for (int i=0;i<1000000;i++);
BSP_LED_Toggle(LED_GREEN);
}
return 0;
}
If I remove the call to LCDInit, my LED toggles, if I call the LCDInit, nothing happens (the LED doesn't toggle) and the LCD stays black. Any ideas?
I've basically tried following the instructions in stm32f769i_discovery_lcd.c, without luck:
2. Driver description:
---------------------
+ Initialization steps:
o Initialize the LCD using the BSP_LCD_Init() function.
o Select the LCD layer to be used using the BSP_LCD_SelectLayer() function.
o Enable the LCD display using the BSP_LCD_DisplayOn() function.
+ Options
o Configure and enable the color keying functionality using the
BSP_LCD_SetColorKeying() function.
o Modify in the fly the transparency and/or the frame buffer address
using the following functions:
- BSP_LCD_SetTransparency()
- BSP_LCD_SetLayerAddress()
+ Display on LCD
o Clear the whole LCD using BSP_LCD_Clear() function or only one specified string
line using the BSP_LCD_ClearStringLine() function.
o Display a character on the specified line and column using the BSP_LCD_DisplayChar()
function or a complete string line using the BSP_LCD_DisplayStringAtLine() function.
o Display a string line on the specified position (x,y in pixel) and align mode
using the BSP_LCD_DisplayStringAtLine() function.
o Draw and fill a basic shapes (dot, line, rectangle, circle, ellipse, .. bitmap)
on LCD using the available set of functions.
EDIT: When debugging with OpenOCD, gdb hangs if I set the breakpoint at the BSP_LCD_Init() line. If I run the debugger again, I can see the program is stuck at the WWDG_IRQHandler ().

In case this ever helps anyone, I'm going to post what turned out to be my issue (with the HAL library):
I have not particularly added any code that deals with overriding interrupt handlers, turns out even the HAL_init() call was blocked, because I had not added the following:
void SysTick_Handler(void)
{
HAL_IncTick();
}
Because of this, my HAL_Delay's would be waiting forever. Probably best to use the templates provided in the STM32CubeF7 templates folder, when starting out, so you don't make the same mistakes as I did..

Related

Calling Chicken Scheme function from SDL2 audio callback function hangs

I am trying to embed Chicken Scheme into a C program, to generate sounds to be played with SDL2's audio system. I would have liked to use the sdl2 egg, but it does not seem to support Audio yet (despite the documentation mentioning the 'audio flag for the init! function).
At first, I was using SDL_QueueAudio from C, passing it a buffer that I had allocated in C and then filled in Scheme. This worked fine, passing a Sint16 * and size_t into Scheme, then using pointer-s16-set! from Scheme to fill it and returning a size_t to note how many cells were filled.
Then, when I realised that using the callback api for generating the audio was much better suited to this, I tried switching to it (having already used it before in C), only for the Scheme function to never be entered. Logging something in the callback function before the Scheme call worked, but logging directly within the Scheme function, or after the Scheme call, never happened.
I can only imagine that this is due to SDL2's audio callback running on a separate thread, and that messing with calling through to Scheme somehow. With this in mind, I tried calling CHICKEN_run(C_toplevel); from within the callback function, the first time that it was called, but that only resulted in a bus error.
So my question is: is there a way of calling embedded Chicken Scheme from SDL2's audio callback?
I am on macOs 10.13.6 High Sierra, with SDL2 and chicken both installed and up-to-date through Homebrew.
I compile with (as I said, this works fine when using the queue audio api):
csc code.c codescm.scm -embedded -o code -L -lSDL2
My simplified code is below:
#include <chicken.h>
#include "SDL2/SDL.h"
extern size_t fill_sound_buffer(Sint16 *buffer, size_t buffer_length);
void fill_sound_callback(void *user_data, Uint8 *stream, int stream_length)
{
// Logging here prints to the console
fill_sound_buffer((Sint16 *)stream, stream_length / 2);
// Logging here does not print to the console
}
void play(void)
{
SDL_AudioSpec audio_want;
SDL_zero(audio_want);
audio_want.freq = 44100;
audio_want.format = AUDIO_S16SYS;
audio_want.channels = 1;
audio_want.samples = 2048;
audio_want.callback = fill_sound_callback;
SDL_AudioSpec audio_have;
SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(NULL, 0, &audio_want, &audio_have, 0);
SDL_PauseAudioDevice(audio_device, 0);
SDL_Delay(5000);
// Logging here shows up after 5 seconds, but the program then continues to wait
SDL_CloseAudioDevice(audio_device);
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_AUDIO);
CHICKEN_run(C_toplevel);
play();
SDL_Quit();
return 0;
}
(import (chicken format)
(chicken foreign)
(chicken memory)
(chicken platform))
(define-external (fill_sound_buffer ((c-pointer short) buffer) (size_t buffer_length)) size_t
; This never prints when using the callback api
(printf "In Scheme~%")
; Removed the code that calculates a sine wave and fills the buffer with it, which works
0)
(return-to-host)

Controlling a keypad with same I/O in C

i have a problem that i have been at for over a day now, and i can not solve this with my skills.
So the problem is as follows. I am trying to get an output from a 4x4 keypad, which would not be a problem but input and output are on the same pins. I know that the 74hc573 should keep the information after LE goes low, but i just can not figure out how to read output from 74hc541 without giving new information to the previous chip, because then the state changes again. At the moment i can only read the keys diagonally, because input and output match in that case.
The schematic of the whole circuit can be seen here:
and the problematic part here:
I have tried many different way in C to make it work, but the best I can do is diagonally from 1 to C because of the shared I/O.
Hope you guys can give a tip and help me understand this.
and my code, it is only the part that should take care of getting the output.
while(1)
{
for(i=0;i<4;i++)
{
P3_7=0;
P3_6=1;
in=((0b11110111>>i)&0b00001111);
//in=0b11110111;
*keypad=in;
*led=in;
P3_7=0;
P3_6=0;
*keypad=0x00;
P3_7=1;
out=*keypad;
P3_7 would be RD and P3_6 would be WR, havent given them proper defines yet
Modified code
while(1)
{
for(i=0;i<4;i++)
{
P3_7=0;
P3_6=1;
in=((0b11110111>>i)&0b00001111);
*keypad=in;
*led=*keypad;
vardelay(100);
P3_7=0;
P3_6=0;
*keypad=0xff;
P3_7=1;
out=(*keypad&0b00001111);
if (in==0b1101&&out==0b1101)
{
P3_7=1;
P3_6=1;
lcd_senddata('5');
}
else if(in==0b1110&&out==0b1101)
{
P3_7=1;
P3_6=1;
lcd_senddata('2');
}
When clicking '5' it prints both 5 and 2. And i am not sure why
The key is being able to control the LE pin. Latch Q1 on, then disable the LE. Scan A1-A4 to test buttons 1, 2, 3 and F. The state Q1-4 shouldn't change during the scan if LE is low. Then enable LE, switch to Q2, disable LE and scan the next row, and so on.
Turns out i was over thinking it, I had to simply write and read from the same address. That is all. a simple code like this, will work
if (*keypad=0b11111101)
{
out=*keypad;
if (out==0b11111110)
lcd_senddata('4');
else if (out==0b11111101)
lcd_senddata('5');
else if (out==0b11111011)
lcd_senddata('6');
else if (out==0b11110111)
lcd_senddata('E');

Capture image from webcam and display it - OpenCV - Eclipse - Windows

I'm new to OpenCV and I want to display what my webcam sees with OpenCV. I'm using the C Coding Language.
I've tried with this code:
#include <stdio.h>
#include <cv.h> // Include the OpenCV library
#include <highgui.h> // Include interfaces for video capturing
int main()
{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
CvCapture* capture =cvCreateCameraCapture(-1);
if (!capture){
printf("Error. Cannot capture.");
}
else{
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE);
while (1){
IplImage* frame = cvQueryFrame(capture);
if(!frame){
printf("Error. Cannot get the frame.");
break;
}
cvShowImage("Window",frame);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Window");
}
return 0;
}
My webcam's light turns on, but the result is a completely grey window, with no image.
Can you help me?
You need to add
cvWaitKey(30);
to the end of while-loop.
cvWaitKey(x) / cv::waitKey(x) does two things:
It waits for x milliseconds for a key press. If a key was pressed during that time, it returns the key's ASCII code. Otherwise, it returns -1.
It handles any windowing events, such as creating windows with cvNamedWindow(), or showing images with cvShowImage().
A common mistake for opencv newcomers is to call cvShowImage() in a loop through video frames, without following up each draw with cvWaitKey(30). In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cvShowImage().
See What does OpenCV's cvWaitKey( ) function do? for more info.

How To Use SetConsoleCursorPosition Func

I just wrote the code for tower of hanoi in c and I wanted to show the solution in graphical mode using characters.
I want to use windows.h and SetConsoleCursorPosition function to move cursor in console.
Could you help me by telling me have does this function works and how to use it?Please give some examples.
Here's an example of how to call the SetConsoleCursorPosition function, taken from cplusplus:
void GoToXY(int column, int line)
{
// Create a COORD structure and fill in its members.
// This specifies the new position of the cursor that we will set.
COORD coord;
coord.X = column;
coord.Y = line;
// Obtain a handle to the console screen buffer.
// (You're just using the standard console, so you can use STD_OUTPUT_HANDLE
// in conjunction with the GetStdHandle() to retrieve the handle.)
// Note that because it is a standard handle, we don't need to close it.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Finally, call the SetConsoleCursorPosition function.
if (!SetConsoleCursorPosition(hConsole, coord))
{
// Uh-oh! The function call failed, so you need to handle the error.
// You can call GetLastError() to get a more specific error code.
// ...
}
}
You can also find out how to use Win32 functions by checking the SDK documentation. Googling for the name of the function will usually turn up the appropriate doc page as the first hit.
For SetConsoleCursorPosition, the page is here, and for GetStdHandle, the page is here.

erasing terminal output on linux

I was writing a command line program which will have a status bar, much like wget.
The main problem I'm facing is: how do I delete what I've already sent into stdout/stderr?
I had on idea: use the backspace char '\b' and erase the output I've sent. Is that the best way? Is it the only way? Is there a better way?
PS: I don't want to use anything like ncurses. Plain old C please.
Thanks
EDIT:
Can I also go up and/or down? Example: I have 10 lines of output, I want to change the 3rd line from Doing ABC to ABC: Done. How can I do that?
Also, can anyone post more details about what VT102 characters are? What are its capabilities? Please post good links on this if you have any.
Thanks
The basic formatting control characters are backspace (\b), tab (\t), newline (\n), and carriage return (\r). If you need more than that then you can use ANSI X3.64 / ISO/IEC 6429 / ECMA-48 escape sequences; at least the VT100 subset is recognized by most modern terminals and emulators. An advantage of using ncurses is that it will look up the capabilities of your particular terminal and so it will work even if your terminal uses a different set of escape sequences.
You have to remember that as far as the regular stdio routines are concerned, stdout is just a byte stream with no inherent display characteristics; that depends on the target device, which can be anything from a regular VT100-style terminal to a hardcopy terminal to a sheet-fed printer to a plotter to whatever.
IMO, you're far better off using a library like ncurses than trying to hack together your own display management code with VT100 escape codes, even for a relatively simple task like this. I know you want to stick with "plain old C", but this is a task that falls outside the bounds of plain old C.
Use '\r' to return to the beginning of the line and possibly rewrite the whole line.
Look for VT102 control sequences - these are character sequences ESC ... to control your terminal.
There is also the possiblity of using Ncurses, which is a library for Textual UI, where this kind of behaviour should have some support. However, it may be overkill for something like this.
A slight variation on your own solution:
You can also print a carriage return (\r), which will return you to the start of the line.
It is a progressbar for bash.
function gauge()
{
progress="$1"
total="$2"
width=`tput cols`
let gwidth=width-7
if [ "$total" == "0" ]; then
percent=100
else
set +e
let percent=progress*100/total;
set -e
fi
set +e
let fillcount=percent*gwidth/100
let nofillcount=gwidth-fillcount
set -e
fill="";
if [ "$fillcount" -gt "0" ]; then
for i in `seq $fillcount`; do
fill="$fill""|"
done
fi;
nofill=""
if [ "$nofillcount" -gt "0" ]; then
for i in `seq $nofillcount`; do
nofill="$nofill"" ";
done
fi
echo -e -n "\r[""$fill""$nofill""] ""$percent""%";
}
About the progress bar: something like this?
#include <stdio.h>
#include <unistd.h>
typedef enum
{
false=0,
true=!false
} bool;
typedef struct
{
/* Start delimiter (e.g. [ )*/
char StartDelimiter;
/* End Delimiter (e.g. ] )*/
char EndDelimiter;
/* Central block (e.g. = )*/
char Block;
/* Last block (e.g. > ) */
char CurBlock;
/* Width of the progress bar (in characters) */
unsigned int Width;
/* Maximum value of the progress bar */
double Max;
/* True if we have to print also the percentage of the operation */
bool PrintPercentage;
/* True if the bar must be redrawn;
note that this must be just set to false before the first call, the function then will change it by itself. */
bool Update;
} ProgressBarSettings;
/* Prints/updates the progress bar */
void PrintProgressBar(double Pos, ProgressBarSettings * Settings);
/* Inits the settings of the progress bar to the default values */
void DefaultProgressBar(ProgressBarSettings * Settings);
int main()
{
int i;
/* Init the bar settings */
ProgressBarSettings pbs;
DefaultProgressBar(&pbs);
pbs.Max=200;
pbs.Width=60;
printf("Progress: ");
/* Show the empty bar */
PrintProgressBar(0,&pbs);
for(i=0;i<=pbs.Max;i++)
{
/* Wait 50 msec */
usleep(50000);
/* Update the progress bar */
PrintProgressBar(i,&pbs);
}
puts(" Done");
return 0;
}
/* Inits the settings of the progress bar to the default values */
void DefaultProgressBar(ProgressBarSettings * Settings)
{
Settings->StartDelimiter='[';
Settings->EndDelimiter=']';
Settings->Block='=';
Settings->CurBlock='>';
Settings->PrintPercentage=true;
Settings->Update=false;
Settings->Max=100;
Settings->Width=40;
}
/* Prints/updates the progress bar */
void PrintProgressBar(double Pos, ProgressBarSettings * Settings)
{
/* Blocks to print */
unsigned int printBlocks=(unsigned int)(Settings->Width*Pos/Settings->Max);
/* Counter */
unsigned int counter;
/* If we are updating an existing bar...*/
if(Settings->Update)
{
/* ... we get back to its first character to rewrite it... */
for(counter=Settings->Width+2+(Settings->PrintPercentage?5:0);counter;counter--)
putchar('\b');
}
else
Settings->Update=true; /* next time we'll be updating it */
/* Print the first delimiter */
putchar(Settings->StartDelimiter);
/* Reset the counter */
counter=Settings->Width;
/* Print all the blocks except the last; in the meantime, we decrement the counter, so in the end we'll have
the number of spaces to fill the bar */
for(;printBlocks>1;printBlocks--,counter--)
putchar(Settings->Block);
/* Print the last block; if the operation ended, use the normal block, otherwise the one for the last block */
putchar((Settings->Max==Pos)?Settings->Block:Settings->CurBlock);
/* Another block was printed, decrement the counter */
counter--;
/* Fill the rest of the bar with spaces */
for(;counter;counter--)
putchar(' ');
/* Print the end delimiter */
putchar(Settings->EndDelimiter);
/* If asked, print also the percentage */
if(Settings->PrintPercentage)
printf(" %3d%%",(int)(100*Pos/Settings->Max));
/* Flush the output buffer */
fflush(stdout);
};
Note: the unistd.h and usleep thing is just to fake the progress of an operation, the progress bar code itself just uses the standard library. Its only assumptions about the output stream are that \b actually gets to the previous written character. I tried it successfully on Windows and Linux (with gnome-terminal), don't know if it doesn't work correctly with some terminal emulators.
Sorry for the excessive amount of comments, I wrote it for another forum where I needed to explain pratically every line of the code to a C newbie.

Resources