Console prints to display signal level in c - mobile

How to create a mobile signal indicator and horizontal volume control in headless c program?
I know how to create a progress bar ...etc. But in Linux do we have any library support graphic creation like graphic.h in windows

i have found solution to move the cursor to desired places with escape sequence. With that i can create mobile signal indicator like below.
code for the same:
void draw_antenna(int level)
{
static current_level = 0;
unsigned char cnt = 0, l = 0;
//let assume atenna having 5 level (0-5) and 5 is max
// 5x5 matrix and color need to be controled in column
for (cnt = 0; cnt <= ANTENNA_L; cnt++)
{
for (l = 0; l < cnt; l++)
{
if (cnt <= level)
printf("\033[%d;%dH\033[0;42m\033[1;32m%c\033[0m", 15-l, 25+(cnt*2),' ');
else
printf("\033[%d;%dH\033[48;5;7m\033[1;34m%c\033[0m", 15-l, 25+(cnt*2),' ');
fflush(stdout);
}
}
printf("\033[%d;%dH \033[1;33m%.*so\033[1;30m%*s\033[0m", 16, 25, level*2, TT,10-(level*2),"");
//update current status
current_level = level;
}
*
enter image description here

Related

BLE data inconsistency with HM-10 and Arduino Nano

We have a project that controls a led strip of 80, that is connected via mobile app. The components we used are as follows:
Arduino Nano Board
HM - 10 Bluetooth module
WS2812b Led Strip
On the app, user selects the colors(up to 5), animation (optional), animation speed and brightness. The selected configuration is transmitted to the BLE module with a certain throttle and debouncing. The color(selected on a color wheel on the app) and brightness transmits fine, without any issues.
The problem we encounter, is that when certain animation is active, if the user changes the animation speed via the app, the arduino part locks itself and will not accept any more commands.
To activate a configuration with animation, we send the data from app to arduino as follows:
<l255180200,240135068:089;04200>
The format is: < [mode] [colors(with ',')] : [brightness] ; [animationCode(2 digit)] [animationSpeed] >
At first, we had some inconsistencies with consecutive data, so we implemented the following for data acquisition:
void loop()
{
// Read all serial data available, as fast as possible
while (bleSerial.available() > 0)
{
char inChar = bleSerial.read();
if (inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if (inChar == EOP)
{
ended = true;
break;
}
else
{
if (index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if (started && ended)
{
// The end of packet marker arrived. Process the packet
Serial.println(inData);
Serial.println(inData[0]);
char mode = inData[0];
if (mode == 'p')
{
togglePower(inData);
finalizeRead();
return;
}
if (mode == 'b')
{
changeBrightness(inData);
finalizeRead();
return;
}
if (mode == 't')
{
char *themeNo = strtok(NULL, ";");
int themeCode = valueFromString(themeNo, 0, 2);
theme(themeCode);
}
// if (mode == 'e') {
// sound();
// return;
// }
char *colorsWithBrightness = strtok(inData, ";");
char *animation = strtok(NULL, ";");
char *colors = strtok(colorsWithBrightness, ":");
char *brightness = strtok(NULL, ":");
custom(colors, brightness, animation);
finalizeRead();
}
}
void finalizeRead()
{
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
int valueFromString(char *string, int start, int width)
{
int value = 0;
for (int n = 0; n < width; n++)
value = value * 10 + string[start + n] - '0';
return value;
}
So the custom function is called when the mode == 'l'. And in custom function, we perform as follows:
void custom(char *colors, char *brightness, char *animation)
{
char *trueColors = strtok(colors, "l");
char *colorsSplit = strtok(trueColors, ",");
int colorCount = 0;
uint32_t colorArray[5] = {};
while (colorsSplit != NULL)
{
int r = valueFromString(colorsSplit, 0, 3);
int g = valueFromString(colorsSplit, 3, 3);
int b = valueFromString(colorsSplit, 6, 3);
colorArray[colorCount] = strip.Color(r, g, b);
colorCount++;
colorsSplit = strtok(NULL, ",");
}
int ledsPerSegment = ledCount / colorCount;
for (int n = 0; n < colorCount; n++)
{
int currentSegment = (n + 1) * ledsPerSegment;
Serial.println(currentSegment);
for (int z = n * ledsPerSegment; z < (n + 1) * ledsPerSegment; z++)
{
strip.setPixelColor(z, colorArray[n]);
}
}
strip.setBrightness(atoi(brightness));
strip.show();
Serial.println("Done");
if (animation)
{
int animationCode = valueFromString(animation, 0, 2);
int animationSpeed = valueFromString(animation, 2, 3);
Serial.println(animationSpeed);
if (animationCode == 1)
breath(animationSpeed, atoi(brightness));
else if (animationCode == 7)
pulse(animationSpeed, colorArray, colorCount);
}
}
The problem occurs in pulse animation, which is like this:
void pulse(int wait, uint32_t colorArray[], int colorCount)
{
black();
while (bleSerial.available() <= 0)
{
for (i = 0; i < colorCount && bleSerial.available() <= 0; i++)
{
for (j = 0; j < ledCount / 2 && bleSerial.available() <= 0; j++)
{
strip.setPixelColor(39 - j, colorArray[i]);
strip.setPixelColor(40 + j, colorArray[i]);
strip.show();
delay(wait);
}
black();
}
}
}
We initialize the animation without any issues using the following:
<l255180200,240135068:089;04200>
As soon as we adjust the animation speed from the app, we send about 2 new configurations per second, which is the same as above, only with different speed (last 3 characters before '>'). The arduino part randomly receives the data incorrectly, losing 2-3 characters, which is like <l2551800,240135068:089;04200>
The loss of characters usually occurs on different parts of the string, but always consecutive characters are misread happen each time. Also, sometimes we get a backwards question mark.
We are not sure where we implement wrong, so any help is appreciated on solving this issue.
Thanks!

Center text in console with windows api?

Is there a way to use windows API (windows.h) to center the text output in the console window?
Or a function from another library, or a general possibility?
Currently I inserted several control characters, but depending on the resolution and size of the window it doesn't fit.
printf ("\n\t\t\t\t --Men\x81--\n\n\t\t\t 1: Neue Spielrunde\n\t\t\t 2: Charaktere laden\n\t\t\t 3: Spielrunde speichern\n\t\t\t 4: Programm beenden\n\n");
Taking reference of this answer:
#include <windows.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
void print_spaces(int n) {
for (int i = 0; i < n; i++) printf(" ");
}
int main(void) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows, cols_by_2;
// Get console window attributes - no. of columns in this case
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
cols_by_2 = columns / 2;
// Put all options in an array
char options[4][100] = {"1: Neue Spielrunde", "2: Charaktere laden",
"3: Spielrunde speichern", "4: Programm beenden"};
char menu_header[5] = "Men\x81";
int len_header_by_2 = strlen(menu_header) / 2;
print_spaces(cols_by_2 - len_header_by_2);
printf("%s\n", menu_header);
// Find max half-length of string
int max_val = INT_MIN;
for (int i = 0; i < 4; i++) {
int len_str = strlen(options[i]) / 2;
if (max_val < len_str)
max_val = len_str;
}
// Compute spaces to add for max half-length string
int no_of_spaces = cols_by_2 - max_val;
// Print all options using computed spaces
for (int i = 0; i < 4; i++) {
print_spaces(no_of_spaces);
printf("%s\n", options[i]);
}
return 0;
}
Here's the link where I tested the underlying logic (excluding computing window attributes): http://ideone.com/KnPrct
You can actually take full control of the graphics on a win32 console if you wish. Effectively you end up calling AllocConsole, and then FillConsoleOutputAttribute to set the colour attributes of the text, and FillConsoleOutputCharacter to specify the characters to display. It does however mean you end up avoiding cout/cin/printf/scanf entirely, which may or may not be an issue

How can I store the 50ms before and after an audio event in a circular buffer?

I am processing a dataset of 17 hours of audio .wav (16-bit PCM, 192khz), to simulate a "real-time" processing that will be embedded in an ESP32, Arduino DUE or in a RASP, depending on the results.
How am I handling with that now?
First I cut the 17 hours file in a 1 minute samples, after I created a program in C that turns this file into a .CSV (jumping the entire head of .wav and taking only the date field).
PS: I chose CSV to have the data in a better disposition in order to perform tests in Scilab to validate the algorithms.
With this generated .CSV file I run it in a second program, that opens this file and fills a circular buffer with 130ms (24900 values), when the buffer is full, the code begins to calculate the RMS (Root Mean Square) in moving window with 10ms overlap, window size is 30ms . When I get a value greater than 1000 it is considered an event.
Below you can see an illustration of the problem:
Here is shown the Window with 50 ms before and after an event which I mean:
PS: Inicio, Fim and Janela means respectively, Start, End, Window.
My question is:
How should I save these 50ms before and after the event, since the event can occur anywhere in the buffer? And what should I do if the event lasts for more than one window?
Some data to help the understanding:
130ms = 24900 values ​​from my .csv file
50ms = 9600 values
30ms = 5700 values
10ms = 1920 values
I've searched for several sources, but most of the DSP bibliographies and Data Structures treat these topics superficially, just illustrating what a circular buffer is and not how to deal with it in a useful way.
Here is my code sketch, which seems to be taking a wrong approach to the problem, but I really have no idea how to proceed, in this case I created a data-set from 1 to 100 to ease of the debug:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
// Define the size of window 50ms
#define window_size 3 // 30ms
#define buffer_size 13 // 130ms = 50ms + 30ms + 50ms
int main()
{
//define variables.
int buffer[buffer_size]={0}; // create the buffer with 150ms;
int write = 0;
int i = 0, j = 0;
int read = 0;
int read1 =0;
int write1 = 0;
int counter_elements = 0;
int number_lines = 0;
int save_line = 0;
char c;
char str[1024]; // array to hold characters in a conversion of char to int.
int inicio = 0, fim = 0;
//RMS
int soma_quadrado = 0;
int rms = 0;
int pre_amostragem[5] = {0};
//Define variaveis referentes a leitura do arquivo e manipulacoes do mesmo.
FILE * fp;
FILE * LOG;
FILE * log_rms_final;
// Open the file and verify is NULL.
if((fp = fopen("generator.txt","r")) == NULL)
{ // Define o nome do csv para abrir
printf("Error! Can't open the file.\n");
exit(1);
}
// store rms values
LOG = fopen("RMSValues.csv", "a");
// store the 50ms after and before a event.
log_rms_final = fopen("Log_RMS.csv","a");
int lines = 0;
while(!feof(fp))
{
fgets(str,1024,fp); //reads 1024 characters and store in str.
buffer[write] = atoi(str);
write = (write + 1) % buffer_size; // circular
counter_elements++; // sum
c = fgetc(fp);
if(c == '\n')
{
lines++;
}
printf("%d\n", lines);
//if buffer is full
if(counter_elements == buffer_size)
{
// window
read1 = read;
for(i = 0; i < window_size; i++)
{
//square and sum.
soma_quadrado += buffer[read1]*buffer[read1];
read1 = (read1 + 1) % buffer_size;
}
// RMS
rms = sqrt(soma_quadrado/window_size);
fprintf(LOG, "\n %d", rms); // store
if(rms > 1000)
{
printf("rms: %d\n",rms);
// store the 50ms befor a event and the window.
write1 = write;
for(j = 0 ; j < 5; j++)
{
write1 = (write1 + (buffer_size - 1)) % buffer_size;
pre_amostragem[j] = buffer[write1];
}
fprintf(log_rms_final,"%s","\n");
for(j = 4; j >= 0; j--)
{
fprintf(log_rms_final,"%d - pre \n",pre_amostragem[j]);
}
fprintf(log_rms_final,"%s","\n");
/*
for(j = 0; j < window_size; j++)
{
fprintf(log_rms_final,"%d - janela\n",buffer[read1]);
read1 = (read1 + 1) % buffer_size;
}
*/
fprintf(log_rms_final,"%s","\n");
//store the 50ms after a event.
/*
fseek(log_rms_final,save_line - 3,save_line);
for(j = 0; j < 5; j++){
fgets(str,1024,fp);
fprintf(log_rms_final,"%d - pós \n",atoi(str));
}
*/
}
soma_quadrado = 0;
rms = 0;
read = (read + 1) % buffer_size;
counter_elements = counter_elements - 2;
}
soma_quadrado = 0;
rms = 0;
}
fclose(fp);
fclose(LOG);
fclose(log_rms_final);
return 0;
}
some comments are in Portuguese but they aren't relevant for the understanding of the problem.
I am giving you an algorithm for the solution here.
Always record 50ms (or even 60ms) data in a circular buffer.
If you detect a start event,
Copy previous 50 ms from circular buffer to final buffer
Continue writing received data into final buffer at 50ms location.
If you detect an end event.
Continue writing into final buffer for 50 ms more.
Start writing into circular buffer again.
If you have multiple events, you need to have multiple final buffers and can repeat the process.
As mentioned below in the comments, this solution will work for a window size > 50 ms also. You need to select the size of the final buffer accordingly.

Need some suggestions on how to print a histogram more neatly

I'm writing a program that will read input and then give back a histogram of the character count from K & R - Ex. 1.13
Any suggestions on how I can improve my code? Does it matter whether or not if I test for status in condition or out first? I have noticed in my examples people test to see if c is a blank or tab first.
I think I need to revisit my histogram. It doesn't really scale the results. It just draws a hyphen based on the length.
Revised to make a little bit more readable I think.
// Print a histogram of the length of words in it's input.
#include <stdio.h>
#define IN 1
#define OUT 2
#define MAX 99
int main(){
int c; // the character
int countOfLetters = 0;
int insideWord = OUT;
int frequencyOfLengths[MAX];
int longestWordCount = 0;
int i, j; // Counters
for (i = 0; i < MAX; i++){
frequencyOfLengths[i] = 0;
}
while ((c = getchar()) != EOF){
if (c == ' ' || c == '\n' || c == '\t'){
if (insideWord == IN){
if (countOfLetters > MAX){
return 1;
}
++frequencyOfLengths[countOfLetters];
if (countOfLetters >= longestWordCount) longestWordCount = countOfLetters;
}
countOfLetters = 0;
}
else {
countOfLetters++;
insideWord = IN;
}
}
for (i = 1; i <= longestWordCount; i++){
printf("%3i : %3i ", i, frequencyOfLengths[i]);
for (j = 0; j < frequencyOfLengths[i]; j++){
printf("*");
}
printf("\n");
}
return 0;
}
Definitely scale results, check out my Character Histogram that does a horizontal scaling histogram.
Also, you could benefit a y-axis label. It's hard to tell which bar is for which kind of word length. I have no idea which bar is for what word length.
I added this code right before you display the histogram, it basically halves every value, which does throw off your bar number labels. You can figure it out!
// Iterates and tells us the most frequent word length
int mostFrequent = 0;
for (i = 1; i < MAXWORD; i++)
if (charCount[i] > mostFrequent)
mostFrequent = charCount[i];
// If the bar will be too big, cut every value in half
while (mostFrequent > 60) {
for (i = 1; i < MAXWORD; i++)
if (charCount[i] > 0) {
charCount[i] /= 2;
charCount[i] |= 1;
}
// Check again to find the most frequent word length category
mostFrequent = 0;
for (i = 1; i < MAXWORD; i++)
if (charCount[i] > mostFrequent)
mostFrequent = charCount[i];
}
Honestly the bars are hard to read, maybe just use a single row of characters such as █ !
Great book so far, we're practically reading it together and are on the same page!
Cheers

Arduino - Reading Serial Data while performing for loop

I just got my arduino the other day. I am currently making a program in java to control my arduino using serial communication. So far, I only have the program turn it on or off. But I ran into an issue. I have my arduino fade two rgb leds, looping through every color. I run into my issue here. When I press the button to turn it off(java program), it doesn't turn off until its ran through every color(complete the for loops). I want it to instantly shut off. Is there any way I can read serial data in the for loops, or is there any possible way I can turn it off instantly, not having to wait for the for loops to complete? Here is the code:
const int redPins[] = {11,6};
const int greenPins[] = {10,5};
const int bluePins[] = {9, 3};
const int pinCountPerColor = 2;
const int sensorPin = 0;
int lightLevel;
int val = 0;
boolean isOn;
void setup() {
Serial.begin(9600);
setColourRgb(0,0,0);
isOn = true;
}
void loop() {
if(isOn) {
unsigned int rgbColour[3];
lightLevel = analogRead(sensorPin);
if(lightLevel >= 400) {
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = decColour == 2 ? 0 : decColour + 1;
for(int i = 0; i < 255; i += 1) {
lightLevel = analogRead(sensorPin);
if(lightLevel <= 400) {
setColourRgb(255, 255, 255);
} else {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(5);
}
}
}
} else {
setColourRgb(255, 255, 255);
}
}
}
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
for(int r = 0; r < pinCountPerColor; r++) {
analogWrite(redPins[r], red);
}
for(int g = 0; g < pinCountPerColor; g++) {
analogWrite(greenPins[g], green);
}
for(int b = 0; b < pinCountPerColor; b++) {
analogWrite(bluePins[b], blue);
}
}
void serialEvent()
{
while (Serial.available())
{
val = Serial.parseInt();
if(val == 1)
{
isOn = true;
//do nothing
}
else if(val == 0)
{
setColourRgb(255, 255, 255);
isOn = false;
}
}
Serial.println("Succesfully received.");
}
It is best to create a state machine for the colors, that does not have any blocking for loops, to dwell in. Especially loops with delays. So that each cycle through the loop changes the color and polls the Serial.
This is the art to writing real time code, or simply non-blocking code.
Note it is possible to create a timer interrupt to better schedule RGB updates. See Answer about Timer Interrupts to write precision updates.
#mpflaga answered your question correctly.
I would add that if you projects get bigger than just two leds fading, you might want to use something more reliable than a hand-made state machine.
That is called a Real Time Operating System (RTOS) and it allows you to have different threads running at different frequencies.
I personally use ChibiOS/RT for my robot project. There is a port for Arduino you can download here, it's very well documented and I'd say pretty easy to use once you get the basics. The nice thing to do is to add a higher level layer to manage the threads.
Here is a page with describing it and other solutions : Arduino rtoslibs
And here are some tutorials on real time and ChibiOS for Arduino:
It’s time for real-time
Blinking in real-time
ChibiOS for the Arduino IDE
Hope it helps! :)

Resources