ACQ2XX_API
Frame.cpp
Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------- */
00002 /* file Frame.h                                                      */
00003 /* ------------------------------------------------------------------------- */
00004 /*   Copyright (C) 2009 Peter Milne, D-TACQ Solutions Ltd
00005  *                      <Peter dot Milne at D hyphen TACQ dot com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of Version 2 of the GNU General Public License
00009     as published by the Free Software Foundation;
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00019 /* ------------------------------------------------------------------------- */
00020 
00021 /** @file Frame.cpp implements ACQ streaming frame. 
00022  * Refs: see ICD for frame definition
00023 */
00024 
00025 #include "local.h"
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 
00030 #include "Frame.h"
00031 
00032 #include "AcqType.h"
00033 
00034 #include "acq32busprot.h"
00035 
00036 Frame::Frame(const AcqType& _acqType) :
00037         acqType(_acqType),
00038         nchan(acqType.getNumChannels()), 
00039         word_size(acqType.getWordSize()) 
00040 {
00041 }
00042 
00043 Frame::~Frame()
00044 {
00045 }
00046 static int nx_bit(unsigned short tag)
00047 {
00048         return (tag&0x8000) != 0;
00049 }
00050 
00051 void Frame::computeStartSampleNumber(void)
00052 {
00053         int ibit;
00054 
00055         startSampleNumber = 0;
00056 
00057         for (ibit = 0; ibit < 48; ++ibit){
00058                 if (nx_bit(tags[ibit])){
00059                         startSampleNumber |= (1 << ibit);
00060                 }
00061         }
00062         dbg(2, "startSampleNumber %lld", startSampleNumber);
00063 }
00064 
00065 unsigned short Frame::getExtra(void) const
00066 {
00067         unsigned short extra = 0;
00068         int ibit;
00069 
00070         for (ibit = 0; ibit < 16; ++ibit){
00071                 if (nx_bit(tags[48+ibit])){
00072                         extra |= (1 <<ibit);
00073                 }
00074         }
00075         return extra;   
00076 }
00077 
00078 unsigned char Frame::getDIO(void) const
00079 {
00080         return tags[MFX_DIO] & 0x00ff;
00081 }
00082 
00083 int Frame::appendTag(int isam, unsigned short tag)
00084 {
00085         unsigned short id = tag & 0x00ff;
00086         int isam_tag = (tag >> 8) & 0x3f;
00087 
00088         switch(isam){
00089         case 0:
00090                 if (id == 0xfe){
00091                         break;
00092                 }else{
00093                         err("TAG is NOT TAG1 0x%04x", tag);
00094                         goto error;
00095                 }
00096         case 1:
00097                 if (id == 0xed){
00098                         break;
00099                 }else{
00100                         err("TAG is NOT TAG2 0x%04x", tag);
00101                         goto error;
00102                 }
00103         }
00104 
00105         if (isam_tag != isam){
00106                 err("ISAM_TAG != isam");
00107                 goto error;
00108         }
00109         
00110         dbg(3, "STORING tag %02d %04x %d", isam, tag, nx_bit(tag));
00111 
00112         tags[isam] = tag;
00113 
00114         switch(isam){
00115         case FRAME_SAMPLES-1:
00116                 state = FULL;
00117                 computeStartSampleNumber();             
00118         }
00119 
00120         return 0;
00121 
00122 error:
00123         state = INVALID;
00124         return -1;
00125 
00126 }
00127 
00128 int Frame::buildFrame(unsigned id, Frame *frame, void *raw, int nraw) {
00129         frame->id = id;
00130         frame->state = EMPTY;
00131         return frame->fillData(raw, nraw);
00132 }