Thursday, January 22, 2009

If you want to add time log in the code ...

PBSearchingTimeMS = \
(unsigned long)((PBSearchingTimeTickEnd - PBSearchingTimeTickStart) / 32.768);

* change clock (frequency) to time: 32.768 (Crystal Clock)

Custom Token Macro in Source Insight

We suppose,
When you use the "Source Insight" as your source code editor,

Source code is as followed

 #define PACKED __packed
 typedef PACKED struct {
  int age;
  int tall;
 } MyStyleType;

Source Insight cannot parsing the variable "MyStyleType".

How can you fix it?
Source Insight support "*.tom" file.
In this case, we can change or add "c.tom"
c.tom file in the MyDocument\Source Insight\C.tom

We can add some key words,
_packed
packed
PACKED

That's all.

Sunday, January 18, 2009

How to define "printf(...)" to "DEBUG(...)" in C/C++

When you make a program or programs, you need to check the program running condition.
In this reason, you want to use the debug message into the program.
In the debugging time you use the print put the logging messages and in the release time you don't want to use the logging messages.
When you use the macro "#define" how can you convert printf(char* format, ...) function to macro function?

As following,

// Multiple arguments
#define MSG_ERR(format, ...)    TRACE(ERROR, format, __VA_ARGS__)
#define MSG_WARN(format, ...)   TRACE(WARNING, format, __VA_ARGS__)
#define MSG_HIGH(format, ...)   TRACE(HIGH, format, __VA_ARGS__)
#define MSG_MID(format, ...)    TRACE(MIDDLE, format, __VA_ARGS__)
#define MSG_LOW(format, ...)    TRACE(LOW, format, __VA_ARGS__)
// Single argument
#define MSG_ERR(format)        TRACE(ERROR, format)
#define MSG_WARN(format)       TRACE(WARNING, format)
#define MSG_HIGH(format)       TRACE(HIGH, format)
#define MSG_MID(format)        TRACE(MIDDLE, format)
#define MSG_LOW(format)        TRACE(LOW, format)

And get more things, please refer to
Support for variadic macros was introduced in Visual C++ 2005.

Friday, January 16, 2009

About Bit Field in C/C++

In the C/C++, 
usually we use the "Bit Field" to optimize code or communicate with other process.
What is the Bit Filed?

I have attached "Example Code".

Example code

// Test Code, Bit Field
// ----------------------------------
{
 #define u8 unsigned char

 #define FILL_STRUCT1 u8:0; u8:8; u8:0;
 #define FILL_STRUCT2 u8:0; u8:8; u8:8; u8:0;
 #define FILL_STRUCT3 u8:0; u8:8; u8:8; u8:8; u8:0;

 typedef struct tag_SIMCommand {
 u8 Class;
 u8 Instruction;
 u8 P1;
 u8 P2;
 u8 P3;
 FILL_STRUCT3 // Bit Field
 u8 CData[8];
 } SIMCommandType;

 SIMCommandType SIMCommand;

 memset(&SIMCommand, 0xFF, sizeof(SIMCommandType));
 memset(&SIMCommand, 0x00, sizeof(SIMCommandType));

 // SELECT
 SIMCommand.Class = 0xA0;
 SIMCommand.Instruction = 0xA4;
 SIMCommand.P1 = 0x00;
 SIMCommand.P2 = 0x00;
 SIMCommand.P3 = 0x02;
 memset(SIMCommand.CData, 0xFF, sizeof(u8)*8);
}
// ----------------------------------

// Real Memory, Dump 1
// ----------------------------------
0012F3B0  A0 A4 00 00  
0012F3B4  02 00 00 00  
0012F3B8  FF FF FF FF  
0012F3BC  FF FF FF FF  
// ----------------------------------

// Test Code, Bit Field
// ----------------------------------
{
 #define u8 unsigned char

 #define FILL_STRUCT1 u8:0; u8:8; u8:0;
 #define FILL_STRUCT2 u8:0; u8:8; u8:8; u8:0;
 #define FILL_STRUCT3 u8:0; u8:8; u8:8; u8:8; u8:0;

 typedef struct tag_SIMCommand {
 u8 Class:4;
 u8 Instruction:2;
 u8 P1:1;
 FILL_STRUCT1
 u8 P2:4;
 FILL_STRUCT2
 u8 P3:1;
 FILL_STRUCT3 // Bit Field
 u8 CData[8];
 } SIMCommandType;

 SIMCommandType SIMCommand;

 memset(&SIMCommand, 0xFF, sizeof(SIMCommandType));
 memset(&SIMCommand, 0x00, sizeof(SIMCommandType));

 // SELECT
 SIMCommand.Class = 0x0F;
 SIMCommand.Instruction = 0x03;
 SIMCommand.P1 = 0x01;
 SIMCommand.P2 = 0x0F;
 SIMCommand.P3 = 0x01;
 memset(SIMCommand.CData, 0xFF, sizeof(u8)*8);
}
// ----------------------------------

// Real Memory, Dump 2
// ----------------------------------
0012F39C  7F 00 0F 00  
0012F3A0  00 01 00 00  
0012F3A4  00 FF FF FF  
0012F3A8  FF FF FF FF  
0012F3AC  FF CC CC CC  
// ----------------------------------

If you have any question? Please let me know :-)

Sunday, January 4, 2009

Soap opera, TV Drama?

Nowadays I am watching TV series
Grey's Anatomy: Season 1, 2, 3, 4, and 5
 now, I am watching season 2.

Previous time, I watched
LOST: Season 1, 2, 3, 4, and 5
 I already watched season 1, 2, and 3.
HEROES: Season 1, 2, and 3
 I already watched season 1, 2.
and NOW, I'm watching Gray's Anatomy.

Previous time, I have watched 24, Numb3rs, ... 

When I was school student, I have watched many Americana's TV series,
for example MacGyver, Air Wolf, The Wonder Years and so on.

In the future, can I watch these TV series without caption?



Thursday, January 1, 2009

Do you have a dream or a plan?

In the computer algorithm, there is "Divide and Conquer" i.e. when you solve the problem divide the problems and solve each of them. 

Today is the first working day of this year 2009.

Now, I am making this year's decision.

1. Increase family member.
2. Read English written books more than 10.
3. Join the on line lessons more than 10.
4. Upload blog more than 10 every month.

And end of this year, I will check them.

Let's start!

January 2nd, 2009.