home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-05-04 | 66.0 KB | 2,158 lines |
- /* robots.c */
- /* machine dependent user interface and system initialization */
- /* by Paul D. Fernhout */
- /* for Macintosh and Lightspeed C */
- /* uses simple tools (c) 1986 Erik Kilk */
-
- #include "simple.h"
- #include .Quickdraw.h/
-
- about() {
- message("Robots - by Paul D. Fernhout\rProgrammed with the aid of\rSimpleTools (c) Erik Kilk 1986");
- }
-
- in_content(x, y)
- int x, y;
- {
- /* MoveTo(x,y);
- LineTo(x,y); */
- }
-
- extern void *psWorld;
- update() {
- home();
- World_Draw(psWorld);
- }
-
- clear_window() {
- home();
- }
-
- extern int iRunning;
-
- stoprunning() {
- iRunning = 0;
- }
-
- void simulation1();
- void simulation2();
-
- setup() {
- menu(applestring, "About Robots...", about);
- menu(applestring, "About Robots...", itemenable);
- window("Robot Demo",20,50,490,325, 0L, 0L, update, in_content);
- menu("File", "Clear Window", clear_window);
- menu("File", "Simulation 1", simulation1);
- menu("File", "Simulation 2", simulation2);
- menu("File", "Stop", stoprunning);
- simplequits();
- }
-
- main() {
- simpletools("About Robots...");
- setup();
- GraphicsInitialize();
- runsimpletools();
- }
-
- /* Graphics.c */
- /* machine dependent graphics routines */
- /* for Macintosh under Lightspeed C */
-
- #include .Quickdraw.h/
-
- #define PART_SIZE 20
- #define TEXT_SIZE 12
- #define ROBOT_SIZE 50
- #define GRIPPER_SIZE 10
- #define AGV_SIZE 40
- #define CNC_SIZE 70
-
- GraphicsInitialize()
- {
- TextSize(9);
- TextMode(patXor); /* draw everything in XOR mode */
- PenMode(patXor);
- }
-
- GraphicsClear()
- {
- home(); /* simple tools clear screen */
- }
-
- GraphicsDrawPart(iX, iY, szLabel)
- int iX, iY;
- char *szLabel;
- {
- DrawRectangle(iX - PART_SIZE/2,iY-PART_SIZE/2,PART_SIZE,PART_SIZE);
- MoveTo(iX-PART_SIZE/2,iY+PART_SIZE/2+TEXT_SIZE);
- DrawTextString(szLabel);
- }
-
- GraphicsErasePart(iX, iY, szLabel)
- int iX, iY;
- char *szLabel;
- { /* part is drawn XOR to erase */
- GraphicsDrawPart(iX, iY, szLabel);
- }
-
- GraphicsDrawRobot(iX, iY, iGripperX, iGripperY, szLabel)
- int iX, iY;
- int iGripperX, iGripperY;
- char *szLabel;
- {
- DrawRectangle(iX - ROBOT_SIZE/2,iY-ROBOT_SIZE/2,ROBOT_SIZE,ROBOT_SIZE);
- GraphicsDrawRobotArm(iX, iY, iGripperX, iGripperY);
- MoveTo(iX-ROBOT_SIZE/2,iY+ROBOT_SIZE/2+TEXT_SIZE);
- DrawTextString(szLabel);
- }
-
-
- GraphicsEraseRobot(iX, iY, iGripperX, iGripperY, szLabel)
- int iX, iY;
- int iGripperX, iGripperY;
- char *szLabel;
- { /* draw robot XOR to erase */
- GraphicsDrawRobot(iX, iY, iGripperX, iGripperY, szLabel);
- }
-
- GraphicsDrawAGV(iX, iY, szLabel)
- int iX, iY;
- char *szLabel;
- {
- DrawRectangle(iX - AGV_SIZE/2,iY-AGV_SIZE/2,AGV_SIZE,AGV_SIZE);
- MoveTo(iX-AGV_SIZE/2,iY+AGV_SIZE/2+TEXT_SIZE);
- DrawTextString(szLabel);
- }
-
- GraphicsEraseAGV(iX, iY, szLabel)
- int iX, iY;
- char *szLabel;
- { /* Draw AGV XOR to erase */
- GraphicsDrawAGV(iX, iY, szLabel);
- }
-
- GraphicsDrawCNC(iX, iY, szLabel)
- int iX, iY;
- char *szLabel;
- {
- DrawRectangle(iX - CNC_SIZE/2,iY-CNC_SIZE/2,CNC_SIZE,CNC_SIZE);
- MoveTo(iX-CNC_SIZE/2,iY+CNC_SIZE/2+TEXT_SIZE);
- DrawTextString(szLabel);
- }
-
- GraphicsEraseCNC(iX, iY, szLabel)
- int iX, iY;
- char *szLabel;
- { /* Draw CNC XOR to erase */
- GraphicsDrawCNC(iX, iY, szLabel);
- }
-
-
- GraphicsDrawRobotArm(iX, iY, iGripperX, iGripperY)
- int iX, iY;
- int iGripperX, iGripperY;
- {
- DrawLine(iX,iY,iX+iGripperX,iY+iGripperY);
- DrawRectangle(iX + iGripperX - GRIPPER_SIZE/2,iY + iGripperY-GRIPPER_SIZE/2,GRIPPER_SIZE,GRIPPER_SIZE);
- }
-
- GraphicsEraseRobotArm(iX, iY, iGripperX, iGripperY)
- int iX, iY;
- int iGripperX, iGripperY;
- { /* Draw arm XOR to erase */
- GraphicsDrawRobotArm(iX, iY, iGripperX, iGripperY);
- }
-
- DrawLine(iX1,iY1,iX2,iY2)
- int iX1, iY1, iX2, iY2;
- {
- MoveTo(iX1,iY1);
- LineTo(iX2,iY2);
- }
-
- DrawRectangle(iX, iY, iSizeX, iSizeY)
- int iX, iY, iSizeX, iSizeY;
- {
- MoveTo(iX,iY);
- LineTo(iX+iSizeX,iY);
- LineTo(iX+iSizeX,iY+iSizeY);
- LineTo(iX,iY+iSizeY);
- LineTo(iX,iY);
- }
-
- DrawTextString(szString)
- char *szString;
- {
- char spString[255]; /* need to handle mac string drawing */
- strcpy(spString, szString);
- CtoPstr(spString);
- DrawString(spString); /* Mac needs pascal type strings */
- }
-
- #define LIGHTSPEED {define either LIGHTSPEED or MEGAMAX or your own}
-
- #include .stdio.h/
-
- #ifdef MEGAMAX
- #include .menu.h/
- #include .win.h/
- #endif
-
- #ifdef LIGHTSPEED
- #include .MenuMgr.h/
- #include .WindowMgr.h/
- #endif
-
- #define itemdisable 0L
- #define itemenable 1L
- #define itemcheck 2L
- #define itemuncheck 3L
-
- extern char applestring[];
- extern WindowPtr windowpoint();
- extern MenuHandle mhand();
- extern int windmenu;
- extern int dogoaway;
- extern int wprocid;
- extern int show_new_window;
- extern int sizeredraw;
- extern int getlinecaps;
- extern ProcPtr keydownproc;
- extern ProcPtr autokeyproc;
- extern void home();
- extern void stnop();
-
- #define LIGHTSPEED {define either LIGHTSPEED or MEGAMAX or your own}
-
- #include .stdio.h/
-
- #ifdef MEGAMAX
- #include .menu.h/
- #include .win.h/
- #endif
-
- #ifdef LIGHTSPEED
- #include .MenuMgr.h/
- #include .WindowMgr.h/
- #endif
-
- #define itemdisable 0L
- #define itemenable 1L
- #define itemcheck 2L
- #define itemuncheck 3L
-
- extern char applestring[];
- extern WindowPtr windowpoint();
- extern MenuHandle mhand();
- extern int windmenu;
- extern int dogoaway;
- extern int wprocid;
- extern int show_new_window;
- extern int sizeredraw;
- extern int getlinecaps;
- extern ProcPtr keydownproc;
- extern ProcPtr autokeyproc;
- extern void home();
- extern void stnop();
-
-
- /* 9/11/88 Paul D. Fernhout */
- /* This code illustrates object oriented simulation in C */
- /* it is slightly machine dependent in regard to SysYield() */
- /* That is used to return control periodically to the MAC operating system
- while the simulation is running */
-
-
- /* global variable used in multitasking user interfaces like macintosh */
- /* used to prevent two simulations from running at once */
- int iRunning = 0;
-
-
- #define MAX_PARTS 10
- #define MAX_CNCS 3
- #define MAX_AGVS 3
- #define MAX_ROBOTS 3
-
- #define MAX_LABEL 32
- #define MAX_AGV_PROGRAM 64
- #define MAX_ROBOT_PROGRAM 64
-
- /* AGV program functions */
- #define AGV_PROGRAM_NORTH 1
- #define AGV_PROGRAM_SOUTH 2
- #define AGV_PROGRAM_WEST 3
- #define AGV_PROGRAM_EAST 4
- #define AGV_PROGRAM_WAIT 5
- #define AGV_PROGRAM_WAITFORLOAD 6
- #define AGV_PROGRAM_WAITFORUNLOAD 7
-
- /* Robot program functions */
- #define ROBOT_PROGRAM_NORTH 1
- #define ROBOT_PROGRAM_SOUTH 2
- #define ROBOT_PROGRAM_WEST 3
- #define ROBOT_PROGRAM_EAST 4
- #define ROBOT_PROGRAM_NORTHEAST 5
- #define ROBOT_PROGRAM_SOUTHEAST 6
- #define ROBOT_PROGRAM_NORTHWEST 7
- #define ROBOT_PROGRAM_SOUTHWEST 8
- #define ROBOT_PROGRAM_WAIT 9
- #define ROBOT_PROGRAM_LOADTIMERFROMINDEX 10
- #define ROBOT_PROGRAM_LOADINDEX 11
- #define ROBOT_PROGRAM_ADDTOINDEX 12
- #define ROBOT_PROGRAM_LOADCOUNTER 13
- #define ROBOT_PROGRAM_ADDTOCOUNTER 14
- #define ROBOT_PROGRAM_JUMPIFCOUNTERTRUE 15
- #define ROBOT_PROGRAM_JUMPIFCOUNTERFALSE 16
- #define ROBOT_PROGRAM_WAITFORLOADEDAGV 17
- #define ROBOT_PROGRAM_WAITFORUNLOADEDAGV 18
- #define ROBOT_PROGRAM_WAITFORCNCFINISHED 19
- #define ROBOT_PROGRAM_WAITFORCNCEMPTY 20
- #define ROBOT_PROGRAM_GRIP 21
- #define ROBOT_PROGRAM_DROP 22
-
- typedef struct { /* Part */
- int iX;
- int iY;
- char szLabel[MAX_LABEL];
- } SPART, *PSPART;
-
-
- /* Part Methods */
- Part_Initialize(psPart, iX, iY, szLabel)
- PSPART psPart;
- int iX, iY;
- char *szLabel;
- {
- psPart-/iX = iX;
- psPart-/iY = iY;
- if (szLabel) {
- strcpy(psPart-/szLabel, szLabel);
- }
- else {
- *(psPart-/szLabel) = '\0';
- }
- }
-
- Part_Draw(psPart)
- PSPART psPart;
- {
- GraphicsDrawPart(psPart-/iX, psPart-/iY, psPart-/szLabel);
- }
-
- Part_Erase(psPart)
- PSPART psPart;
- {
- GraphicsErasePart(psPart-/iX, psPart-/iY, psPart-/szLabel);
- }
-
- Part_Move(psPart,iChangeX,iChangeY)
- PSPART psPart;
- {
- GraphicsErasePart(psPart-/iX, psPart-/iY, psPart-/szLabel);
- psPart-/iX += iChangeX;
- psPart-/iY += iChangeY;
- GraphicsDrawPart(psPart-/iX, psPart-/iY, psPart-/szLabel);
- }
-
- Part_ChangeLabel(psPart, szNewLabel)
- PSPART psPart;
- char *szNewLabel;
- {
- Part_Erase(psPart);
- strcpy(psPart-/szLabel, szNewLabel);
- Part_Draw(psPart);
- }
-
- Part_ElapseTime(psPart)
- PSPART psPart;
- {
- /* do nothing */
- }
-
-
- typedef struct { /* Computer Numerically Controlled Machine */
- int iX;
- int iY;
- char szLabel[MAX_LABEL];
- PSPART psPart;
- int iStep;
- char szProgram[MAX_LABEL];
- int iProgramSteps;
- } SCNC, *PSCNC;
-
- /* CNC Methods */
- CNC_Initialize(psCNC, iX, iY, szLabel, szProgram, iProgramSteps)
- PSCNC psCNC;
- int iX, iY;
- char *szLabel;
- char *szProgram;
- int iProgramSteps;
- {
- psCNC-/iX = iX;
- psCNC-/iY = iY;
- if (szLabel) {
- strcpy(psCNC-/szLabel, szLabel);
- }
- else {
- *(psCNC-/szLabel) = '\0';
- }
- if (szProgram) {
- strcpy(psCNC-/szProgram, szProgram);
- }
- else {
- *(psCNC-/szProgram) = '\0';
- }
- psCNC-/iProgramSteps = iProgramSteps;
- psCNC-/iStep = 0;
- psCNC-/psPart = 0L;
- }
-
- CNC_Draw(psCNC)
- PSCNC psCNC;
- {
- GraphicsDrawCNC(psCNC-/iX, psCNC-/iY, psCNC-/szLabel);
- }
-
- CNC_Erase(psCNC)
- PSCNC psCNC;
- {
- GraphicsEraseCNC(psCNC-/iX, psCNC-/iY, psCNC-/szLabel);
- }
-
- CNC_StartProcessing(psCNC)
- PSCNC psCNC;
- {
- if (psCNC-/psPart) {
- Part_ChangeLabel(psCNC-/psPart, "In Process - Start");
- }
- }
-
- CNC_FinishProcessing(psCNC)
- PSCNC psCNC;
- {
- if (psCNC-/psPart) {
- Part_ChangeLabel(psCNC-/psPart, psCNC-/szProgram);
- }
- }
-
- CNC_ElapseTime(psCNC)
- PSCNC psCNC;
- {
- char szNewLabel[MAX_LABEL];
-
- if (psCNC-/psPart) {
- if (psCNC-/iProgramSteps) { /* if a program */
- if (psCNC-/iStep == 0) {
- CNC_StartProcessing(psCNC);
- psCNC-/iStep++;
- }
- else if (psCNC-/iStep == psCNC-/iProgramSteps) {
- CNC_FinishProcessing(psCNC);
- psCNC-/iStep++;
- }
- else if (psCNC-/iStep / psCNC-/iProgramSteps) { /* do nothing */
- }
- else { /* processing part */
- strcpy(szNewLabel,"In Process Step ");
- my_itoa(psCNC-/iStep, szNewLabel + strlen(szNewLabel));
- Part_ChangeLabel(psCNC-/psPart,szNewLabel);
- psCNC-/iStep++;
- }
- }
- }
- /* else no part loaded in machine */
- else {
- psCNC-/iStep = 0; /* reset step count for next part */
- }
- }
-
- typedef struct { /* Automated Guided Vehicle */
- int iX;
- int iY;
- char szLabel[MAX_LABEL];
- PSPART psPart;
- int iStep;
- int iProgramSteps;
- int riProgram[MAX_AGV_PROGRAM]; /* Direction, Wait, Load, Unload */
- int iDirection;
- int iTimer;
- int iStop;
- } SAGV, *PSAGV;
-
-
- /* AGV Methods */
- AGV_Initialize(psAGV, iX, iY, szLabel, riProgram)
- PSAGV psAGV;
- int iX, iY;
- char *szLabel;
- int *riProgram;
- {
- psAGV-/iX = iX;
- psAGV-/iY = iY;
- if (szLabel) {
- strcpy(psAGV-/szLabel, szLabel);
- }
- else {
- *(psAGV-/szLabel) = '\0';
- }
- if (riProgram) {
- AGV_LoadProgram(psAGV, riProgram);
- }
- else {
- psAGV-/iStep = 0;
- psAGV-/iProgramSteps = 0;
- psAGV-/iStop = 0;
- psAGV-/iDirection = 0;
- psAGV-/iTimer = 0;
- }
- psAGV-/psPart = 0L;
- }
-
- AGV_Draw(psAGV)
- PSAGV psAGV;
- {
- GraphicsDrawAGV(psAGV-/iX, psAGV-/iY, psAGV-/szLabel);
- }
-
- AGV_Erase(psAGV)
- PSAGV psAGV;
- {
- GraphicsEraseAGV(psAGV-/iX, psAGV-/iY, psAGV-/szLabel);
- }
-
- AGV_Move(psAGV,iChangeX,iChangeY)
- PSAGV psAGV;
- {
- GraphicsEraseAGV(psAGV-/iX, psAGV-/iY, psAGV-/szLabel);
- /* if part carried - handle that */
- if (psAGV-/psPart) {
- Part_Move(psAGV-/psPart,iChangeX,iChangeY);
- }
- psAGV-/iX += iChangeX;
- psAGV-/iY += iChangeY;
- GraphicsDrawAGV(psAGV-/iX, psAGV-/iY, psAGV-/szLabel);
- }
-
- AGV_LoadProgram(psAGV, riProgram)
- PSAGV psAGV;
- int *riProgram;
- {
- int iStep;
- iStep = 0;
- while (riProgram[iStep] != 0 && iStep . MAX_AGV_PROGRAM) { /* While valid command load command and parameter */
- psAGV-/riProgram[iStep] = riProgram[iStep];
- psAGV-/riProgram[iStep+1] = riProgram[iStep+1];
- iStep += 2;
- }
- psAGV-/iProgramSteps = iStep;
- psAGV-/iStep = 0;
- psAGV-/iTimer = 0;
- psAGV-/iStop = 0;
- }
-
- AGV_ElapseTime(psAGV)
- PSAGV psAGV;
- {
- int iProgram;
-
- if (psAGV-/iProgramSteps / 0 && psAGV-/iStop == 0) { /* if a program and running */
- if (psAGV-/iTimer / 0) { /* keep moving as is */
- switch (psAGV-/iDirection) {
- case AGV_PROGRAM_NORTH:
- AGV_Move(psAGV,0,-1);
- break;
- case AGV_PROGRAM_SOUTH:
- AGV_Move(psAGV,0,1);
- break;
- case AGV_PROGRAM_WEST:
- AGV_Move(psAGV,-1,0);
- break;
- case AGV_PROGRAM_EAST:
- AGV_Move(psAGV,1,0);
- break;
- case AGV_PROGRAM_WAIT:
- break; /* do nothing */
- }
- psAGV-/iTimer--;
- }
- else { /* handle program step */
- if (psAGV-/iStep /= psAGV-/iProgramSteps) { /* reset program */
- psAGV-/iStop = 1;
- psAGV-/iStep = 0;
- }
- else { /* handle program */
- iProgram = psAGV-/riProgram[psAGV-/iStep];
- switch (iProgram) {
- case AGV_PROGRAM_NORTH:
- case AGV_PROGRAM_SOUTH:
- case AGV_PROGRAM_WEST:
- case AGV_PROGRAM_EAST:
- case AGV_PROGRAM_WAIT:
- psAGV-/iDirection = iProgram;
- psAGV-/iTimer = psAGV-/riProgram[psAGV-/iStep+1];
- psAGV-/iStep += 2;
- break;
- case AGV_PROGRAM_WAITFORLOAD:
- if (psAGV-/psPart) {
- psAGV-/iStep += 2;
- }
- break;
- case AGV_PROGRAM_WAITFORUNLOAD:
- if (psAGV-/psPart == 0) {
- psAGV-/iStep += 2;
- }
- break;
- default: /* unknown command */
- psAGV-/iStep += 2; /* Skip it */
- break;
- }
- }
- }
- }
- /* else do nothing */
- }
-
-
-
- /* Robot Object */
-
- typedef struct { /* Robot */
- int iX;
- int iY;
- char szLabel[MAX_LABEL];
- int iGripperX;
- int iGripperY;
- PSPART psPart;
- int iStep;
- int iProgramSteps;
- int riProgram[MAX_ROBOT_PROGRAM]; /* Move, Pause, Grip, Drop, Wait */
- int iDirection;
- int iTimer;
- int iStop;
- int iIndex; /* Additional Offset To Timer */
- int iCounter;
- } SROBOT, *PSROBOT;
-
- /* Robot Methods */
- Robot_Initialize(psRobot, iX, iY, szLabel, riProgram)
- PSROBOT psRobot;
- int iX, iY;
- char *szLabel;
- int *riProgram;
- {
- psRobot-/iX = iX;
- psRobot-/iY = iY;
- if (szLabel) {
- strcpy(psRobot-/szLabel, szLabel);
- }
- else {
- *(psRobot-/szLabel) = '\0';
- }
- if (riProgram) {
- Robot_LoadProgram(psRobot, riProgram);
- }
- else {
- psRobot-/iStep = 0;
- psRobot-/iProgramSteps = 0;
- psRobot-/iStop = 0;
- psRobot-/iDirection = 0;
- psRobot-/iTimer = 0;
- }
- psRobot-/iGripperX = 0;
- psRobot-/iGripperY = 0;
- psRobot-/psPart = 0L;
- }
-
- Robot_Draw(psRobot)
- PSROBOT psRobot;
- {
- GraphicsDrawRobot(psRobot-/iX, psRobot-/iY, psRobot-/iGripperX,
- psRobot-/iGripperY, psRobot-/szLabel);
- }
-
- Robot_Erase(psRobot)
- PSROBOT psRobot;
- {
- GraphicsEraseRobot(psRobot-/iX, psRobot-/iY, psRobot-/iGripperX,
- psRobot-/iGripperY, psRobot-/szLabel);
- }
-
- Robot_MoveArm(psRobot,iChangeX,iChangeY)
- PSROBOT psRobot;
- {
- GraphicsEraseRobotArm(psRobot-/iX, psRobot-/iY, psRobot-/iGripperX,
- psRobot-/iGripperY);
- /* if part carried - handle that */
- if (psRobot-/psPart) {
- Part_Move(psRobot-/psPart,iChangeX,iChangeY);
- }
- psRobot-/iGripperX += iChangeX;
- psRobot-/iGripperY += iChangeY;
- GraphicsDrawRobotArm(psRobot-/iX, psRobot-/iY, psRobot-/iGripperX,
- psRobot-/iGripperY);
- }
-
- Robot_LoadProgram(psRobot, riProgram)
- PSROBOT psRobot;
- int *riProgram;
- {
- int iStep;
- iStep = 0;
- while (riProgram[iStep] != 0 && iStep . MAX_ROBOT_PROGRAM) { /* While valid command load command and parameter */
- psRobot-/riProgram[iStep] = riProgram[iStep];
- psRobot-/riProgram[iStep+1] = riProgram[iStep+1];
- iStep += 2;
- }
- psRobot-/iProgramSteps = iStep;
- psRobot-/iStep = 0;
- psRobot-/iTimer = 0;
- psRobot-/iStop = 0;
- }
-
- Robot_ElapseTime(psRobot)
- PSROBOT psRobot;
- {
- int iProgram;
-
- if (psRobot-/iProgramSteps / 0 && psRobot-/iStop == 0) { /* if a program and running */
- if (psRobot-/iTimer / 0) { /* keep moving as is */
- switch (psRobot-/iDirection) {
- case ROBOT_PROGRAM_NORTH:
- Robot_MoveArm(psRobot,0,-1);
- break;
- case ROBOT_PROGRAM_SOUTH:
- Robot_MoveArm(psRobot,0,1);
- break;
- case ROBOT_PROGRAM_WEST:
- Robot_MoveArm(psRobot,-1,0);
- break;
- case ROBOT_PROGRAM_EAST:
- Robot_MoveArm(psRobot,1,0);
- break;
- case ROBOT_PROGRAM_NORTHEAST:
- Robot_MoveArm(psRobot,1,-1);
- break;
- case ROBOT_PROGRAM_SOUTHEAST:
- Robot_MoveArm(psRobot,1,1);
- break;
- case ROBOT_PROGRAM_NORTHWEST:
- Robot_MoveArm(psRobot,-1,-1);
- break;
- case ROBOT_PROGRAM_SOUTHWEST:
- Robot_MoveArm(psRobot,-1,1);
- break;
- case ROBOT_PROGRAM_WAIT:
- break; /* do nothing */
- }
- psRobot-/iTimer--;
- }
- else { /* handle program step */
- if (psRobot-/iStep /= psRobot-/iProgramSteps) { /* reset program */
- psRobot-/iStop = 1;
- psRobot-/iStep = 0;
- }
- else { /* handle program */
- iProgram = psRobot-/riProgram[psRobot-/iStep];
- switch (iProgram) {
- case ROBOT_PROGRAM_NORTH:
- case ROBOT_PROGRAM_SOUTH:
- case ROBOT_PROGRAM_WEST:
- case ROBOT_PROGRAM_EAST:
- case ROBOT_PROGRAM_NORTHEAST:
- case ROBOT_PROGRAM_SOUTHEAST:
- case ROBOT_PROGRAM_NORTHWEST:
- case ROBOT_PROGRAM_SOUTHWEST:
- case ROBOT_PROGRAM_WAIT:
- psRobot-/iDirection = iProgram;
- psRobot-/iTimer = psRobot-/riProgram[psRobot-/iStep+1];
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_LOADTIMERFROMINDEX:
- psRobot-/iTimer = psRobot-/iIndex;
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_LOADINDEX:
- psRobot-/iIndex = psRobot-/riProgram[psRobot-/iStep+1];
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_ADDTOINDEX:
- psRobot-/iIndex += psRobot-/riProgram[psRobot-/iStep+1];
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_LOADCOUNTER:
- psRobot-/iCounter = psRobot-/riProgram[psRobot-/iStep+1];
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_ADDTOCOUNTER:
- psRobot-/iCounter += psRobot-/riProgram[psRobot-/iStep+1];
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_JUMPIFCOUNTERTRUE:
- if(psRobot-/iCounter != 0) {
- psRobot-/iStep = psRobot-/riProgram[psRobot-/iStep+1];
- }
- else {
- psRobot-/iStep += 2;
- }
- break;
- case ROBOT_PROGRAM_JUMPIFCOUNTERFALSE:
- if(psRobot-/iCounter == 0) {
- psRobot-/iStep = psRobot-/riProgram[psRobot-/iStep+1];
- }
- else {
- psRobot-/iStep += 2;
- }
- break;
- case ROBOT_PROGRAM_WAITFORLOADEDAGV:
- if (Robot_CheckLoadedAGVPresence(psRobot)) {
- psRobot-/iStep += 2;
- }
- break;
- case ROBOT_PROGRAM_WAITFORUNLOADEDAGV:
- if (Robot_CheckUnloadedAGVPresence(psRobot)) {
- psRobot-/iStep += 2;
- }
- break;
- case ROBOT_PROGRAM_WAITFORCNCFINISHED:
- if (Robot_CheckForCNCFinished(psRobot)) {
- psRobot-/iStep += 2;
- }
- break;
- case ROBOT_PROGRAM_WAITFORCNCEMPTY:
- if (Robot_CheckForCNCEmpty(psRobot)) {
- psRobot-/iStep += 2;
- }
- break;
- case ROBOT_PROGRAM_GRIP:
- Robot_Grip(psRobot);
- psRobot-/iStep += 2;
- break;
- case ROBOT_PROGRAM_DROP:
- Robot_Drop(psRobot);
- psRobot-/iStep += 2;
- break;
- default: /* unknown command */
- psRobot-/iStep += 2; /* Skip it */
- break;
- }
- }
- }
- }
- /* else do nothing */
- }
-
-
- /* define World Object */
- typedef struct {
- PSPART rpsParts[MAX_PARTS];
- int iPartCount;
- PSCNC rpsCNCs[MAX_CNCS];
- int iCNCCount;
- PSAGV rpsAGVs[MAX_AGVS];
- int iAGVCount;
- PSROBOT rpsRobots[MAX_ROBOTS];
- int iRobotCount;
- } SWORLD, *PSWORLD;
-
- World_Initialize(psWorld)
- PSWORLD psWorld;
- {
- GraphicsClear();
- psWorld-/iPartCount = 0;
- psWorld-/iCNCCount = 0;
- psWorld-/iAGVCount = 0;
- psWorld-/iRobotCount = 0;
- }
-
- World_Draw(psWorld)
- PSWORLD psWorld;
- {
- int iPart;
- int iCNC;
- int iAGV;
- int iRobot;
- simpleevents(); /* simple tools call */
- for (iPart = 0; iPart . psWorld-/iPartCount; iPart++) {
- Part_Draw(psWorld-/rpsParts[iPart]);
- }
- for (iCNC = 0; iCNC . psWorld-/iCNCCount; iCNC++) {
- CNC_Draw(psWorld-/rpsCNCs[iCNC]);
- }
- for (iAGV = 0; iAGV . psWorld-/iAGVCount; iAGV++) {
- AGV_Draw(psWorld-/rpsAGVs[iAGV]);
- }
- for (iRobot = 0; iRobot . psWorld-/iRobotCount; iRobot++) {
- Robot_Draw(psWorld-/rpsRobots[iRobot]);
- }
- }
-
-
-
- World_AddPart(psWorld, psPart)
- PSWORLD psWorld;
- PSPART psPart;
- {
- if (psPart && psWorld-/iPartCount . MAX_PARTS) {
- psWorld-/rpsParts[psWorld-/iPartCount++] = psPart;
- }
- Part_Draw(psPart);
- }
-
- World_AddCNC(psWorld, psCNC)
- PSWORLD psWorld;
- PSCNC psCNC;
- {
- if (psCNC && psWorld-/iCNCCount . MAX_CNCS) {
- psWorld-/rpsCNCs[psWorld-/iCNCCount++] = psCNC;
- }
- CNC_Draw(psCNC);
- }
-
- World_AddAGV(psWorld, psAGV)
- PSWORLD psWorld;
- PSAGV psAGV;
- {
- if (psAGV && psWorld-/iAGVCount . MAX_AGVS) {
- psWorld-/rpsAGVs[psWorld-/iAGVCount++] = psAGV;
- }
- AGV_Draw(psAGV);
- }
-
- World_AddRobot(psWorld, psRobot)
- PSWORLD psWorld;
- PSROBOT psRobot;
- {
- if (psRobot && psWorld-/iRobotCount . MAX_ROBOTS) {
- psWorld-/rpsRobots[psWorld-/iRobotCount++] = psRobot;
- }
- Robot_Draw(psRobot);
- }
-
- PSPART World_PartGripped(psWorld, iX, iY)
- PSWORLD psWorld;
- int iX, iY;
- /* This routine would be much simpler in a true object oriented language.
- Then, there would only be one pass through all the objects, sending them
- all the same message to see if they held onto the part.
- */
- {
- int iPart;
- int iCNC;
- int iAGV;
- int iRobot;
- PSPART psPart;
- for (iPart = 0; iPart . psWorld-/iPartCount; iPart++) {
- psPart = psWorld-/rpsParts[iPart];
- if (psPart-/iX == iX &&
- psPart-/iY == iY) { /* found match */
- /* first remove from any other object that is holding it */
- /* Need to do for every object class explicitly since this is pseudo OOP */
- for (iCNC = 0; iCNC . psWorld-/iCNCCount; iCNC++) {
- if (psWorld-/rpsCNCs[iCNC]-/psPart == psPart) {
- psWorld-/rpsCNCs[iCNC]-/psPart = 0L;
- }
- }
- for (iAGV = 0; iAGV . psWorld-/iAGVCount; iAGV++) {
- if (psWorld-/rpsAGVs[iAGV]-/psPart == psPart) {
- psWorld-/rpsAGVs[iAGV]-/psPart = 0L;
- }
- }
- for (iRobot = 0; iRobot . psWorld-/iRobotCount; iRobot++) {
- if (psWorld-/rpsRobots[iRobot]-/psPart == psPart) {
- psWorld-/rpsRobots[iRobot]-/psPart = 0L;
- }
- }
- return(psPart);
- }
- }
- return(0L);
- }
-
- World_PartDropped(psWorld, psPart)
- PSWORLD psWorld;
- PSPART psPart;
- {
- PSCNC psCNC;
- PSAGV psAGV;
- int iCNC;
- int iAGV;
- int iX;
- int iY;
- iX = psPart-/iX;
- iY = psPart-/iY;
- /* for CNCs and AGVs, need to see if part dropped on it */
- /* This would be easier in a true OOPS */
- for (iCNC = 0; iCNC . psWorld-/iCNCCount; iCNC++) {
- psCNC = psWorld-/rpsCNCs[iCNC];
- if (psCNC-/iX == iX && psCNC-/iY == iY) {
- psCNC-/psPart = psPart;
- return;
- }
- }
- for (iAGV = 0; iAGV . psWorld-/iAGVCount; iAGV++) {
- psAGV = psWorld-/rpsAGVs[iAGV];
- if (psAGV-/iX == iX && psAGV-/iY == iY) {
- psAGV-/psPart = psPart;
- return;
- }
- }
- }
-
- World_CheckLoadedAGVPresence(psWorld, iX, iY)
- PSWORLD psWorld;
- int iX, iY;
- {
- PSAGV psAGV;
- int iAGV;
-
- for (iAGV = 0; iAGV . psWorld-/iAGVCount; iAGV++) {
- psAGV = psWorld-/rpsAGVs[iAGV];
- if (psAGV-/iX == iX && psAGV-/iY == iY && psAGV-/psPart) {
- return(1);
- }
- }
- return(0);
- }
-
- World_CheckUnloadedAGVPresence(psWorld, iX, iY)
- PSWORLD psWorld;
- int iX, iY;
- {
- PSAGV psAGV;
- int iAGV;
-
- for (iAGV = 0; iAGV . psWorld-/iAGVCount; iAGV++) {
- psAGV = psWorld-/rpsAGVs[iAGV];
- if (psAGV-/iX == iX && psAGV-/iY == iY && psAGV-/psPart == 0) {
- return(1);
- }
- }
- return(0);
- }
-
- World_CheckForCNCFinished(psWorld, iX, iY)
- PSWORLD psWorld;
- int iX, iY;
- {
- PSCNC psCNC;
- int iCNC;
-
- for (iCNC = 0; iCNC . psWorld-/iCNCCount; iCNC++) {
- psCNC = psWorld-/rpsCNCs[iCNC];
- if (psCNC-/iX == iX && psCNC-/iY == iY) {
- return(psCNC-/iStep / psCNC-/iProgramSteps);
- }
- }
- return(0);
- }
-
- World_CheckForCNCEmpty(psWorld, iX, iY)
- PSWORLD psWorld;
- int iX, iY;
- {
- PSCNC psCNC;
- int iCNC;
-
- for (iCNC = 0; iCNC . psWorld-/iCNCCount; iCNC++) {
- psCNC = psWorld-/rpsCNCs[iCNC];
- if (psCNC-/iX == iX && psCNC-/iY == iY) {
- return(psCNC-/psPart == 0);
- }
- }
- return(0);
- }
-
- World_ElapseTime(psWorld)
- PSWORLD psWorld;
- {
- int iPart;
- int iCNC;
- int iAGV;
- int iRobot;
- simpleevents(); /* simple tools call */
- for (iPart = 0; iPart . psWorld-/iPartCount; iPart++) {
- Part_ElapseTime(psWorld-/rpsParts[iPart]);
- }
- for (iCNC = 0; iCNC . psWorld-/iCNCCount; iCNC++) {
- CNC_ElapseTime(psWorld-/rpsCNCs[iCNC]);
- }
- for (iAGV = 0; iAGV . psWorld-/iAGVCount; iAGV++) {
- AGV_ElapseTime(psWorld-/rpsAGVs[iAGV]);
- }
- for (iRobot = 0; iRobot . psWorld-/iRobotCount; iRobot++) {
- Robot_ElapseTime(psWorld-/rpsRobots[iRobot]);
- }
- }
-
- /* variables defining world */
- SWORLD sWorld;
- PSWORLD psWorld = &sWorld;
-
- World_ElapseTimeAmount(psWorld, iAmount)
- PSWORLD psWorld;
- int iAmount;
- {
- for (;iAmount / 0; iAmount--) {
- if (iRunning == 0) return; /* break out of routine in multitasking system */
- World_ElapseTime(psWorld);
- }
- }
-
- /* Robot routines that interact with the world object */
-
- Robot_CheckLoadedAGVPresence(psRobot) /* check if AGV is below gripper */
- PSROBOT psRobot;
- {
- return(World_CheckLoadedAGVPresence(psWorld,
- psRobot-/iX + psRobot-/iGripperX, psRobot-/iY + psRobot-/iGripperY));
- }
-
- Robot_CheckUnloadedAGVPresence(psRobot) /* check if AGV is below gripper */
- PSROBOT psRobot;
- {
- return(World_CheckUnloadedAGVPresence(psWorld,
- psRobot-/iX + psRobot-/iGripperX, psRobot-/iY + psRobot-/iGripperY));
- }
-
- Robot_CheckForCNCFinished(psRobot) /* check if finished CNC below gripper */
- PSROBOT psRobot;
- {
- return(World_CheckForCNCFinished(psWorld,
- psRobot-/iX + psRobot-/iGripperX, psRobot-/iY + psRobot-/iGripperY));
- }
-
- Robot_CheckForCNCEmpty(psRobot) /* check if finished CNC below gripper */
- PSROBOT psRobot;
- {
- return(World_CheckForCNCEmpty(psWorld,
- psRobot-/iX + psRobot-/iGripperX, psRobot-/iY + psRobot-/iGripperY));
- }
-
- Robot_Grip(psRobot)
- PSROBOT psRobot;
- {
- psRobot-/psPart = World_PartGripped(psWorld,
- psRobot-/iX + psRobot-/iGripperX, psRobot-/iY + psRobot-/iGripperY);
- }
-
- Robot_Drop(psRobot)
- PSROBOT psRobot;
- {
- PSPART psPart;
- psPart = psRobot-/psPart;
- psRobot-/psPart = 0L;
- World_PartDropped(psWorld, psPart);
- }
-
-
- /* Simulation Code */
-
- int riRobotProgram1[] = {
- ROBOT_PROGRAM_WEST, 50,
- ROBOT_PROGRAM_WAITFORLOADEDAGV, 0,
- ROBOT_PROGRAM_GRIP, 0,
- ROBOT_PROGRAM_SOUTHEAST, 100,
- ROBOT_PROGRAM_DROP, 0,
- ROBOT_PROGRAM_NORTH, 100,
- ROBOT_PROGRAM_WEST, 50,
- ROBOT_PROGRAM_SOUTHEAST, 50,
- ROBOT_PROGRAM_SOUTH, 50,
- ROBOT_PROGRAM_WAITFORCNCFINISHED, 0,
- ROBOT_PROGRAM_GRIP, 0,
- ROBOT_PROGRAM_NORTHWEST, 100,
- ROBOT_PROGRAM_WAITFORUNLOADEDAGV, 0,
- ROBOT_PROGRAM_DROP, 0,
- ROBOT_PROGRAM_EAST, 50,
- 0, 0
- };
-
- int riAGVProgram1[] = {
- AGV_PROGRAM_SOUTH, 50,
- AGV_PROGRAM_WAITFORUNLOAD, 0,
- AGV_PROGRAM_NORTH, 200,
- AGV_PROGRAM_WAIT, 200,
- AGV_PROGRAM_SOUTH, 200,
- AGV_PROGRAM_WAITFORLOAD, 0,
- AGV_PROGRAM_SOUTH, 200,
- 0, 0
- };
-
-
- simulation1() {
- SPART sPart;
- PSPART psPart;
- SROBOT sRobot;
- PSROBOT psRobot;
- SAGV sAGV;
- PSAGV psAGV;
- SCNC sCNC;
- PSCNC psCNC;
-
- if (iRunning) return;
- iRunning = 1;
-
- psPart = &sPart;
- psRobot = &sRobot;
- psAGV = &sAGV;
- psCNC = &sCNC;
-
- World_Initialize(psWorld);
-
- Part_Initialize(psPart, 50, 50, "Raw");
- World_AddPart(psWorld,psPart);
-
- Robot_Initialize(psRobot, 100, 100, "Robot One", riRobotProgram1);
- World_AddRobot(psWorld,psRobot);
-
- AGV_Initialize(psAGV, 50, 50, "AGV One", riAGVProgram1);
- psAGV-/psPart = psPart; /* needed for simulation as part starts on AGV */
- World_AddAGV(psWorld,psAGV);
-
- CNC_Initialize(psCNC, 150, 200, "CNC One", "Vase", 300);
- World_AddCNC(psWorld,psCNC);
-
- World_ElapseTimeAmount(psWorld,1500);
- iRunning = 0;
- }
-
-
- int riRobotProgram2[] = {
- ROBOT_PROGRAM_LOADINDEX, 0,
- ROBOT_PROGRAM_LOADCOUNTER, 6,
- ROBOT_PROGRAM_NORTH, 50,
- ROBOT_PROGRAM_EAST, 0,
- ROBOT_PROGRAM_LOADTIMERFROMINDEX, 0,
- ROBOT_PROGRAM_GRIP, 0,
- ROBOT_PROGRAM_WEST, 0,
- ROBOT_PROGRAM_LOADTIMERFROMINDEX, 0,
- ROBOT_PROGRAM_SOUTH, 150,
- ROBOT_PROGRAM_WAITFORCNCEMPTY, 0,
- ROBOT_PROGRAM_DROP, 0,
- ROBOT_PROGRAM_NORTH, 100,
- ROBOT_PROGRAM_ADDTOCOUNTER, -1,
- ROBOT_PROGRAM_JUMPIFCOUNTERFALSE, 44,
- ROBOT_PROGRAM_NORTH, 50,
- ROBOT_PROGRAM_EAST, 0,
- ROBOT_PROGRAM_LOADTIMERFROMINDEX, 0,
- ROBOT_PROGRAM_ADDTOINDEX, 30,
- ROBOT_PROGRAM_EAST, 30,
- ROBOT_PROGRAM_JUMPIFCOUNTERTRUE, 10,
- ROBOT_PROGRAM_WEST, 0,
- 0, 0
- };
-
- int riRobotProgram3[] = {
- ROBOT_PROGRAM_LOADINDEX, 0,
- ROBOT_PROGRAM_LOADCOUNTER, 6,
- ROBOT_PROGRAM_WEST, 100,
- ROBOT_PROGRAM_WAITFORCNCFINISHED, 0,
- ROBOT_PROGRAM_GRIP, 0,
- ROBOT_PROGRAM_EAST, 150,
- ROBOT_PROGRAM_LOADTIMERFROMINDEX, 0,
- ROBOT_PROGRAM_DROP, 0,
- ROBOT_PROGRAM_WEST, 0,
- ROBOT_PROGRAM_LOADTIMERFROMINDEX, 0,
- ROBOT_PROGRAM_WEST, 50,
- ROBOT_PROGRAM_ADDTOCOUNTER, -1,
- ROBOT_PROGRAM_ADDTOINDEX, 50,
- ROBOT_PROGRAM_JUMPIFCOUNTERTRUE, 4,
- 0, 0
- };
-
-
- simulation2() {
- SPART sPart1, sPart2, sPart3, sPart4, sPart5, sPart6;
- SROBOT sRobot1, sRobot2, sRobot3;
- SAGV sAGV1, sAGV2;
- SCNC sCNC1, sCNC2;
-
- if (iRunning) return;
- iRunning = 1;
-
- World_Initialize(psWorld);
-
- Part_Initialize(&sPart1, 50, 50, "Raw");
- World_AddPart(psWorld,&sPart1);
- Part_Initialize(&sPart2, 80, 50, "Raw");
- World_AddPart(psWorld,&sPart2);
- Part_Initialize(&sPart3, 110, 50, "Raw");
- World_AddPart(psWorld,&sPart3);
- Part_Initialize(&sPart4, 140, 50, "Raw");
- World_AddPart(psWorld,&sPart4);
- Part_Initialize(&sPart5, 170, 50, "Raw");
- World_AddPart(psWorld,&sPart5);
- Part_Initialize(&sPart6, 200, 50, "Raw");
- World_AddPart(psWorld,&sPart6);
-
- Robot_Initialize(&sRobot1, 50, 100, "Robot One", riRobotProgram2);
- World_AddRobot(psWorld,&sRobot1);
-
- Robot_Initialize(&sRobot2, 150, 200, "Robot Two", riRobotProgram3);
- World_AddRobot(psWorld,&sRobot2);
-
- CNC_Initialize(&sCNC1, 50, 200, "CNC One", "Vase", 60);
- World_AddCNC(psWorld,&sCNC1);
-
- World_ElapseTimeAmount(psWorld,4000);
-
- iRunning = 0;
- }
-
-
- my_itoa(iNumber, szString)
- int iNumber;
- char *szString;
- {
- int iDigit;
- char szNumber[MAX_LABEL+1];
- szNumber[MAX_LABEL] = '\0';
- for (iDigit = 1; iNumber / 0; iDigit++) {
- szNumber[MAX_LABEL - iDigit] = '0' + (iNumber % 10);
- iNumber /= 10;
- }
- strcat(szString, szNumber+MAX_LABEL-iDigit+1);
- }
-
-
- /*
- Title : SimpleTools2.c
- Author : Erik Kilk Copyright 1985, 1987
- Dates : June 7, 1985, June 3, 1986, November 8, 1986
- November 28, 1986, April 17, 1987, April 21, 1987.
-
- SimpleTools is a collection of routines to aid programming
- simple "Macintosh looking" programs. SimpleTools initializes
- the toolbox, monitors & acts upon events, and provides generic
- i/o routines for your application. You initialize your program
- by letting SimpleTools know what windows and menus you want along
- with what functions SimpleTools should call when they are
- selected.
-
- The purpose of SimpleTools is to encourage you to program those
- simple programs or to pilot larger programs which you may not
- do due to the enormous effort required to use the Macintosh
- toolbox. My goal was to study Inside Macintosh once to
- Create SimpleTools and then be able to forget most of the usages
- of the Toolbox routines. Instead of thumbing through hundreds of
- pages of Inside Macintosh just to get something up and running,
- one need only remember a dozen generic calls.
-
- SimpleTools is very powerful, yet also very simple to use. One
- can get a program up and running with desk accessories, windows,
- and menus in only a few minutes. Advance features of SimpleTools
- allow you to retrieve enough information from SimpleTools to call
- any of the toolbox routines manually if need be.
-
-
- =========================================================================
-
- You may use, study, copy, and freely distribute SimpleTools if:
- 1) You mention "Programmed with the aid of SimpleTools
- (c) Erik Kilk 1986" in your About... window of all
- programs distributed free, share, or marketted.
- 2) You register by sending $20 or more to:
- Erik Kilk
- 834 Hudis St
- Rohnert Park, CA 94928
- (707) 794-2424 weekday afternoons
- to encourage me to maintain and improve SimpleTools.
-
- ==/ For a diskette including the most recent version of SimpleTools,
- several detailed examples of using SimpleTools, and a MacWrite file
- describing SimpleTools and its use in more detail, send me a
- diskette with enough stamps to mail it back to you. Make sure you
- have registered as stated above.
-
- This is 128k, MFS, HFS, old ROM, new ROM, Mac+, & TMON compatible.
- This file compiles and executes with Megamax 3.0 beta and
- LightSpeed 2.1. Adjust the definition in simple.h for your
- compiler. When porting to other compilers, pay particular
- attention to where the Lightspeed and Megamax code is specified
- since these places are the likely problem areas.
-
- LIGHTSPEED NOTE:
-
- Drag SimpleTools out of the main segment in your project window.
- You do this by dragging it below the dotted horizontal Line in the
- project window.
-
- SimpleTools requires the MacTraps library and stringsasm.c or
- strings.c. If you load the unix library, your project will be
- larger than needed (unless you need unix for your own program.)
-
- MEGAMAX NOTE:
-
- Use Megamax's convert utility to convert all Mac names to all
- lower case. If you send me suggestions and/or new code for
- SimpleTools, please convert back to mixed case first.
-
- =========================================================================
-
- SimpleTools provides the following functions for your application
- to call. Note that your application need not call any Toolbox
- routines directly. The entire C program (including the standard
- desk accessory support):
-
- main ()
- {
- simpletools ("Skel");
- simplequits ();
- runsimpletools ();
- }
-
- will execute as a Macintosh program, terminating upon the user
- selecting Quit. SimpleTools includes:
-
- simpletools () - init Toolbox and SimpleTools
- simplequits () - add Transfer & Quit menus
- simpleevents () - process next Mac event
- runsimpletools () - continuously process events
-
- menu ("File","New",new) - install a menu
- window ("My Window",..) - install a window
-
- run (function) - install a periodic function
- stop (function) - remove a periodic function
-
- havenewrom () - test for new 128k ROM
- withwindow("My Window") - set output to a window
-
- stgotoxy (x, y) - set pen to text position x, y
- home () - clear window, set pen to home
- getline (deflt, dest) - with TE editing, get a Line
- prompt (question, dest) - with dialog box & TE, get a Line
- message (string) - with dialog box, print a string
- getfile ("TEXT", name) - with list, select a filename
- putfile (orig, name) - with list, select a filename
-
-
- A complete Macintosh Style application (including windowing and
- menus) is given in the following trivial example...
-
- #include .simple.h/
-
- char name[50];
-
- got_beep()
- {
- SysBeep (10);
- }
-
- got_getname()
- {
- char newname[50];
- *newname = 0;
- if (prompt ("What is your name?", newname)) {
- strcpy (name, newname);
- withwindow ("My Window");
- home();
- update();
- }
- }
-
- in_content(x, y)
- int x, y;
- {
- MoveTo (x, y); LineTo (x, y);
- }
-
- update()
- {
- char outstring[100];
- stgotoxy (1, 5);
- sprintf (outstring,"Hello, %s", name);
- #ifndef MEGAMAX
- CtoPstr (outstring);
- #endif
- DrawString (outstring);
- }
-
- main ()
- {
- simpletools ("Sample Program");
- simplequits ();
- menu ("Commands", "Beep", got_beep);
- menu ("Commands", "Get Name", got_getname);
- strcpy (name, "World");
- window ("My Window",0,0,0,0,0L,0L,update,
- in_content);
- runsimpletools ();
- }
-
- =========================================================================
-
- ROUTINES YOUR APPLICATION MAY CALL:
-
-
- simpleevents ()
-
- To be called repeatedly by your program's main routine. In
- most SimpleTools programs, your main routine will initialize
- and install SimpleTools followed by an loop such as:
-
- for (;;) simpleevents();
-
- This routine handles all window changes, menu requests, and
- other Macintosh events. There is also a routine called with
- runsimpletools() which does not return. It simply performs the
- above loop. Program exit is accomplished by assigning an
- exiting routine to a menu, usually this is File/Quit.
-
-
- simpletools (about_string)
- char *about_string;
-
- To be called once at the very beginning of your main routine.
- This routine initializes all the Macintosh software managers
- and installs the basic Apple, File, and Edit menus. The
- about_string is the name of the menu Item to appear first under
- the Apple menu.
-
-
- simplequits ()
-
- Installs a simple File/Quit and File/Transfer menu. You only
- want to installs these if no application dependent processing
- must be done when the user selects Quit or Transfer.
-
-
- menu (name, Item, routine)
- char *name;
- char *Item;
- ProcPtr routine;
-
- To be called when a new menu is to be installed on the menu bar
- or when the characteristics of that menu are to be modified.
- Name is the name of the menu to appear on the menu bar. Item
- is the name of the Item to appear under the menu name. Routine
- is the name of the routine to be executed when the stated menu/Item
- has been selected. The characteristics of the menu may be changed
- by passing one of the constants itemdisable, itemenable, itemcheck,
- or itemuncheck in place of the routine.
-
-
- window (name, xtop, ytop, xbot, ybot, act, deact, update, content)
- char *name;
- int xtop, ytop;
- int xbot, ybot;
- ProcPtr act, deact, update, content;
-
- To be called when a new window is to be installed on the screen
- or when the characteristics of that window are to be modified.
- Name is the name of the window. Xtop, ytop, xbot, and ybot are
- the initial coordinates of the new window. Act is the name of
- the procedure to execute when the window becomes the top or
- active window. Deact is the name of the procedure to execute when
- the window ceases being the top window and deactivates. Update
- is the name of the procedure called when the Macintosh needs to
- redraw the window's contents. Content is the name of the procedure
- called when the mouse is pressed within the window. The procedure
- specified as content will be passed an x and y integer value
- representing the local mouse coordinates.
-
-
- withwindow (name)
- char *name;
-
- To be called when you want to select which window will receive
- output and drawings. In most cases, SimpleTools will select the
- appropriate window before calling your specified act, deact,
- update, or content procedures. Use this at other times.
-
-
- run (routine)
- ProcPtr routine;
-
- To be called when you want a routine to be continuously executed
- once each time simpleevents() is called. Small, quickly running
- routines should be used so as not to Delay the next event
- processing. Pseudo multiprocessing with each routine running in
- its own window can be accomplished by making sure a run routine
- uses withwindow() to direct its output to the proper window.
-
-
- stop (routine)
- ProcPtr routine;
-
- To be called when you want to remove a previously run() routine
- from the list of routines to run. 50 routines can fit into the
- run list.
-
-
- home ()
-
- Clears the current window and positions the pen such that any
- following text will appear in the upper left corner of the
- window.
-
-
- stgotoxy (column, row)
- int column, row;
-
- Positions the pen in the current window so that the next text
- output will appear in text row and column. This is compatible
- with some old text only terminals. stgotoxy (1, 1) positions the
- pen in the upper left corner. Any negative coordinate leaves
- that axis of the pen where it currently is.
-
-
- getline (default, destination)
- char *default;
- char *destination;
-
- Calling this routine begins a "modal" mode where the user is
- required to enter a Line of text. This would be similar to
- using scanf() on "non-Mac", text-only terminals. This routine
- uses the Macintoshes built in Text-Edit routines allowing the
- user to edit his Line until .RETURN/ is pressed. The flashing
- bar Cursor is positioned at the current pen location. The
- resulting string is placed into destination. Default contains
- the initial value to be displayed on the screen. You may use
- the null string "" for default.
-
-
- prompt (question, answer)
- char *question;
- char *answer;
-
- This routine places a small 3-lined Macintosh Style dialog window
- prompting the user with question and getting the answer in a
- boxed Text-Edit area. Two buttons are displayed to terminate the
- user entry. If Cancel is clicked upon, FALSE is returned. If
- okay is clicked upon, TRUE is returned. Answer must be set to
- a default value, "" is okay.
-
-
- message (message)
- char *message;
-
- This routine is similar to prompt except no textual response is
- asked from the user. This is like an Alert dialog. Just like
- prompt, TRUE or FALSE is returned depending upon which Button the
- user presses.
-
-
- getfile (file_type, reply)
- char *file_type;
- char *replay;
-
- This routine places the standard Macintosh SFGetFile() window
- up with a list of files of file_type. Once the user selects a
- file, the answer is returned in the string reply. Also, and
- very important for HFS, the working volume/folder is set so that
- any subsequent open() with reply as the file name will open the
- correct selected file. The open() should be done before someone
- has a chance to change the working volume. This routine will
- return FALSE if the user selects the CANCEL Button.
-
-
- putfile (origname, reply)
- char *origname;
- char *reply;
-
- This routine is like getfile, except the standard putfile window
- is displayed with origname as the default name to save a file as.
- The actual name selected by the user is returned in reply. As
- getfile, the working volume/folder is set properly for the next
- open() call.
-
- =========================================================================
-
- THE FOLLOWING IS THE FILE simple.h. YOU SHOULD COPY THIS PORTION
- INTO A NEW FILE NAMED simple.h SO YOU CAN #include IT INTO YOUR
- SOURCE FILES.
-
- =========================================================================
-
- #define LIGHTSPEED {define either LIGHTSPEED or MEGAMAX or your own}
-
- #include .stdio.h/
-
- #ifdef MEGAMAX
- #include .menu.h/
- #include .win.h/
- #endif
-
- #ifdef LIGHTSPEED
- #include .MenuMgr.h/
- #include .WindowMgr.h/
- #endif
-
- #define itemdisable 0L
- #define itemenable 1L
- #define itemcheck 2L
- #define itemuncheck 3L
-
- extern char applestring[];
- extern WindowPtr windowpoint();
- extern MenuHandle mhand();
- extern int windmenu;
- extern int dogoaway;
- extern int wprocid;
- extern int show_new_window;
- extern int sizeredraw;
- extern int getlinecaps;
- extern ProcPtr keydownproc;
- extern ProcPtr autokeyproc;
- extern void home();
- extern void stnop();
- =========================================================================
-
- Here begins SimpleTools.c
- _________________________
- */
-
- #include "simple.h" /* define compiler in here */
-
- #ifdef MEGAMAX
- overlay "simpletools" /* compiler dependent */
-
- #include .mem.h/
- #include .qd.h/
- #include .qdvars.h/
- #include .misc.h/
- #include .event.h/
- #include .res.h/
- #include .win.h/
- #include .dialog.h/
- #include .menu.h/
- #include .string.h/
- #include .stdio.h/
- #include .pack.h/
- #include .te.h/
- #include .toolbox.h/
-
- #define ZZ &
- #else
- #include .MemoryMgr.h/
- #include .Quicke .te.h/
- #include .toolbox.h/
-
- #define ZZ &
- #else
- #include .MemoryMgr.h/
- #include .Quickdraw.h/
- #include .EventMgr.h/
- #include .ResourceMgr.h/
- #include .WindowMgr.h/
- #include .TextEdit.h/
- #include .DialogMgr.h/
- #include .MenuMgr.h/
- #include .strings.h/
- #include .stdio.h/
- #include .PackageMgr.h/
- #include .ToolboxUtil.h/
- #include .StdFilePkg.h/
- #include .pascal.h/
-
- #define ZZ
- #endif
-
- #define TRUE (-1) /* local definitions */
- #define FALSE 0
- #define maxsruns 50 /* procedure table size */
- #define MESSN 30 /* array size for message dialog items */
- #define QUESN 40 /* array size for prompt dialog items */
- #define ROM85 0x28E /* new rom stuff */
- #define NEWROM 0x7FFF
- #define inzoomout 8
- #define inzoomin 7
- #define zoomproc 8
-
- typedef struct { /* structure for an Item */
- char itemname[40];
- int itemno; /* Item number within menu */
- int menuId; /* menu id */
- MenuHandle menuhand; /* Item's menu's Handle */
- ProcPtr menurun; /* procedure to run */
- Ptr next; /* pointer to the next Item */
- } itemdatum;
-
- typedef struct { /* structure for a menu */
- char menuname[20]; /* to allow reference by name */
- int menuId; /* menu id */
- MenuHandle menuhand; /* menu Handle to reference menu*/
- itemdatum **itemlist; /* pointer to the list of items */
- Ptr next; /* pointer to the next menu */
- } menudatum;
-
- typedef struct { /* structure for a window */
- char windname[80]; /* window's name and reference */
- WindowPtr wptr; /* window's pointer reference */
- ProcPtr wact; /* the activate procedure */
- ProcPtr wdeact; /* the deactivate procedure */
- ProcPtr wupdate; /* the update procedure */
- ProcPtr wcontent; /* the content procedure */
- Ptr next; /* pointer to the next window */
- } winddatum;
-
- #ifdef LIGHTSPEED
- pascal Boolean *TrackBox() = 0xA83B;
- pascal void *ZoomWindow() = 0xA83A;
- #endif
-
- WindowPtr windowpoint();
-
- /* Local variables */
-
- menudatum **simplemenus; /* Handle to menu data */
- char accname[80]; /* desk accessory name to open */
- Rect dragrect, sizerect; /* limits for moving windows */
- Rect swholescreen;
- winddatum **simplewinds; /* Handle to window data */
- int firstwind; /* if no windows have been made */
- ProcPtr simpleruns[maxsruns]; /* list of procedures to run */
- WindowPtr debugw; /* window pointer for debugging */
- int snewrom;
- int getlinecaps = FALSE;
-
- /************************************************************************/
- /* GLOBAL USER MODIFIABLE VARIABLES */
- /* These are variables that you can declare extern so that you can use */
- /* them to change the SimpleTools defaults */
- /************************************************************************/
-
- /* wprocid = type of window to Create on next window() call */
- /* For LightSpeed C, use a lower case d and upper case P for */
- /* DocumentProc. Megamax conversion program does this wrong. */
-
- int wprocid = documentProc;
-
- /* dogoaway = is the next created window to have a go-away box */
-
- int dogoaway = TRUE;
-
- /* keydownproc = the procedure to be called when keyDown is detected */
- /* autokeyproc = the procedure to be called when autoKey is detected */
- /* BOTH OF THESE ARE PASSED THE EVENTRECORD SO THE KEY CAN BE FOUND */
-
- ProcPtr keydownproc,
- autokeyproc;
-
- /* applestring = a string containing the Apple for menu reference */
-
- char applestring[2]
- = {'\024', '\0'};
-
- /* windmenu = does the next window created get a choice under the
- window menu (so a closed window can be brought back */
-
- int windmenu = TRUE;
-
- /* sizeredraw = is the window's content area erased and made
- updateable upon being resized */
-
- int sizeredraw = FALSE;
-
- /* show_new_window = does the created window get displayed right away
- on the screen, if not, then it is hidden */
-
- int show_new_window = TRUE;
-
-
- /******************************************************************/
- /* Dialog lists. These were calculated by using the new resource */
- /* editor to make a template for a dialog and then using fedit to */
- /* list the hex listing of the Item list for the dialog. */
- /******************************************************************/
-
- int messd[MESSN] = {2,0,0,0x38,0xf1,0x4c,0x12d,0x402,0x4f4b,0,0,5,5,
- 0x36,0x12d,0x800,0,0,0x38,0xac,0x4c,0xe8,0x406,
- 0x4361,0x6e63,0x656c};
- int quesd[QUESN] = {3,0,0,0x21,0xf0,0x35,0x12c,0x402,0x4f4b,0,0,8,8,
- 0x28,0xe8,0x800,0,0,0x2b,8,0x4b,0xe8,0x1000,0,0,
- 8,0xf0,0x1c,0x12c,0x406,0x4361,0x6e63,0x656c};
-
-
- /* Local procedure */
-
- void stnop() /* a no op procedure for defaults */
- {
- }
-
- char *ptoc(s)
- char *s;
- {
- #ifndef MEGAMAX
- return (PtoCstr(s));
- #else
- return (s);
- #endif
- }
-
- char *ctop(s)
- char *s;
- {
- #ifndef MEGAMAX
- return (CtoPstr(s));
- #else
- return (s);
- #endif
- }
-
- /* Given a menu name, find our data structure for it. Return a Handle
- to this structure. This is a local procedure for SimpleTools use.
- */
-
- /* local procedure */
-
- menudatum **getourmenuhandle (name)
- char *name; /* name of menu bar menu */
- {
- menudatum **here, **temp; /* hand to menu structure*/
- here = simplemenus;
-
- /* find the menu name or the end of out menu list */
- HLock (here);
- while (strcmp(name,(**here).menuname) && (**here).next ) {
- temp = here;
- here = (menudatum **)(**here).next;
- HUnlock (temp);
- HLock (here);
- }
-
- /* see if we found it or just the end of the list */
- if (!strcmp(name,(**here).menuname)) {
- HUnlock (here);
- return (here);
- } else {
- HUnlock (here);
- return ((menudatum **)0L);
- }
- }
-
- /* This takes a Handle to our personal Item record and either a
- procedure name or a modifier code. If it got a procedure name,
- it sets it to the Item's procedure to run when the Item is chosen.
- If it got a modifier code, it changes the state of the menu's Item
- to checked, unchecked, enabled, or disabled. It especially keeps
- track of the standard Edit menu items so we can restore them after
- a desk accessory is finished.
- */
-
- /* Local procedure */
-
- setitems ( items, routine) /* set a menu Item's routine or display */
- itemdatum **items; /* if items is neg, then whole menu */
- ProcPtr routine;
- {
- int inumber;
- MenuHandle mhand;
-
- /* check to see if a procedure pointer was given to us */
- if ( (((long)items)/0L) && (routine / (ProcPtr)0x1000L)) {
- /* good procedure value */
- (**items).menurun = routine;
- return;
- }
-
- /* Calculate which Item number we are going to modify */
- if ( (long)items . 0L) { /* the whole menu */
- mhand = (MenuHandle) (0L - (long)items);
- inumber = 0;
- } else { /* just one Item */
- mhand = (**items).menuhand;
- inumber = (**items).itemno;
- }
-
- /* If a NULL procedure pointer, then set to a no_op routine */
- if ( (inumber / 0) && ((**items).menurun == (ProcPtr)0L) )
- (**items).menurun = (ProcPtr) stnop;
-
- /* Now change the state of a menu Item */
- switch ((int)routine) {
- case itemdisable:
- DisableItem(mhand,inumber); break;
- case itemenable:
- EnableItem(mhand, inumber); break;
- case itemcheck:
- CheckItem(mhand, inumber, TRUE); break;
- case itemuncheck:
- CheckItem(mhand, inumber, FALSE); break;
- }
- if (inumber == 0) DrawMenuBar(); /* if main menu was changed */
-
- }
-
- /* This routine is called by the simpletools() initial routine. It gets
- the pointer list of menus started, loads the desk accessories into
- the Apple menu, and loads up some standard menu entries. The reason
- menu File has a New entry, and none others, is because as this code
- currently stands, a menu must have at least one Item. And since we
- want File before Edit, I had to make an entry. The most commonly used
- Item under File is Quit. But we like quit to be at the end of the list.
- So, since New is usually always first when it is used, that the one
- chosen to start File.
- */
-
- /* Local procedure */
-
- initsmenus(about) /* init simpletools' menus */
- char *about;
- {
- itemdatum **items;
-
- simplemenus = (menudatum **) NewHandle ( (long)sizeof(menudatum));
- HLock (simplemenus);
-
- strcpy ( (**simplemenus).menuname, applestring);
- (**simplemenus).menuId = 1;
- (**simplemenus).next = (Ptr) 0L;
- ctop ((**simplemenus).menuname);
- (**simplemenus).menuhand = NewMenu (1, (**simplemenus).menuname);
- ptoc ((**simplemenus).menuname);
- HUnlock ((**simplemenus).menuhand);
-
- (**simplemenus).itemlist = (itemdatum **)NewHandle (
- (long)sizeof(itemdatum));
- items = (itemdatum **) (**simplemenus).itemlist;
- HLock (items);
-
- strcpy ((**items).itemname, about);
- (**items).itemno = 1;
- (**items).menuId = 1;
- (**items).menuhand = (**simplemenus).menuhand;
- (**items).menurun = (ProcPtr) stnop;
- (**items).next = 0L;
- HUnlock (items);
-
- ctop (about);
- AppendMenu ((**simplemenus).menuhand, about);
- ptoc (about);
- DisableItem ((**simplemenus).menuhand, 1);
- menu (applestring, "-", (ProcPtr) itemdisable);
- #ifdef MEGAMAX
- AddResMenu ((**simplemenus).menuhand, "DRVR");
- #else
- AddResMenu ((**simplemenus).menuhand, 'DRVR');
- #endif
- InsertMenu ((**simplemenus).menuhand, 0);
- HUnlock (simplemenus);
-
- menu ("File", "New", (ProcPtr)itemdisable);
- menu ("Edit", "Undo", stnop);
- menu ("Edit", "-", (ProcPtr)itemdisable);
- menu ("Edit", "Cut/X", stnop);
- menu ("Edit", "Copy/C", stnop);
- menu ("Edit", "Paste/V", stnop);
- menu ("Edit", "Clear", stnop);
- }
-
- /* Local procedure */
-
- #ifndef LIGHTSPEED
- gottrans ()
- {
- char prog[80];
- char *argv[3];
- if ( getfile("APPL", prog) ) {
- argv[1] = NULL;
- execv (prog, argv);
- }
- }
- #endif
-
- /* Local procedure */
-
- gotquit ()
- {
- ExitToShell();
- }
-
- /* This routine is for the Windows menu Item. The Windows menu is
- set up when new windows are added. It is used to bring forward and
- bring into view windows that may be under other windows or have been
- sent hiding by a click on their close box.
- */
-
- /* Local procedure */
-
- showawindow(name) /* show the named window */
- char *name;
- {
- WindowPtr foo;
- foo = windowpoint(name); /* get its window pointer */
- if ( foo ) {
- ShowWindow(foo); /* show it on the screen */
- SetPort (foo); /* set further output to it */
- if ( foo != FrontWindow()) /* if it isn't active, */
- SelectWindow (foo); /* activate it */
- }
- }
-
- /* Local procedure */
-
- winddatum **wdatum(windpt) /* return Handle to window data */
- WindowPtr windpt;
- {
- winddatum **wind, **temp;
-
- if (firstwind) return ((winddatum **) 0L);
- wind = simplewinds;
- HLock (wind);
-
- while ( ((**wind).wptr != windpt) && (**wind).next) {
- temp = wind;
- wind = (winddatum **) (**wind).next;
- HUnlock (temp);
- HLock (wind);
- }
-
- if ( (**wind).wptr == windpt) {
- HUnlock (wind);
- return (wind);
- } else {
- HUnlock (wind);
- return ((winddatum **) 0L); /* zero if not found */
- }
- }
-
- /* Local procedure */
-
- runruns(event) /* run all the installed run procedures */
- EventRecord *event; /* returns number of routines run */
- {
- int i=0;
- WindowPtr saveport;
- GetPort (&saveport);
- while ( simpleruns[i] )
- (*(simpleruns[i++])) (event);
- SetPort (saveport);
- return(i);
- }
-
- /* Local procedure */
-
- stdialog( question, answer, type) /* a general dialog displayer */
- char *question;
- char *answer;
- int type; /* type: 1=prompt, 2=message */
- {
- DialogPtr dialog; /* dialog reference */
- Handle Item, items; /* handles for the dialog items */
- Rect screen, box; /* rectangles for dialog/items */
- int dtype, hit, canc; /* Item type and which was hit */
- char tempanswer[255]; /* address where answer is */
-
- items = NewHandle (512L);/* get memory for items list */
- HLock (items); /* lock it down */
- if (type == 1)
- BlockMove (quesd, *items, (long) QUESN * 2L);
- else
- BlockMove (messd, *items, (long) MESSN * 2L);
- SetRect (&screen, 103, 50, 409, 137);
-
- /* For LIGHTSPEED, use a lower case d and upper case B and P */
- /* for DBoxProc. Megamax conversion utility does this wrong. */
-
- dialog = NewDialog (0L, &screen, "", 0, dBoxProc, -1L, 0, 0L, items);
- GetDItem (dialog, 2, &dtype, &Item, &box);
- ctop (question);
- SetIText (Item, question); /* set Item#2 text */
- ptoc (question);
- if (type == 1) { /* set default answer */
- GetDItem (dialog, 3, &dtype, &Item, &box);
- ctop (answer);
- SetIText (Item, answer);
-