[Message Prev][Message Next][Thread Prev][Thread Next][Message Index][Thread Index]

X10 C++ source-code for firecracker module. CM17A



I did this in VS6.  The cpp is in the first part.  The dsp follows to
create cm17a executable.

<<cm17a.cpp>>
#include <conio.h>
#include <stdio.h>
#include <windows.h>

#define CODE_HEADER 0x55AB
#define CODE_FOOTER 0xB5

#define CODE_A 0x6
#define CODE_B 0xE
#define CODE_C 0x2
#define CODE_D 0xA
#define CODE_E 0x1
#define CODE_F 0x9
#define CODE_G 0x5
#define CODE_H 0xD
#define CODE_I 0x7
#define CODE_J 0xF
#define CODE_K 0x3
#define CODE_L 0xB
#define CODE_M 0x0
#define CODE_N 0x8
#define CODE_O 0x4
#define CODE_P 0xC

#define CODE_01 0x0000
#define CODE_02 0x0800
#define CODE_03 0x1000
#define CODE_04 0x1800
#define CODE_05 0x0200
#define CODE_06 0x0A00
#define CODE_07 0x1200
#define CODE_08 0x1A00
#define CODE_09 0x0020
#define CODE_10 0x0820
#define CODE_11 0x1020
#define CODE_12 0x1820
#define CODE_13 0x0220
#define CODE_14 0x0A20
#define CODE_15 0x1220
#define CODE_16 0x1A20

#define CODE_ON  0x0000
#define CODE_OFF 0x0400

#define CODE_BRIGHT 0x1100
#define CODE_DIM 0x1900

DCB dcb;
HANDLE hCom;

void SendByte(BYTE bValue)
{
	for (BYTE b = 0; b < 8; b++) {
		// wait
		dcb.fDtrControl = DTR_CONTROL_ENABLE;
		dcb.fRtsControl = RTS_CONTROL_ENABLE;
		Sleep(1);
		SetCommState(hCom, &dcb);
		if (bValue & (1 << b)) {
			// '1'
			dcb.fDtrControl = DTR_CONTROL_DISABLE;
			dcb.fRtsControl = RTS_CONTROL_ENABLE;
		} else {
			// '0'
			dcb.fDtrControl = DTR_CONTROL_ENABLE;
			dcb.fRtsControl = RTS_CONTROL_DISABLE;
		}
		Sleep(1);
		SetCommState(hCom, &dcb);
		Sleep(1);
	}
}

void SendWord(WORD wValue)
{
	for (BYTE w = 0; w < 16; w++) {
		// wait
		dcb.fDtrControl = DTR_CONTROL_ENABLE;
		dcb.fRtsControl = RTS_CONTROL_ENABLE;
		Sleep(1);
		SetCommState(hCom, &dcb);
		if (wValue & (1 << w)) {
			// '1'
			dcb.fDtrControl = DTR_CONTROL_DISABLE;
			dcb.fRtsControl = RTS_CONTROL_ENABLE;
		} else {
			// '0'
			dcb.fDtrControl = DTR_CONTROL_ENABLE;
			dcb.fRtsControl = RTS_CONTROL_DISABLE;
		}
		Sleep(1);
		SetCommState(hCom, &dcb);
		Sleep(1);
	}
}

void X10Command(WORD wCommand)
{
	SendWord(CODE_HEADER);
	SendWord(wCommand);
	SendByte(CODE_FOOTER);
	Sleep(1000);
}

