My keyboard library for an esp32s2 is not working as it should - keyboard

The problem:
The keyboard script should type the text within a second. But it's typing slow, like a
human....
ps The program is printing text every 20sec
The board:
esp32s2
The code:
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;
void setup() {
// Set up Serial Monitor
Serial.begin(9600);
Keyboard.begin();
USB.begin();
}
void loop() {
delay(20000);
Keyboard.println("kajdgjskajgklhjagkljakjgkl");
}

Not a direct answer to your question, but an alternative: if you don't have to use "USBHIDKeyboard.h" specifically, consider "hidkeyboard.h" of ESP32TinyUSB library. It allows typing text very fast.
#include "hidkeyboard.h"
HIDkeyboard kbd;
void setup()
{
Serial.begin(115200);
kbd.begin();
//kbd.sendChar(66); // send ASCII char
delay (3000);
kbd.sendString("This is test \n");
}
void loop() {
//kbd.sendChar(65); // send ASCII char
kbd.sendString("Lorem ipsum dolor sit amet. \n");
delay(3000);
}
Supproted typing functions of this library:
bool sendKey(uint8_t _keycode, uint8_t modifier = 0);
bool sendChar(uint8_t _keycode);
bool sendPress(uint8_t _keycode, uint8_t modifier = 0);
bool sendRelease();
bool sendString(const char* text);
bool sendString(String text);

Related

Arduino WiFi functions cause problems in setup() and loop() functions

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
const char *ssid = "name";
const char *password = "pass";
AsyncWebServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("setup0");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("setup1");
}
void loop() {
delay(2000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("loop .... ");
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
}
I am running the above code using the platformio ide in vscode. Whenever I upload the code using the wifi functions and then use the serial monitor to see the output, it prints nothing. If I exclude the wifi functions and upload the rest of the code then the serial monitor shows the correct println outputs. I cannot figure out if it has something to do with the network and what would I have to change or something else. Any ideas on why this occurs would be much appreciated, thanks.

C++ console application async accepting user input

I recently stumbled upon this
link and I just tried it, but it's not working as I expect.
With this code:
#include <atomic>
#include <thread>
#include <iostream>
void ReadCin(std::atomic<bool>& run)
{
std::string buffer;
while (run.load())
{
std::cin >> buffer;
if (buffer == "q")
{
run.store(false);
}
}
}
int main()
{
std::atomic<bool> run(true);
std::thread cinThread(ReadCin, std::ref(run));
while (run.load())
{
// some lengthy operation
}
run.store(false);
cinThread.join();
return 0;
}
In the main While loop, I have an object of a class that is doing some lengthy operation, one which I'm trying to stop with the letter "q" coming from the user. When I type "q", i see the "run.store(false);" hit in the ReadCin method, but this doesn't break me off from the main while loop. What am I doing wrong?

SIM800L string concatenation

I have this code from a website that I'm using as a guide to send SMS message from a SIM800L connected to my Arduino Mega.
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l; //declare the library
char* text;
char* number;
bool error;
void setup(){
Sim800l.begin();
text="Testing Sms";
number="+542926556644";
error=Sim800l.sendSms(number,text);
// OR
//error=Sim800l.sendSms("+540111111111","the text go here");
}
void loop(){
//do nothing
}
I added some bits of code in the middle so that it will receive a string input from a user in my Python GUI via serial connection.
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l; //declare the library
char* text;
char* number;
bool error;
String data;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0)
{
data = Serial.readString();
Serial.print(data);
sendmess();
}
}
void sendmess()
{
Sim800l.begin();
text="Power Outage occured in area of account #: ";
number="+639164384650";
error=Sim800l.sendSms(number,text);
// OR
//error=Sim800l.sendSms("+540111111111","the text go here");
}
I am trying to concatenate the data from my serial.readString() to the end of the text. Conventional methods like the + and %s don't work.
In Arduino IDE I'm getting this error:
error: cannot convert ‘StringSumHelper’ to ‘char*’ in assignment
If I'm correct char* is a pointer that points to an address. Is there anyway to add the string from the serial monitor to the text?
You have to convert the Arduino String object to a standard C string. You can do this by using the c_str() method of the String class. It will return a char* pointer.
Now you can concatenate the two string with the strncat function from C library, string.h and also using strncpy as well.
#include <string.h>
char message[160]; // max size of an SMS
char* text = "Power Outage occured in area of account #: ";
String data;
/*
* populate <String data> with data from serial port
*/
/* Copy <text> to message buffer */
strncpy(message, text, strlen(text));
/* Calculating remaining space in the message buffer */
int num = sizeof(message) - strlen(message) - 1;
/* Concatenate the data from serial port */
strncat(message, data.c_str(), num);
/* ... */
error=Sim800l.sendSms(number, message);
Note it will simply chop off the remaining data if there is not enough space in the buffer.

linux: alarm function doesn't work sometimes?

