Która metoda jest lepsza

krycha88
Posts: 5575
Joined: Fri Nov 16, 2018 7:25 am
Been thanked: 5 times

Post

Mam pytanie do bardziej doświadczonych :)

Napisałem sobie 3 metody które robią to samo ale inaczej. Który sposób jest lepszy a może macie inne pomysły?

https://onlinegdb.com/rk4AtzlX8

Dla tych co nie lubią powyższej stronki:

Code: Select all

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

const char* stringAddress = "282DF42105000094";

uint8_t* getDeviceAddress1()
{
    static uint8_t addrv[8] = { 0 };
    
    sscanf(stringAddress, "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx",
                 &addrv[0], &addrv[1], &addrv[2], &addrv[3],
                &addrv[4], &addrv[5], &addrv[6], &addrv[7]);
        
    return addrv;
}

void getDeviceAddress2(uint8_t *buf)
{
    uint8_t addrv[8] = { 0 };

    sscanf(stringAddress, "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx",
         &addrv[0], &addrv[1], &addrv[2], &addrv[3],
         &addrv[4], &addrv[5], &addrv[6], &addrv[7]);

    for (int i = 0; i < 8; i++) {
        buf[i] = (__typeof__(buf[0])) addrv[i];
    }
}

void getDeviceAddress3(uint8_t *buf)
{
    uint8_t lengthDestination = 8;
    uint8_t addrv[lengthDestination] = { 0 };

    sscanf(stringAddress, "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx",
         &addrv[0], &addrv[1], &addrv[2], &addrv[3],
         &addrv[4], &addrv[5], &addrv[6], &addrv[7]);
         
    memcpy(buf, addrv, lengthDestination);

    //memcpy(buf, addrv, lengthDestination - 1);
    //buf[lengthDestination - 1] = 0;
}

void print(uint8_t * buf) {
    char strAddr[64];
    sprintf(
            strAddr,
            "{0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}",
            buf[0],
            buf[1],
            buf[2],
            buf[3],
            buf[4],
            buf[5],
            buf[6],
            buf[7]);
            
    printf( "address %s\n", strAddr);
}
int main() {
    uint8_t *buf1 = getDeviceAddress1();
    
    uint8_t buf2[8];
    getDeviceAddress2(buf2);
    
    uint8_t buf3[8];
    getDeviceAddress3(buf3);
    
    print(buf1);
    print(buf2);
    print(buf3);
}
https://gui-generic-builder.supla.io/
User avatar
klew
Posts: 13908
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

A może:

Code: Select all

void getDeviceAddress3(uint8_t *buf)
{
    sscanf(stringAddress, "%2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx %2hhx",
         &buf[0], &buf[1], &buf[2], &buf[3],
         &buf[4], &buf[5], &buf[6], &buf[7]);
}
?

Pytanie tylko skąd bierzesz stringAddress i czy sprawdzasz, że zawiera to co powinien?
Najlepsze suple dla Twojego domu :mrgreen:
krycha88
Posts: 5575
Joined: Fri Nov 16, 2018 7:25 am
Been thanked: 5 times

Post

Jak już wiesz napisałem sobie prosty interfejs do przechowywania wartości w Jsonie. Jeszcze nie skończony ale już działa:
https://github.com/krycha88/Primary_GUI ... anager.cpp


chciałem się pozbyć tak naprawdę powielonych metod na przykładzie:
void SuplaConfigManager::getWifiSSID(char *buf)
oraz
String SuplaConfigManager::getWifiSSID()

metodę SuplaConfigManager::getWifiSSID(char *buf) wykorzystuję tylko przy starcie programu a SuplaConfigManager::getWifiSSID() w GUI do wyświetlania wartości.

I nie wiem w którą stronę pójść, wydaje mi się że drugi sposób jest bardziej praktyczny.


W przykładzie wyżej dałem przykład konwersji z char* do uint8_t bo ten przypadek wydawał mi się bardziej problematyczny, ponieważ potrzebuję char* aby wyświetlić adres czujnika w GUI oraz uint8_t aby przekazać do DeviceAddress.
https://gui-generic-builder.supla.io/
User avatar
klew
Posts: 13908
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

Stringa zawsze możesz potraktować metodą c_str aby dostać "null terminated C string"
O ile nie jest to używane regularnie podczas pracy programu, to powinno być ok. Ogólnie ludzie odradzają w Arduino używać String, bo potrafi pofragmentować pamięć, ale będzie to problematyczne tylko jeśli będzie to wykonywane wielokrotnie
Najlepsze suple dla Twojego domu :mrgreen:

Return to “Arduino IDE”