void main(int argc,char* argv[])
{
	printf("CM17A rsandoz@xxxxxxxxxxxxxxxxxxxxx\n");
	if ((argc != 5) && (argc != 4))
	{
		printf("Usage: cm17a comPort houseCode deviceCode [0|1]\n");
		printf("Usage: cm17a comPort houseCode [0|1] (for dim or bright)\n");
		printf("       comPort = COM1, COM2, COM3, COM4\n");
		printf("       houseCode = A, B ... P\n");
		printf("       deviceCode = 1, 2 ... 16\n");
		printf("       0 = off   1 = on\n");
		return;
	}

	char strPort[MAX_PATH];
	sprintf(strPort, "\\\\.\\%s", argv[1]);
	if ((hCom  = CreateFile( strPort, GENERIC_READ | GENERIC_WRITE,
			0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
		return;

	if (!GetCommState(hCom, &dcb)) return;
	dcb.fDtrControl			= DTR_CONTROL_ENABLE;
	dcb.fRtsControl			= RTS_CONTROL_ENABLE;
	if (!SetCommState(hCom, &dcb)) return;

	WORD wCommand = 0;
	char houseCode = toupper(argv[2][0]);
	if ((houseCode < 'A') || (houseCode > 'P'))
	{
		printf("Invalid house code\n");
		return;
	}
	BYTE houseCodeArray[] =
{CODE_A,CODE_B,CODE_C,CODE_D,CODE_E,CODE_F,CODE_G,CODE_H,CODE_I,CODE_J,CODE_K,CODE_L,CODE_M,CODE_N,CODE_O,CODE_P};
	wCommand |= houseCodeArray[houseCode - 'A'];

	if (argc == 4)
	{
		BYTE commandCode = atoi(argv[3]);
		if ((commandCode < 0) || (commandCode > 1))
		{
			printf("Invalid command code\n");
			return;
		}
		WORD commandCodeArray[] = {CODE_DIM,CODE_BRIGHT};
		wCommand |= commandCodeArray[commandCode];

		X10Command(wCommand);
		return;
	}

	BYTE deviceCode = atoi(argv[3]);
	if ((deviceCode < 1) || (deviceCode > 16))
	{
		printf("Invalid device code\n");
		return;
	}
	WORD deviceCodeArray[] =
{CODE_01,CODE_02,CODE_03,CODE_04,CODE_05,CODE_06,CODE_07,CODE_08,CODE_09,CODE_10,CODE_11,CODE_12,CODE_13,CODE_14,CODE_15,CODE_16};
	wCommand |= deviceCodeArray[deviceCode - 1];

	BYTE commandCode = atoi(argv[4]);
	if ((commandCode < 0) || (commandCode > 1))
	{
		printf("Invalid command code\n");
		return;
	}
	WORD commandCodeArray[] = {CODE_OFF,CODE_ON};
	wCommand |= commandCodeArray[commandCode];

	X10Command(wCommand);

	CloseHandle(hCom);

//	X10Command(CODE_A | CODE_09 | CODE_ON);
//	X10Command(CODE_B | CODE_01 | CODE_ON);
//	X10Command(CODE_A | CODE_09 | CODE_OFF);
//	X10Command(CODE_B | CODE_01 | CODE_OFF);
}

<<cm17a.dsp>>
# Microsoft Developer Studio Project File - Name="cm17a" - Package
Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **

# TARGTYPE "Win32 (x86) Console Application" 0x0103

CFG=cm17a - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using
NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "cm17a.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "cm17a.mak" CFG="cm17a - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "cm17a - Win32 Release" (based on "Win32 (x86) Console
Application")
!MESSAGE "cm17a - Win32 Debug" (based on "Win32 (x86) Console
Application")
!MESSAGE

# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe

!IF  "$(CFG)" == "cm17a - Win32 Release"

# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"
/D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D
"_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386

!ELSEIF  "$(CFG)" == "cm17a - Win32 Debug"

# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D
"_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D
"_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
/pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib
odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
/pdbtype:sept

!ENDIF

# Begin Target

# Name "cm17a - Win32 Release"
# Name "cm17a - Win32 Debug"
# Begin Group "Source Files"

# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File

SOURCE=.\cm17a.cpp
# End Source File
# End Group
# End Target
# End Project



comp.home.automation Main Index | comp.home.automation Thread Index | comp.home.automation Home | Archives Home