My code is as follows,The first time ,alarm works very well and handler2() function can work. however, the alarm doesn't work after it implement "doMain()" in the "handler2()".
I mean after the second time print ""In main Pleasae input: \n"", handler2() doesn't wrok anymore.
I don't know why? My code as follows:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#define MAX_LEN_COMM 64
jmp_buf jumper;
int stop =0; //o is not stop ,otherwise is stop;
void hanlder2();
void doMain();
void handler2()
{
int len_command = 0;
char character;
char commandStr[60];
printf("******************************\n");
printf("In Alarm Pleasae input: \n");
while((character=getchar())!='\n')
{
commandStr[len_command]=character;
len_command++;
}
commandStr[len_command]='\0';
printf("In Alarm input is %s\n",commandStr);
if (strcmp(commandStr,"N")==0||strcmp(commandStr,"n")==0){
printf("In Alarm You put no, we will stop alarm \n");
stop=1;
longjmp(jumper, 2);
}
else if(strcmp(commandStr,"Y")==0||strcmp(commandStr,"y")==0){
printf("In Alarm You put yes, we will continue alarm \n");
signal(SIGALRM, handler2);
alarm(5);
doMain();
}
}
void doMain(){
while(1){
setjmp(jumper);
if(stop==0){
signal(SIGALRM, handler2);
printf("return time %d\n",alarm(5));
}
int len_command = 0;
char character;
char commandStr[60];
printf("In main Pleasae input: \n");
while((character=getchar())!='\n')
{
commandStr[len_command]=character;
len_command++;
}
commandStr[len_command]='\0';
printf("In main input is %s\n",commandStr);
if (strcmp(commandStr,"N")==0||strcmp(commandStr,"n")==0){
printf("In main You put no\n");
}
else if(strcmp(commandStr,"Y")==0||strcmp(commandStr,"y")==0){
printf("In main You put yes\n");
}
}
}
void main()
{
doMain();
}
What you are doing is very wrong.
First, the signature of the handler should be void handler(int sig).
Second, there are very few functions that are safe to use within a handler so you should try to get out of a handler as quickly as possible and definitely not doing console i/o. You are using several unsafe library functions.
Lastly a signal handler is a function. It runs and returns to where your program was interrupted by the signal. During the time a handler runs signals of the same type are not deliverd. By calling doMain() from the handler - which is crazy - the handler never ends. Because it doesn't end you won't see any more alarm signals.

How to use signals & slots in Qt for inter-threading communication

I want to make an application where the user will hit a QPushButton and this will trigger a secondary thread which will add some text to a QListWidget in the main window. But for a reason that I cannot figure out ,although the signal from the thread to the main window is emitted it never reaches the destination. Probably because the connection fails. But why this happens here is my code(my application is compiled using Visual Studio 2010):
mythread.h
#ifndef MY_THREAD_H
#define MY_THREAD_H
#include <QThread>
#include <QString>
class mythread:public QThread
{
Q_OBJECT
public:
void setName(QString& name);
signals:
void sendMsg(QString& msg);
protected:
void run();
private:
QString m_name;
QString msg;
};
#endif
mythread.cpp
#include "mythread.h"
void mythread::setName(QString& name)
{
m_name=name;
}
void mythread::run()
{
msg="Hello "+m_name;
emit sendMsg(msg);
}
mydialog.h:
#ifndef MY_DIALOG_H
#define MY_DIALOG_H
#include <QtGui>
#include "mythread.h"
class mydialog:public QDialog
{
Q_OBJECT
public:
mydialog();
public slots:
void receiveMsg(QString& msg);
void fillList();
private:
QListWidget list1;
QPushButton btn1;
QGridLayout layout;
mythread thread;
};
#endif
mydialog.cpp:
#include "mydialog.h"
mydialog::mydialog()
{
layout.addWidget(&list1,0,0);
btn1.setText("Find");
layout.addWidget(&btn1,0,1);
setLayout(&layout);
QString myname="leonardo";
thread.setName(myname);
connect(&btn1,SIGNAL(clicked()),this,SLOT(fillList()));
connect(&thread,SIGNAL(sendMsg(QString&)),this,SLOT(receiveMsg(Qstring&)));
}
void mydialog::fillList()
{
thread.start();
}
void mydialog::receiveMsg(QString& msg)
{
list1.addItem(msg);
}
find.cpp:
#include <QApplication>
#include "mydialog.h"
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
mydialog window;
window.setWindowTitle("Find");
window.show();
return app.exec();
}
find.pro:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += mydialog.h mythread.h
SOURCES += find.cpp mydialog.cpp mythread.cpp
Two things:
In your second connect call, Qstring must be changed to QString
Qt cannot deliver QString& accross threads by default. There's two ways to fix this:
Change your Signals and Slots and the connect to use QString rather than QString&
Use qRegisterMetaType in order to make QString& usable.
I still recommend reading
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
and Kari's comment
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong#commento-comment-name-a6fad43dec11ebe375cde77a9ee3c4331eb0c5f0bcac478ecbe032673e8ebc82
when working with threads, though.
First of all use const qualifier for arguments if you're not planning to modify it. After fixing typo in connection SLOT(receiveMsg(Qstring&)) and changing signals and slots signature to const references everything works fine

Resources