bloguetronica / mcp2210-qt

A C++ class that uses libusb to interface with MCP2210 devices. It can be used to control the several aspects, such as NVRAM or volatile configurations, GPIO directions and states, or to perform SPI transfers. Note that this is a class variant made specifically for Qt. If you wish to use the original, non-Qt variant, please refer to https://github.com/bloguetronica/mcp2210.
0 stars 0 forks source link

Command vectors should be constructed, and not assigned to #3

Closed samuelfmlourenco closed 1 year ago

samuelfmlourenco commented 1 year ago

Currently, the above vectors are having arrays assigned to them. One example of such can be found in lines 190 to 204:

// Configures volatile chip settings
quint8 MCP2210::configureChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr)
{
    QVector<quint8> command = {
        SET_CHIP_SETTINGS, 0x00, 0x00, 0x00,                                                             // Header
        settings.gp0,                                                                                    // GP0 pin configuration
        settings.gp1,                                                                                    // GP1 pin configuration
        settings.gp2,                                                                                    // GP2 pin configuration
        settings.gp3,                                                                                    // GP3 pin configuration
        settings.gp4,                                                                                    // GP4 pin configuration
        settings.gp5,                                                                                    // GP5 pin configuration
        settings.gp6,                                                                                    // GP6 pin configuration
        settings.gp7,                                                                                    // GP7 pin configuration
        settings.gp8,                                                                                    // GP8 pin configuration
        settings.gpout, 0x00,                                                                            // Default GPIO outputs (GPIO7 to GPIO0)
        settings.gpdir, 0x01,                                                                            // Default GPIO directions (GPIO7 to GPIO0)
        static_cast<quint8>(settings.rmwakeup << 4 | (0x07 & settings.intmode) << 1 | settings.nrelspi)  // Other chip settings
    };
    QVector<quint8> response = hidTransfer(command, errcnt, errstr);
    return response[1];
}

However, this is neither correct nor efficient. In fact, QVector provides a constructor based on arrays. Thus, the above code can be fixed by simply deleting the "=" sign, like so:

// Configures volatile chip settings
quint8 MCP2210::configureChipSettings(const ChipSettings &settings, int &errcnt, QString &errstr)
{
    QVector<quint8> command{
        SET_CHIP_SETTINGS, 0x00, 0x00, 0x00,                                                             // Header
        settings.gp0,                                                                                    // GP0 pin configuration
        settings.gp1,                                                                                    // GP1 pin configuration
        settings.gp2,                                                                                    // GP2 pin configuration
        settings.gp3,                                                                                    // GP3 pin configuration
        settings.gp4,                                                                                    // GP4 pin configuration
        settings.gp5,                                                                                    // GP5 pin configuration
        settings.gp6,                                                                                    // GP6 pin configuration
        settings.gp7,                                                                                    // GP7 pin configuration
        settings.gp8,                                                                                    // GP8 pin configuration
        settings.gpout, 0x00,                                                                            // Default GPIO outputs (GPIO7 to GPIO0)
        settings.gpdir, 0x01,                                                                            // Default GPIO directions (GPIO7 to GPIO0)
        static_cast<quint8>(settings.rmwakeup << 4 | (0x07 & settings.intmode) << 1 | settings.nrelspi)  // Other chip settings
    };
    QVector<quint8> response = hidTransfer(command, errcnt, errstr);
    return response[1];
}
samuelfmlourenco commented 1 year ago

Fixed in version 1.1.0.