home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / midi / synth_librarian / sources / synthlib / comtask.c < prev    next >
C/C++ Source or Header  |  1994-08-20  |  24KB  |  1,000 lines

  1. /**************************************************************/
  2. /* $VER: ComTask.c Revison 1.1                                */
  3. /* This file is part of Synth Librarian v1.1                  */
  4. /* ©1993-1994 Jean-Alexis MONTIGNIES                          */
  5. /* This file must not be distributed modified or separatly    */
  6. /* without my written permission                              */
  7. /* See Synth_Librarian.guide for more details                 */
  8. /**************************************************************/
  9. /* Synth librarian part for I/O with serial device */
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <dos/dos.h>
  13. #include <devices/serial.h>
  14. #include <devices/timer.h>
  15. #include <libraries/locale.h>
  16. #include <proto/locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include "SynthLib.h"
  22. #define CATCOMP_NUMBERS
  23. #define CATCOMP_BLOCK
  24. #include "ComTaskLoc.h"
  25.  
  26. struct LocaleInfo CLi;
  27. struct MsgPort *ComPort;
  28. struct IOExtSer *SerialIN,*SerialOUT;
  29. struct timerequest *TimerIO;
  30. ULONG (* DriverPtr)(ULONG *StepPtr,struct Exchange *Ex);
  31. extern struct MsgPort *MainPort;
  32.  
  33. ULONG Status;
  34. struct Exchange Ex;
  35. ULONG Step;
  36. ULONG MessagePosted;
  37. char FormattedText[64];
  38. BOOL INUsed,OUTUsed,DelayUsed,MessageUsed[2];
  39. struct LibMessage MessTab[2];
  40.  
  41. STRPTR GetString(struct LocaleInfo *li, LONG stringNum,char *CatCompBlock);
  42. void Request(char *DataPtr,ULONG RemainChars);
  43. void ClearSerial(void);
  44. int ParseINResults( BOOL EOFMode );
  45. void Send(char *DataPtr,ULONG Lenght);
  46. void SendMessage(UBYTE Code,APTR DataPtr);
  47. void ReqDelay(ULONG Duration);
  48. void Advance(void);
  49. ULONG InitComTask(void);
  50. void CloseComTask(void);
  51. void CallDriver(void);
  52.  
  53. ULONG InitComTask(void)
  54.  {ULONG ReturnCode=0;
  55.   UBYTE i;
  56.   BYTE io_Error=0;
  57.  
  58.   ComPort=NULL;
  59.   SerialIN=NULL;
  60.   SerialOUT=NULL;
  61.   TimerIO=NULL;
  62.   DriverPtr=NULL;
  63.   MessagePosted=0;
  64.   INUsed=OUTUsed=DelayUsed=FALSE;
  65.  
  66.   if(CLi.li_LocaleBase=LocaleBase)
  67.    {CLi.li_Catalog=OpenCatalog(NULL,"SynthLib/ComTask.catalog",OC_BuiltInLanguage,"english",TAG_DONE);
  68.    }
  69.   if (!(ComPort=CreatePort(0,0)))
  70.     ReturnCode=CODE_INIT_NOPORT;
  71.  
  72.   for (i=0;i<2;i++)
  73.    {MessTab[i].LMess.mn_ReplyPort=ComPort;
  74.     MessTab[i].Code=i;
  75.     MessageUsed[i]=FALSE;
  76.    }
  77.  
  78.   if (!ReturnCode)
  79.     if (!(SerialIN=(struct IOExtSer *)CreateExtIO(ComPort,sizeof(struct IOExtSer))))
  80.       ReturnCode=CODE_INIT_NOSERIAL;
  81.  
  82.   if (!ReturnCode)
  83.     if (OpenDevice(SERIALNAME,0L,(struct IORequest *)SerialIN,0))
  84.      {ReturnCode=CODE_INIT_NOSERIAL;
  85.       io_Error=SerialIN->IOSer.io_Error;
  86.       DeleteExtIO((struct IORequest *)SerialIN);
  87.       SerialIN=NULL;
  88.      }
  89.  
  90.   if (!ReturnCode)
  91.    {
  92. #ifndef DEBUG_SL
  93.     SerialIN->io_RBufLen=0x2000;
  94. #else
  95.     SerialIN->io_RBufLen=0x20000;
  96. #endif
  97.     SerialIN->io_Baud=31250;
  98.     SerialIN->io_TermArray.TermArray0=0xFFFEF8F7;
  99.     SerialIN->io_TermArray.TermArray1=0xF0F0F0F0;
  100.     SerialIN->io_SerFlags|=SERF_RAD_BOOGIE|SERF_EOFMODE|SERF_XDISABLED;
  101.     SerialIN->IOSer.io_Command=SDCMD_SETPARAMS;
  102.     ReturnCode=DoIO((struct IORequest *)SerialIN);
  103.     if (ReturnCode)
  104.       ReturnCode=CODE_INIT_NOSERIAL;
  105.    }
  106.  
  107.   if (!ReturnCode)
  108.     if (SerialOUT=(struct IOExtSer *)CreateExtIO(ComPort,sizeof(struct IOExtSer)))
  109.      {memcpy(SerialOUT,SerialIN,sizeof(struct IOExtSer));
  110.       SerialOUT->io_SerFlags&=~SERF_EOFMODE;
  111.      }
  112.      else
  113.      {
  114.       ReturnCode=CODE_INIT_NOSERIAL;
  115.       io_Error=SerialOUT->IOSer.io_Error;
  116.      }
  117.  
  118.   if (!ReturnCode)
  119.     if (!(TimerIO=(struct timerequest *)CreateExtIO(ComPort,sizeof(struct timerequest))))
  120.       ReturnCode=CODE_INIT_NOTIMER;
  121.  
  122.   if (!ReturnCode)
  123.     if (OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)TimerIO,0))
  124.      {ReturnCode=CODE_INIT_NOTIMER;
  125.       DeleteExtIO((struct IORequest *)TimerIO);
  126.       TimerIO=NULL;
  127.      }
  128.  
  129.   if (!ReturnCode)
  130.       SendMessage(MSGCODE_INFO,GetString(&CLi,ReturnCode,ComCatBlock));
  131.  
  132.   if (ReturnCode)
  133.     if (ReturnCode==CODE_INIT_NOSERIAL && (io_Error>0))
  134.       SendMessage(MSGCODE_ERROR,GetString(&CLi,MSG_SERIAL-1+SerialIN->IOSer.io_Error,ComCatBlock));
  135.      else
  136.       SendMessage(MSGCODE_ERROR,GetString(&CLi,MSG_INIT-1+ReturnCode,ComCatBlock));
  137.  
  138.   return(ReturnCode);
  139.  }
  140.  
  141. void CloseComTask()
  142.  {struct Message *Mess;
  143.  
  144.   if (DriverPtr)
  145.    {Step=STEP_EXIT_DRIVER;
  146.     DriverPtr(&Step,&Ex);
  147.    }
  148.  
  149.   if (TimerIO && DelayUsed)
  150.     AbortIO((struct IORequest *)TimerIO);
  151.  
  152.   if (SerialOUT && OUTUsed)
  153.     AbortIO((struct IORequest *)SerialOUT);
  154.  
  155.   if (SerialIN && INUsed)
  156.     AbortIO((struct IORequest *)SerialIN);
  157.  
  158.   if (ComPort)
  159.    {do
  160.      {while(Mess=GetMsg(ComPort))
  161.         if (Mess->mn_ReplyPort!=ComPort)
  162.          {if (Mess->mn_ReplyPort==MainPort)
  163.             ((struct LibMessage *)Mess)->Code|=MSGCODE_IGNORED;
  164.           ReplyMsg(Mess);
  165.          }
  166.          else
  167.          {MessagePosted--;
  168.           if (Mess!=SerialIN && Mess!=SerialOUT)
  169.             if (((struct LibMessage *)Mess)->Code>=2)
  170.               FreeMem((APTR)Mess,sizeof(struct LibMessage));
  171.          }
  172.       if (MessagePosted)
  173.         Wait(1<<(ComPort->mp_SigBit));
  174.      }
  175.     while(MessagePosted);
  176.    }
  177.  
  178.   if (TimerIO)
  179.    {CloseDevice((struct IORequest *)TimerIO);
  180.     DeleteExtIO((struct IORequest *)TimerIO);
  181.    }
  182.  
  183.   if (SerialIN)
  184.    {CloseDevice((struct IORequest *)SerialIN);
  185.     DeleteExtIO((struct IORequest *)SerialIN);
  186.    }
  187.  
  188.   if (SerialOUT)
  189.     DeleteExtIO((struct IORequest *)SerialOUT);
  190.  
  191.   MessTab[0].LMess.mn_ReplyPort=NULL;
  192.   MessTab[0].Code=MSGCODE_BYE;
  193.   MessTab[0].Data=GetString(&CLi,MSG_BYE,ComCatBlock);
  194.   PutMsg(MainPort,(struct Message *)&MessTab[0]);
  195.  
  196.   if (LocaleBase)
  197.    {CloseCatalog(CLi.li_Catalog);
  198.    }
  199.  
  200.   Wait(SIGBREAKF_CTRL_C);
  201.  
  202.   if (ComPort)
  203.    {while(Mess=GetMsg(ComPort))
  204.      {if (Mess->mn_ReplyPort==MainPort)
  205.         ((struct LibMessage *)Mess)->Code|=MSGCODE_IGNORED;
  206.       ReplyMsg(Mess);
  207.      }
  208.  
  209.     DeletePort(ComPort);
  210.    }
  211.  }
  212.  
  213. void Request(char *DataPtr,ULONG RemainChars)
  214.  {
  215.   SerialIN->IOSer.io_Command=CMD_READ;
  216.   SerialIN->IOSer.io_Length=min(RemainChars,128);
  217.   SerialIN->IOSer.io_Data=(APTR)DataPtr;
  218.   SerialIN->IOSer.io_Flags|=IOF_QUICK;
  219.   BeginIO((struct IORequest *)SerialIN);
  220.  
  221.   if (!(SerialIN->IOSer.io_Flags & IOF_QUICK))
  222.    {MessagePosted++;
  223.     INUsed=TRUE;
  224.    }
  225.  }
  226.  
  227. void Send(char *DataPtr,ULONG Length)
  228.  {SerialOUT->IOSer.io_Command=CMD_WRITE;
  229.   SerialOUT->IOSer.io_Length=Length;
  230.   SerialOUT->IOSer.io_Data=(APTR)DataPtr;
  231.   SerialOUT->IOSer.io_Flags|=IOF_QUICK;
  232.   BeginIO((struct IORequest *)SerialOUT);
  233.  
  234.   if (!(SerialOUT->IOSer.io_Flags & IOF_QUICK))
  235.    {MessagePosted++;
  236.     OUTUsed=TRUE;
  237.    }
  238.  }
  239.  
  240. void ClearSerial()
  241.  {SerialIN->IOSer.io_Command=CMD_CLEAR;
  242.   SerialIN->IOSer.io_Flags|=IOF_QUICK;
  243.   BeginIO((struct IORequest *)SerialIN);
  244.  
  245.   if (!(SerialIN->IOSer.io_Flags & IOF_QUICK))
  246.    {MessagePosted++;
  247.     INUsed=TRUE;
  248.    }
  249.  }
  250.  
  251. void SendMessage(UBYTE Code,APTR DataPtr)
  252.  {struct LibMessage *Mess;
  253.  
  254.   if (Code>=2)
  255.    { /* Message très important */
  256.     Mess=(struct LibMessage *)AllocMem(sizeof(struct LibMessage),0L);
  257.     if (Mess)
  258.      {Mess->LMess.mn_Length=sizeof(struct LibMessage);
  259.       Mess->LMess.mn_ReplyPort=ComPort;
  260.       Mess->Code=Code;
  261.       Mess->Data=DataPtr;
  262.  
  263.       PutMsg(MainPort,(struct Message *)Mess);
  264.       MessagePosted++;
  265.      }
  266.      else
  267.      /* Cas désespéré, on efface les données recues */
  268.      {
  269.       if (Code==MSGCODE_DATA || (Code==MSGCODE_REQ_RESULT && DataPtr))
  270.        {
  271.         FreeMem(((struct DumpNode *)DataPtr)->Dump,((struct DumpNode *)DataPtr)->DumpLength);
  272.         if (((struct DumpNode *)DataPtr)->Data)
  273.           FreeMem(((struct DumpNode *)DataPtr)->Data,((struct DumpNode *)DataPtr)->DataLength);
  274.         FreeMem(DataPtr,sizeof(struct DumpNode));
  275.        }
  276.      }
  277.    }
  278.    else
  279.      /* Message moins important */
  280.     if (!MessageUsed[Code])
  281.      {MessageUsed[Code]=TRUE;
  282.       MessTab[Code].Data=DataPtr;
  283.  
  284.       PutMsg(MainPort,(struct Message *)(&MessTab[Code]));
  285.       MessagePosted++;
  286.      }
  287.  }
  288.  
  289. void ReqDelay(ULONG Duration)
  290.  { if (DelayUsed)
  291.     {AbortIO((struct IORequest *)TimerIO);
  292.      WaitIO((struct IORequest *)TimerIO);
  293.      MessagePosted--;
  294.     }
  295.  
  296.    TimerIO->tr_node.io_Command=TR_ADDREQUEST;
  297.    TimerIO->tr_time.tv_secs=Duration/1000000;
  298.    TimerIO->tr_time.tv_micro=Duration%1000000;
  299.    SendIO((struct IORequest *)TimerIO