Arduino: call digitalWrite within a same naming class-member - arduino-c++

let me describe as following:
class foo{
public:
[...]
digitalWrite(uint8_t pin, uint8_t function){
digitalWrite(CS_PIN, LOW); // <<== CALLS foo::digitalWrite(...
delayMicroseconds(1);
SPI.beginTransaction(settings);
SPI.transfer16(this->m_txFrame);
SPI.endTransaction();
digitalWrite(CS_PIN, HIGH); // <<== CALLS foo::digitalWrite(...
}
[...]
};`
For that Arduino has no encapsilation for digitalWrite, the beaviour ist as expected. Is there another solution except renaming foo:digitalWrite(...) I do not know yet?
Thanks in advance!
Joerg

Related

Adafruit Oled library causing my esp8266 to crash

today I wanted to try to use a oled display with esp8266 using arduino language. Before I always used micropython to use an oled display. I had written a long code but it kept showing error. Then I decided to comment out the code that I used for the oled display. Then the error disappeared. Can someone please help me solve that problem?
My code :
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
//netwok
const char* ssid = "Iffaiman";
const char* password = "iffaiman313";
//oled var
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
display.println("Hello, world!");
display.display();
}
void loop() {
}
The error message:
xception (28): epc1=0x40201e0a epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
I tried to remove any optional code but it didn't help.
Your code is crashing because you're not calling the begin() method on the display object. That means it's not initialized, so its behavior is undefined.
You need to call the begin() method before doing anything else with the display.
Serial.println("Connected to WiFi");
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("Can't find display");
while(1)
yield();
}
display.println("Hello, world!");
display.display();
SCREEN_ADDRESS should be the I2C address of the SSD1306, either 0x3C or 0x3D.
Adafruit publishes extensive examples and tutorials on how to use their products and libraries. These are a very good place to start when you're having problems with the hardware or software.

STML4 USB virtual com port

I have the nucleo board (nucleo-L4R5ZI) and want to write a code to be able to send data from a uC to a PC via the USB. I followed some tutorials, used STM32CubeMx, other solutions found across the Internet, but anyways I failed. I can open the vcp on the PC side (using Hterm, TeraTerm and Realterm), but cannot get any data.
I use Eclipse and the buildin debugger, which I flashed to JLink.
The main loop:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USB_DEVICE_Init();
HAL_Delay(10000);
uint8_t HiMsg[] = "0123456789987654321001234567899876543210\r\n";
while (1)
{
if( CDC_Transmit_FS(HiMsg, 20) == USBD_OK )
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_7); // blue LED
}
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14); // red LED
HAL_Delay(1000);
}
}
After executing this function, the blue LED lights only once and never changes its state (does not blink). It means that the CDC_Transmit_FS(...) returns USBD_OK only once, and next calls give USBD_Busy.
The MX_USB_DEVICE_Init() looks as follow:
void MX_USB_DEVICE_Init(void)
{
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);
USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
USBD_Start(&hUsbDeviceFS);
USBD_CDC_Init (&hUsbDeviceFS, &USBD_CDC); // I need to init it somewhere so I think here is a good place
}
The CDC_Transmit_FS looks like that:
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
/* USER CODE BEGIN 7 */
CDC_Init_FS();
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
if (hcdc->TxState != 0){
return USBD_BUSY;
}
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
CDC_Init_FS();
/* USER CODE END 7 */
return result;
}
Does anyone know how to make it running? What do I miss?
Best,
This part look suspicious:
result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
CDC_Init_FS();
The call to CDC_Init_FS() probably kills the packet before it had a chance to be sent to the host.
SO... I found the solution!
I can confirm that the code above works (just remove the CDC_Init_FS)!
Acctully, it was a driver problem. for windows 10 you also need to install it despide what's written in the reference

Arduino Bluetooth connection gives me uploading error

İt's a simple basic thing in which i have my arduino connected to a HC-06 bluetooth. The point is to control switch on/off led pin with my phone. Here's the code :
int ledPin = 13;
int state = 0;
int flag = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
if(Serial.available() >0) {
state = Serial.read();
flag = 0;
}
if(state == '0') {
digitalWrite(ledPin, LOW);
Serial.println("LED: off");
flag = 1;
}
}
else if (state == '1') {
Serial.println("LED: on");
flag = 1;
}
}
I don't think it might have much relevance. Whenever i try to upload the code it gives me the following error:
avrdude stk500_recv() programmer is not responding
avrdude stk500_getsync() attempt # of 10 not in sync resp=0x00
Any idea why and how i might solve it. Thnx in advance!Douglas
I guess that there is a conflict between the USB/Serial and HC06/Serial.
You can solve this issue by using the Software Serial library and connect the HC06 to other pins. You can find an example here on how to use the library.
Unplug the rx and tx pins and keep in power and ground while uploading. When these pins are connected it interferes with the program's ability to upload. If this is in fact the problem then once the program is uploaded you can reattach rx and tx. Now you should be able to properly pair to your device.
Please disconnect the Tx and RX pin from Arduino before uploading and connect it after uploading the code. Otherwise, it will show an error

DetourAttach success but no functions hooked :(

Good morning !
I have recently read articles quite interesting about hooking functions, I have followed one or two tutorials but it never seems to work, I am using Detoured and here is the full code which seems to me perfectly normal :(
#include <stdio.h>
#include <windows.h>
#include "stdafx.h"
#include "detours.h"
#pragma comment(lib, "detours.lib")
int(__stdcall* realFunc)(int) = (int(__stdcall*)(int))(0x004157B0);
void hookedFunc(int num)
{
printf("Test : %d\n", num + 100);
}
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
break;
case DLL_THREAD_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
DetourTransactionCommit();
hookedFunc(100);
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
DetourDetach((PVOID*)0x004157B0, hookedFunc);
break;
}
return TRUE;
}
When using RemoteDLL and a simple console application as dummy to hook the function, all steps are completed successfully (running as administrator), the memory address to the function I want to be hooked matches, however the code line "printf("Test : %d\n", num + 100);" is not executed, the result does not appears at screen...
If anyone would have an idea about what's going on I would be really happy to hear it !
Thanks in advance !
First, hookedFunc must have the same signature: int __stdcall hookedFunc(int x).
I suppose the following effect of your code: hookedFunc is called each time somebody calls the function at address 0x004157B0. Is it what you expect?
For testing, you call this address. Let me change the code a little to clarify:
extern int __stdcall FunctionIWantToHook(int);
int(__stdcall* realFunc)(int) = FunctionIWantToHook;
...
DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
FunctionIWantToHook(100); // hookedFunc will be called here

Pushbutton variable change not working

I am a few days into programming with Arduino and I've run into an annoying problem. My circuit contains three LEDs connected to pins 2,3,4 and a push button connected to pin 8. What I want to do is alternate the lit LED by pressing the button. I'm using a variable to count which LED is lit at a certain point and resets when it reaches the value 4. The hardware part works fine, because I've tested it separately (automated alternation of the LEDs and the button example program in the Arduino IDE), so there has to be something with my code. What is it exactly?
void setup(){
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(8, INPUT);
}
void loop(){
int buton= digitalRead(8);
int led = 1;
if(led == 1){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
if(led == 2){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}
if(led == 3){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}
if(buton == HIGH){
led++;
if(led == 4) led = 1;
}
}
I know for a fact that it doesn't enter the last if (the one for the button input value) because I've placed a Serial.println() to see both the value of variable led and a constant string and it didn't show.
Thank you in advance!
Smilledge was right in the comments, the led variable was reset to 1 at every iteration of the loop. I wasn't aware you could have variable declarations outside the two functions (setup and loop), so I just made the variable global.