PDA

View Full Version : convert fprint() to writing text in text area



Darktyr
17th August 2012, 04:37
premise:

I am using an FTDI C232H cable to communicate via I2C protocol to a MAX31785 fan controller. I have a basic script setup
that can communicate with the fan controller and do other meaningful stuff. I want to convert that into a GUI that just has a
column of buttons on the left side that perform various functions, program, read temp, read fan rpm, and then write the
output and stuff to a text area.

Problem/Question:

I am not sure how to go about this using Qt. I am sure there are at least a dozen ways to perform this task but I am not sure
which one would work the best and make the most sense. Basically I want to transform all the fprints() to a write to the text area.

here is the script that I currently have:



#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include "Headers/ftd2xx.h"
#include "Headers/libMPSSE_i2c.h"

using namespace std;



/************************************************** ****************************/
/* Function Definitions */
/************************************************** ****************************/
/* Helper macros */


void Check_Status(unsigned long exp)
{
if(exp!=FT_OK)
{
printf("\n\n\n%s: %d : %s(): \n status(0x%x)=\n",__FILE__, __LINE__, __FUNCTION__,exp);
getchar();
exit(1);
}
else
{
;
}
}

FT_STATUS status;
FT_HANDLE Handle;


//int _tmain(int argc, _TCHAR* argv[])
int main()
{
//FTDI Driver stuff
FT_DEVICE_LIST_INFO_NODE devList;
uint32 channels;
int channelOpen = 0;

// EEPROM reading variables and structures
char Manufacturer[64];
char ManufacturerID[64];
char Description[64];
char SerialNumber[64];

FT_EEPROM_HEADER ft_eeprom_header;
ft_eeprom_header.deviceType = FT_DEVICE_232H;

FT_EEPROM_232H ft_eeprom_232h;
ft_eeprom_232h.common = ft_eeprom_header;
ft_eeprom_232h.common.deviceType = FT_DEVICE_232H;

// libMPSSE-related variables
ChannelConfig channelConf;
Init_libMPSSE();
//General variables
uint32 TrialAddress = 0x00;
int i;
int j;
uint8 i2cAddress = 0x00;
unsigned char i2cDataSend[1024] = {0}; // Initialize a data buffer and set to all Zeros
unsigned char i2cDataReceive[1024] = {0}; // Initialize a data buffer and set to all Zeros
uint32 i2cNumBytesToTx = 0; // Initialize a counter for the buffer and set to Zero
uint32 i2cNumBytesTxd = 0; // Initialize another counter for the number of bytes actually sent and set to Zero
uint32 i2cNumBytesToRx = 0; // Initialize a counter for the recieve buffer and set to Zero
uint32 i2cNumBytesRxd = 0; // Initialize another counter for the number of bytes actually recieved and set to Zero


channelConf.ClockRate = I2C_CLOCK_STANDARD_MODE;// 100,000 Hz
channelConf.LatencyTimer= 255;
channelConf.Options = I2C_DISABLE_3PHASE_CLOCKING;
printf("Press Enter to Start");
getchar();

status = I2C_GetNumChannels(&channels);
Check_Status(status);
printf("Number of available I2C channels = %d\n",channels);
//Get information on each available channel.
if(channels>0)
{
for(i=0;i<channels;i++)
{
status = I2C_GetChannelInfo(i, &devList);
Check_Status(status);
printf("Information on channel number %d:\n",i);
//print the dev info
printf(" Flags=0x%x\n", devList.Flags);
printf(" Type=0x%x\n", devList.Type);
printf(" ID=0x0%x\n", devList.ID);
printf(" LocId=0x%x\n", devList.LocId);
printf(" SerialNumber=%s\n", devList.SerialNumber);
printf(" Description=%s\n", devList.Description);
}
}
else
{
printf("No FTDI devices were found\n");
getchar();
return 0;
}


//
//I2C_OpenChannel
status = FT_Open(channelOpen,&Handle);
if (status == FT_OK)
{
printf("Channel %d has been successfully opened\n", channelOpen);
}
else
{
printf("Channel %d is unable to be opened\n", channelOpen);
printf("status = 0x%x\n");
getchar();
return 0;
}
printf("Reading the EEPROM...\n");
status = FT_EEPROM_Read(Handle, &ft_eeprom_232h, sizeof(ft_eeprom_232h), Manufacturer, ManufacturerID, Description, SerialNumber);
if (status == FT_OK)
{
printf(" VendorID = 0x%04x\n", ft_eeprom_232h.common.VendorId);
printf(" ProductID = 0x%04x\n", ft_eeprom_232h.common.ProductId);
printf(" Manufacturer = %s\n",Manufacturer);
printf(" Manufacturer ID = %s\n",ManufacturerID);
printf(" Description = %s \n", Description);
printf(" SerialNumber = %s \n", SerialNumber);
}
else
{
printf("Unable to read EEPROM\n");
}

printf("Initializing for I2C operation...\n");
status = I2C_InitChannel(Handle,&channelConf);
if (status == FT_OK)
{
printf("Channel %d has been successfully initialized.\n", channelOpen);
}
else
{
printf("Channel %d is unable to be initialized\n", channelOpen);
printf("status = 0x%x\n", status);
getchar();
I2C_CloseChannel(Handle);
return 0;
}
getchar();
printf("Searching for I2C devices... ");
j = 0;

//this section finds all MAX chips by sending out the write_protect command
//on every availavle 7 bit I2C address, not including address 0x00 because that is
//a broadcast address

i2cNumBytesToTx = 0;
i2cDataSend[i2cNumBytesToTx++] = 0x10; //Write_Protect Command
i2cDataSend[i2cNumBytesToTx++] = 0x00; //Disable write protect
for (i = 0x00; i < 128; i++)
{
TrialAddress = i;
status = I2C_DeviceWrite(Handle, TrialAddress, i2cNumBytesToTx, i2cDataSend, &i2cNumBytesTxd, 0x07);
if (status == FT_OK)
{

j++;
printf("\n I2C Address %X Responded\n",i);
}
else
{
;
}
}
if (j == 0)
{
printf("Could not find any I2C devices\n");
}
printf("\n\nThat is all for now, move along,\n nothing more to see here,\n move along to another program \n");
getchar();
Cleanup_libMPSSE();
}

ChrisW67
17th August 2012, 05:47
There are plenty of tutorials and examples and in the documentation to help build the basic application shell. The Qt equivalent to printf() is QString::arg() and you get and set the text of a QTextEdit or QPlainTextEdit with their text() and setText() functions respectively.

Darktyr
17th August 2012, 15:48
There are plenty of tutorials and examples and in the documentation to help build the basic application shell.
If I had found something, I probably would not of posted this.
In all of the examples I have seen, I have never seen QString::arg().

This is more of what I was talking about, or I should say it has the effect that I am looking for.



QString line = "Writing Text";
QString line2 = "Writing more";
ui->textEdit->append(line);
ui->textEdit->append(line2);
ui->textEdit->show();


Is there an example using the QString::arg()?

yeye_olive
17th August 2012, 16:43
I just had a very quick look and I saw that the Class Wizard example (http://qt-project.org/doc/qt-4.8/dialogs-classwizard.html) uses this. Anyway it is as simple as this:


myTextEdit->setText(QString::fromAscii("Hello, %1! Did you know that %2 + %3 = %4?").arg(QString::fromAscii("world"), 2, 2, 4));

ChrisW67
17th August 2012, 23:12
Is there an example using the QString::arg()?

The text of docs I gave you a link to gives you an example. Here: QString::arg()

Darktyr
20th August 2012, 21:58
Thank You ChrisW67 and yeye_olive.