예제 #1
0
void IRCSocket::OnConnect()
{
    //TODO: Send authentication info from main IRCBOT class
    //Log.Success("Connected to: %s.", RetreiveClientIP());
    SendLine("NICK %s", _Bot.NickName.c_str());
    SendLine("USER %s * 0 : %s", _Bot.NickName.c_str(), _Bot.NickName.c_str());
}
예제 #2
0
파일: softmodem.cpp 프로젝트: Avin15/dospad
void CSerialModem::SendRes(ResTypes response) {
	char const * string;Bitu code;
	switch (response)
	{
		case ResNONE:		return;
		case ResOK:			string="OK"; code=0; break;
		case ResERROR:		string="ERROR"; code=4; break;
		case ResRING:		string="RING"; code=2; break;
		case ResNODIALTONE: string="NO DIALTONE"; code=6; break;
		case ResNOCARRIER:	string="NO CARRIER" ;code=3; break;
		case ResCONNECT:	string="CONNECT 57600"; code=1; break;
      default:;
	}
	
	if(doresponse!=1) {
		if(doresponse==2 && (response==ResRING || 
			response == ResCONNECT || response==ResNOCARRIER)) return;
		if(numericresponse) SendNumber(code);
		else SendLine(string);

		//if(CSerial::CanReceiveByte())	// very fast response
		//	if(rqueue->inuse() && CSerial::getRTS())
		//	{ Bit8u rbyte =rqueue->getb();
		//		CSerial::receiveByte(rbyte);
		//	LOG_MSG("Modem: sending byte %2x back to UART2",rbyte);
		//	}

		LOG_MSG("Modem response: %s", string);
	}
}
예제 #3
0
void
SMTPMessage::SendLine
	(
	const JCharacter* line
	)
{
	JString sLine(line);
	SendLine(sLine);
}
예제 #4
0
파일: InMapDraw.cpp 프로젝트: eXLabT/spring
void CInMapDraw::MouseMove(int x, int y, int dx,int dy, int button)
{
	float3 pos = GetMouseMapPos();
	if (pos.x < 0) {
		return;
	}
	if (mouse->buttons[SDL_BUTTON_LEFT].pressed && lastLineTime < gu->gameTime - 0.05f) {
		SendLine(pos, lastPos, false);
		lastLineTime = gu->gameTime;
		lastPos = pos;
	}
	if (mouse->buttons[SDL_BUTTON_RIGHT].pressed) {
		SendErase(pos);
	}

}
예제 #5
0
파일: softmodem.cpp 프로젝트: Avin15/dospad
void CSerialModem::DoCommand() {
	cmdbuf[cmdpos] = 0;
	cmdpos = 0;			//Reset for next command
	upcase(cmdbuf);
	LOG_MSG("Command sent to modem: ->%s<-\n", cmdbuf);
	/* Check for empty line, stops dialing and autoanswer */
	if (!cmdbuf[0]) {
		reg[0]=0;	// autoanswer off
		return;
	}
	//else {
		//MIXER_Enable(mhd.chan,false);
	//	dialing = false;
	//	SendRes(ResNOCARRIER);
	//	goto ret_none;
	//}
	/* AT command set interpretation */

	if ((cmdbuf[0] != 'A') || (cmdbuf[1] != 'T')) {
		SendRes(ResERROR);
		return;
	}
	if (strstr(cmdbuf,"NET0")) {
		telnetmode = false;
		SendRes(ResOK);
		return;
	}
	else if (strstr(cmdbuf,"NET1")) {
		telnetmode = true;
		SendRes(ResOK);
		return;
	}

	char * scanbuf = &cmdbuf[2];
	while (1) {
		// LOG_MSG("loopstart ->%s<-",scanbuf);
		char chr = GetChar(scanbuf);
		switch (chr) {
		case 'D': { // Dial
			char * foundstr=&scanbuf[0];
			if (*foundstr=='T' || *foundstr=='P') foundstr++;
			// Small protection against empty line and long string
			if ((!foundstr[0]) || (strlen(foundstr)>100)) {
				SendRes(ResERROR);
				return;
			}
			char* helper;
			// scan for and remove spaces; weird bug: with leading spaces in the string,
			// SDLNet_ResolveHost will return no error but not work anyway (win)
			while(foundstr[0]==' ') foundstr++;
			helper=foundstr;
			helper+=strlen(foundstr);
			while(helper[0]==' ') {
				helper[0]=0;
				helper--;
			}
			if (strlen(foundstr) >= 12) {
				// Check if supplied parameter only consists of digits
				bool isNum = true;
				for (Bitu i=0; i<strlen(foundstr); i++)
					if (foundstr[i] < '0' || foundstr[i] > '9') isNum = false;
				if (isNum) {
					// Parameter is a number with at least 12 digits => this cannot
					// be a valid IP/name
					// Transform by adding dots
					char buffer[128];
					Bitu j = 0;
					for (Bitu i=0; i<strlen(foundstr); i++) {
						buffer[j++] = foundstr[i];
						// Add a dot after the third, sixth and ninth number
						if (i == 2 || i == 5 || i == 8)
							buffer[j++] = '.';
						// If the string is longer than 12 digits,
						// interpret the rest as port
						if (i == 11 && strlen(foundstr)>12)
							buffer[j++] = ':';
					}
					buffer[j] = 0;
					foundstr = buffer;
				}
			}
			Dial(foundstr);
			return;
		}
		case 'I': // Some strings about firmware
			switch (ScanNumber(scanbuf)) {
			case 3: SendLine("DosBox Emulated Modem Firmware V1.00"); break;
			case 4: SendLine("Modem compiled for DosBox version " VERSION); break;
			}
			break;
		case 'E': // Echo on/off
			switch (ScanNumber(scanbuf)) {
			case 0: echo = false; break;
			case 1: echo = true; break;
			}
			break;
		case 'V':
			switch (ScanNumber(scanbuf)) {
			case 0: numericresponse = true; break;
			case 1: numericresponse = false; break;
			}
			break;
		case 'H': // Hang up
			switch (ScanNumber(scanbuf)) {
			case 0:
				if (connected) {
					SendRes(ResNOCARRIER);
					EnterIdleState();
					return;
				}
				// else return ok
			}
			break;
		case 'O': // Return to data mode
			switch (ScanNumber(scanbuf)) {
			case 0:
				if (clientsocket) {
					commandmode = false;
					return;
				} else {
					SendRes(ResERROR);
					return;
				}
			}
			break;
		case 'T': // Tone Dial
		case 'P': // Pulse Dial
			break;
		case 'M': // Monitor
		case 'L': // Volume
			ScanNumber(scanbuf);
			break;
		case 'A': // Answer call
			if (waitingclientsocket) {
				AcceptIncomingCall();
			} else {
				SendRes(ResERROR);
				return;
			}
			return;
		case 'Z': { // Reset and load profiles
			// scan the number away, if any
			ScanNumber(scanbuf);
			if (clientsocket) SendRes(ResNOCARRIER);
			Reset();
			break;
		}
		case ' ': // skip space
			break;
		case 'Q': {
			// Response options
			// 0 = all on, 1 = all off,
			// 2 = no ring and no connect/carrier in answermode
			Bitu val = ScanNumber(scanbuf);	
			if(!(val>2)) {
				doresponse=val;
				break;
			} else {
				SendRes(ResERROR);
				return;
			}
		}
		case 'S': { // Registers	
			Bitu index=ScanNumber(scanbuf);
			if(index>=SREGS) {
				SendRes(ResERROR);
				return; //goto ret_none;
			}
			
			while(scanbuf[0]==' ') scanbuf++;	// skip spaces
			
			if(scanbuf[0]=='=') {	// set register
				scanbuf++;
				while(scanbuf[0]==' ') scanbuf++;	// skip spaces
				Bitu val = ScanNumber(scanbuf);
				reg[index]=val;
				break;
			}
			else if(scanbuf[0]=='?') {	// get register
				SendNumber(reg[index]);
				scanbuf++;
				break;
			}
			//else LOG_MSG("print reg %d with %d",index,reg[index]);
		}
		break;
		case '&': { // & escaped commands
			char cmdchar = GetChar(scanbuf);
			switch(cmdchar) {
				case 'K': {
					Bitu val = ScanNumber(scanbuf);
					if(val<5) flowcontrol=val;
					else {
						SendRes(ResERROR);
						return;
					}
					break;
				}
				case '\0':
					// end of string
					SendRes(ResERROR);
					return;
				default:
					LOG_MSG("Modem: Unhandled command: &%c%d",cmdchar,ScanNumber(scanbuf));
					break;
			}
			break;
		}
		case '\\': { // \ escaped commands
			char cmdchar = GetChar(scanbuf);
			switch(cmdchar) {
				case 'N':
					// error correction stuff - not emulated
					if (ScanNumber(scanbuf) > 5) {
						SendRes(ResERROR);
						return;
					}
					break;
				case '\0':
					// end of string
					SendRes(ResERROR);
					return;
				default:
					LOG_MSG("Modem: Unhandled command: \\%c%d",cmdchar, ScanNumber(scanbuf));
					break;
			}
			break;
		}
		case '\0':
			SendRes(ResOK);
			return;
		default:
			LOG_MSG("Modem: Unhandled command: %c%d",chr,ScanNumber(scanbuf));
			break;
		}
	}
}
void *MuninNodeClient::Entry()
{	
  int ret = 0;
  static const int BUFFER_SIZE = 8096;
  char buffer[BUFFER_SIZE] = {0};
  char hostname[64] = {0};
  int len = 0;
  
  ret = gethostname(hostname, 64);
  if (ret) {
    _Module.LogEvent("Failed to get hostname!");
  }
  ret = _snprintf(buffer, BUFFER_SIZE, "# munin node at %s\n", hostname);

  // we simply send this string to the client
  ret = SendLine(buffer);

  while (!TestDestroy()) {
    // Now wait for a reply
    ret = RecvLine(buffer, BUFFER_SIZE);
    if (ret == -1) {
      // Recieve error, connection reset?
      break;
    }
    // Remove newlines
    char *buffer2 = strstr(buffer, "\n");
    if (buffer2 != NULL) {
      *buffer2 = NULL;
    }
    buffer2 = strstr(buffer, "\r");
    if (buffer2 != NULL) {
      *buffer2 = NULL;
    }
    if (strstr(buffer, "quit") == buffer) {
      break;

    } else if (strstr(buffer, "version") == buffer) {   
      // Read in Version Infomation
      CFileVersionInfo ver;
      ver.Open(GetModuleHandle(NULL));
      ret = _snprintf(buffer, BUFFER_SIZE, "munin node on %s version: Munin Node for Windows %i.%i.%i\n", hostname, ver.GetFileVersionMajor(), ver.GetFileVersionMinor(), ver.GetFileVersionQFE());
      ret = SendLine(buffer);        

    } else if (strstr(buffer, "nodes") == buffer) {        
      // This version only supports one node
      ret = _snprintf(buffer, BUFFER_SIZE, "%s\n.\n", hostname);
      ret = SendLine(buffer);

    } else if (strstr(buffer, "list") == buffer) {     
      memset(buffer, 0, BUFFER_SIZE);
      m_PluginManager->FillPluginList(buffer, BUFFER_SIZE);
      ret = SendLine(buffer);

    } else if (strstr(buffer, "config") == buffer) {      
      MuninNodePlugin *plugin = m_PluginManager->LookupPlugin(buffer);
      if (plugin != NULL) {
        memset(buffer, 0, BUFFER_SIZE);
        ret = plugin->GetConfig(buffer, BUFFER_SIZE);
        if (ret < 0) {
          ret = SendLine("# Unknown Error\n.\n");
        } else {
          ret = SendLine(buffer);
        }
      } else {
        ret = SendLine("# Unknown service\n.\n");
      }

    } else if (strstr(buffer, "fetch") == buffer) {    
      MuninNodePlugin *plugin = m_PluginManager->LookupPlugin(buffer);
      if (plugin != NULL) {
        memset(buffer, 0, BUFFER_SIZE);
        ret = plugin->GetValues(buffer, BUFFER_SIZE);
        if (ret < 0) {
          ret = SendLine("# Unknown Error\n.\n");
        } else {
          ret = SendLine(buffer);
        }
      } else {
        ret = SendLine("# Unknown service\n.\n");
      }

    } else {
      ret = SendLine("# Unknown command. Try list, nodes, config, fetch, version or quit\n");
    }
    
  }
  
  return 0;
}
예제 #7
0
파일: serial.cpp 프로젝트: abiaozsh/MyCode
int main(void) {
	
	TCCR0A = 0;
	TCCR0B = 3;//1/64
	TIMSK0 = 0;

	TCCR1A = 0;
	TCCR1B = 5;//1/1024 (16000000/1024=15625)tick/s
	TCCR1C = 0;
	TIMSK1 = 0;
		
	DDR_595_6 = 0xFC;//B11111100;
	DDR_CLK_ON;
	DDR_OE_ON;
	DDR_PNP1_ON;
	DDR_LED1_ON;
	DDR_LED2_ON;
	
	while(true)
	{
		//init/////////////////////////////////////////////////////////////////////////////////////////////////////////////
		TCNT1 = 0;//0.000064 s/tick
		for(uint16_t i=0;i<48;i++)
		{
			for(uint8_t t = 0;t<6;t++)
			{
				buff[i*8+t] = pgm_read_byte_near(data+i*6+t);
			}
			Conv(buff+i*8);
		}
		//part1///////////////////////////////////////////////////////////////////////////////////////////////////////////
		while(TCNT1<100);//0.0064s
		for(uint8_t m = 0;m<10;m++)
		{
			for(uint16_t i=0;i<48;i++)
			{
				TCNT0 = 0;
				SendLine(buff+(i<<3));
				while(TCNT0<187);
				PORT_PNP1_ON;//PNP on H
				PORT_OE_OFF;//OE on L
				while(TCNT0<230);
				PORT_LED2_ON;//PNP on H
				while(TCNT0<235);
				PORT_PNP1_OFF;//PNP disable L
				PORT_OE_ON;//OE disable H
				PORT_LED2_OFF;//PNP disable L
			}
		}
		//while(TCNT1<30000);//part2///////////////////////////////////////////////////////////////////////////////////////////

		//while(TCNT1<30000+100);//0.0064s
		for(uint8_t m = 0;m<10;m++)
		{
			for(uint16_t i=0;i<48;i++)
			{
				TCNT0 = 0;
				SendLine(buff+((47-i)<<3));
				while(TCNT0<187);
				PORT_PNP1_ON;//PNP on H
				PORT_OE_OFF;//OE on L
				while(TCNT0<230);
				PORT_LED1_ON;//PNP on H
				while(TCNT0<235);
				PORT_PNP1_OFF;//PNP disable L
				PORT_OE_ON;//OE disable H
				PORT_LED1_OFF;//PNP disable L
			}
		}
		
		//while(TCNT1<60000);
    }
}
예제 #8
0
void
SMTPMessage::SendNextData()
{
	if (itsCurrentMode == kHello)
		{
		JCharacter hostname[255];
		gethostname(hostname, 255);
		JString senddata = "HELO ";
		senddata += hostname;
		SendLine(senddata);
		senddata += "\n";
		GMGetSMTPDebugDir()->AddText(senddata);
		}
	else if (itsCurrentMode == kFrom)
		{
		JString senddata = "MAIL FROM: " + *itsFrom;
		SendLine(senddata);
		senddata += "\n";
		GMGetSMTPDebugDir()->AddText(senddata);
		}
	else if (itsCurrentMode == kTo)
		{
		assert(itsCurrentIndex <= itsToNames->GetElementCount());
		JString senddata;
		JString name = *(itsToNames->NthElement(itsCurrentIndex));
		senddata = "RCPT TO: " + name;
		SendLine(senddata);
		senddata += "\n";
		GMGetSMTPDebugDir()->AddText(senddata);
		}
	else if (itsCurrentMode == kCc)
		{
		assert(itsCurrentIndex <= itsCcNames->GetElementCount());
		JString senddata;
		JString name = *(itsCcNames->NthElement(itsCurrentIndex));
		senddata = "RCPT TO: " + name;
		SendLine(senddata);
		senddata += "\n";
		GMGetSMTPDebugDir()->AddText(senddata);
		}
	else if (itsCurrentMode == kBcc)
		{
		assert(itsCurrentIndex <= itsBccNames->GetElementCount());
		JString senddata;
		JString name = *(itsBccNames->NthElement(itsCurrentIndex));
		senddata = "RCPT TO: " + name;
		SendLine(senddata);
		senddata += "\n";
		GMGetSMTPDebugDir()->AddText(senddata);
		}
	else if (itsCurrentMode == kDataHeader)
		{
		JString senddata = "DATA";
		SendLine(senddata);
		senddata += "\n";
		GMGetSMTPDebugDir()->AddText(senddata);
		}
	else if (itsCurrentMode == kData)
		{
		JString from	 = itsFullName + " <" + itsSMTPUser + ">";
		if (!from.IsEmpty())
			{
			JString senddata = "From: " + from;
			SendLine(senddata);
			senddata += "\n";
			GMGetSMTPDebugDir()->AddText(senddata);
			AppendToOutbox("From: ");
			AppendToOutbox(from);
			AppendToOutbox("\n");
			}

		struct utsname buf;
		uname(&buf);

		JString senddata = "X-Mailer: Arrow ";
		senddata += GMGetVersionNumberStr();
		senddata += JString(" (X11; ") + buf.sysname + JString(" ") +  + JString(buf.release);
		senddata += JString("; ") + buf.machine + JString(")");
		SendLine(senddata);
		AppendToOutbox(senddata);
		AppendToOutbox("\n");
		GMGetSMTPDebugDir()->AddText(senddata);

		senddata = "To: ";
		assert(itsToNames->GetElementCount() > 0);
		senddata += *(itsToNames->NthElement(1));
		for (JSize i = 2; i <= itsToNames->GetElementCount(); i++)
			{
			senddata += ",\n\t" + *(itsToNames->NthElement(i));
			}
		SendLine(senddata);
		AppendToOutbox(senddata);
		AppendToOutbox("\n");
		GMGetSMTPDebugDir()->AddText(senddata);

		if (!itsReplyTo->IsEmpty())
			{
			senddata = "Reply-To: " + *itsReplyTo;
			SendLine(senddata);
			GMGetSMTPDebugDir()->AddText(senddata);
			}

		if (itsCcNames->GetElementCount() != 0)
			{
			JSize count = itsCcNames->GetElementCount();
			senddata = "Cc: ";
			senddata += *(itsCcNames->NthElement(1));
			for (JSize i = 2; i <= count; i++)
				{
				senddata += ",\n\t" + *(itsCcNames->NthElement(i));
				}
			SendLine(senddata);
			AppendToOutbox(senddata);
			AppendToOutbox("\n");
			GMGetSMTPDebugDir()->AddText(senddata);
			}

		if (!itsSubject->IsEmpty())
			{
			senddata = "Subject: " + *itsSubject;
			SendLine(senddata);
			AppendToOutbox("Subject: ");
			AppendToOutbox(*itsSubject);
			AppendToOutbox("\n");
			GMGetSMTPDebugDir()->AddText(senddata);
			}

		if (!itsAddedHeaders->IsEmpty())
			{
			SendMultiLine(itsAddedHeaders);
			}

		SendLine("");
		AppendToOutbox("\n");

		GMGetSMTPDebugDir()->AddText("\nData not shown ...\n");
		AppendToOutbox(*itsData);
		SendData();
		AppendToOutbox("\n");
		AppendToOutbox("\n");

		SendLine("");
		SendLine(".");
		SendLine("QUIT");
		WriteToOutbox();
		}
}
예제 #9
0
void dodeopall(struct user *user, char *tail)
{
  unsigned char flags;
  struct reggedchannel *chanptr;
  char *channel;

#if defined(HORRIBLE_DEOPALL_HACK) || defined(HAVE_CLEARMODE)
  char buf[512];
#endif /* HORRIBLE_DEOPALL_HACK */

  channel = tail;
  SeperateWord(tail);

  if (channel == NULL) {
    NoticeToUser(user, "Usage: deopall #channel");
    return;
  }

  if ((chanptr = GetChannelPointer(channel)) == NULL) {
    NoticeToUser(user, "Unknown channel %s.", channel);
    return;
  }

  flags = GetChannelFlags(user->authedas, chanptr);

  /* Check for MASTER or OWNER flag */
  if (!(flags & (CFLAG_MASTER | CFLAG_OWNER)) || IsSuspended(chanptr)) {
    /* No flags -- perhaps they are an oper? */
    if (!((user->oper) && (user->authedas->authlevel > 200))) {
      /* Nope, not an oper either */
      NoticeToUser(user, "Sorry, you need the +m or +n flag on %s to use deopall.", channel);
      return;
    }
  }

  /* Do the actual deopall */
  /* erm, how? */

/*
  sprintf(buf,"%s GL * +%s 0 :Clearing channel\r\n",my_numeric,channel);
  SendLine(buf);
*/

#ifdef HAVE_CLEARMODE
  sprintf(buf, "%sAAA CM %s o\r\n", my_numeric, channel);
  SendLine(buf);
  sprintf(buf, "%s M %s +o %sAAA\r\n", my_numeric, channel, my_numeric);
  SendLine(buf);
#else /* !HAVE_CLEARMODE */
# ifdef HORRIBLE_DEOPALL_HACK
  if (chanptr->timestamp > 0) {
    chanptr->timestamp--;
#  ifdef SIT_ON_CHANNELS
    /* If we're sitting on the channel, we can just rejoin it with a new timestamp... */
    if (IsJoined(chanptr)) {
      sprintf(buf, "%sAAA L %s\r\n", my_numeric, channel);
      SendLine(buf);
    }
    SetJoined(chanptr);
    sprintf(buf, "%s B %s %ld %sAAA:o\r\n", my_numeric, channel, chanptr->timestamp, my_numeric);
#  else /* !SIT_ON_CHANNELS */
    /* We're not sitting on the channel, so we send a burst with a _fake_ user */
    sprintf(buf, "%s B %s %ld %sAAB:o\r\n", my_numeric, channel, chanptr->timestamp, my_numeric);
#  endif /* SIT_ON_CHANNELS */
    SendLine(buf);
  } else {
    NoticeToUser(user, "Sorry, cannot deopall on %s at this time.", channel);
    return;
  }
# else /* !HORRIBLE_DEOPALL_HACK */
  if (NULL == Optr) {
    NoticeToUser(user, "Sorry, cannot deopall on %s at this time.", channel);
    return;
  } else {
    MessageToUser(Optr, "deopall -l %s", channel);
/*** This is no longer required
    MessageToUser(Optr, "opchan %s %s", channel, my_nick); */
  }
# endif /* HORRIBLE_DEOPALL_HACK */
#endif /* HAVE_CLEARMODE */

  Log("DeopAll: %s (%s) requested deopall in %s", user->nick, user->authedas->authname, channel);
  NoticeToUser(user, "Done.");
}
예제 #10
0
void LineProcessing()
{
#ifdef THRESHOLD_DETECTION
    uint32_t sum = 0;
    float contrast;
    uint16_t min,max;
    uint16_t weight;
    int white = 0;

    contrast = CalculateContrast(&min,&max);

//        if(contrast<MINIMUM_CONTRAST)
//        	return;

    whiteThreshold = (uint16_t)(max - 0.40*(max-min));
    blackThreshold = (uint16_t)(min + 0.20*(max-min));

    //CorrectExposure();

    //filter result
    j = 0;
    for(i=0; i<15; i++) {
        Result[i] = whiteThreshold;
    }
    for (i = 15; i < 128-15; i++) { // ignore the first and last 16 pixels in the camera frame
        weight = (uint16_t)fabsf(127 - (i-line_middle));
        weight = weight*weight;
        if (Result[i] <= blackThreshold) { //black
            Result[i] = blackThreshold;
            sum += i*weight;
            j += weight;
        }
        else if(Result[i]>=whiteThreshold) { //white
            Result[i] = whiteThreshold;
            white++;
        }
    }
    for(i=128-15; i<128; i++) {
        Result[i] = whiteThreshold;
    }

    SendLine();
    //Check where the center of line is
    if(white>5) { //j>0){
        line_middle = (uint8_t)(line_middle + 0.95*((1.0*sum)/j-line_middle));
        CorrectExposure();
        /**
        if(midMag != 0 && pMidMag != 0 && ppMidMag == 0){
        	int mag_avg = (midMag+pMidMag+ppMidMag)/3;
        	if(Result[line_middle] < mag_avg){
        		exposureTime -= EXPOSURE_STEP;
            	if(exposureTime<LOWER_EXPOSURE_TIME){
            		exposureTime = LOWER_EXPOSURE_TIME;
            	}
        	}
        	else if(Result[line_middle] > mag_avg){
        		exposureTime += EXPOSURE_STEP;
        		if(exposureTime>UPPER_EXPOSURE_TIME){
        			exposureTime = UPPER_EXPOSURE_TIME;
        		}
        	}

        }

        ppMidMag = pMidMag;
        pMidMag = midMag;
        midMag = Result[line_middle];
        **/
    }

#endif
#ifdef CONSECUTIVE_DETECTION
    uint32_t sum = 0;
    uint32_t w_sum = 0;
    float contrast;
    uint16_t min,max;
    int consect_points = 0;
    uint16_t weight;
    contrast = CalculateContrast(&min,&max);

//        if(contrast<MINIMUM_CONTRAST)
//        	return;

    whiteThreshold = (uint16_t)(max - 0.40*(max-min));
    blackThreshold = (uint16_t)(min + 0.20*(max-min));

    //CorrectExposure();

    //filter result
    j = 0;
    for(i=0; i<128; i++) {
        if(i<15 || i>113) Result[i] = whiteThreshold;
    }

    for (i=15; i<113; i++) { // ignore the first and last 16 pixels in the camera frame
        if (Result[i] <= blackThreshold) { //black
            for(j=i; Result[j]<blackThreshold && j<113; j++);
            if((j-i)>6 ) { //|| (j-i)<20){
                for(; i<j; i++) {
                    Result[i] = blackThreshold;
                    weight = (uint16_t)fabsf(127 - (i-line_middle));
                    weight = weight*weight;
                    sum += i*weight;
                    w_sum += weight;
                }
            }
        }
        else if(Result[i]>=whiteThreshold) { //white
            Result[i] = whiteThreshold;
        }
    }

    SendLine();
    //Check where the center of line is
    if(w_sum>0) {
        line_middle = (uint8_t)(line_middle + 0.95*((1.0*sum)/w_sum-line_middle));
        //CorrectExposure();
        /**
        if(midMag != 0 && pMidMag != 0 && ppMidMag == 0){
        	int mag_avg = (midMag+pMidMag+ppMidMag)/3;
        	if(Result[line_middle] < mag_avg){
        		exposureTime -= EXPOSURE_STEP;
            	if(exposureTime<LOWER_EXPOSURE_TIME){
            		exposureTime = LOWER_EXPOSURE_TIME;
            	}
        	}
        	else if(Result[line_middle] > mag_avg){
        		exposureTime += EXPOSURE_STEP;
        		if(exposureTime>UPPER_EXPOSURE_TIME){
        			exposureTime = UPPER_EXPOSURE_TIME;
        		}
        	}

        }

        ppMidMag = pMidMag;
        pMidMag = midMag;
        midMag = Result[line_middle];
        **/
    }

#endif
#ifdef WEIGHT_DETECTION
    uint8_t i = 0;
    uint32_t sum=0, weightedSum=0;
    //invert the result
    for(i=0; i<128; i++)
    {
        Result[i]=5000-Result[i];
    }

    for(i=15; i<128-15; i++)
    {
        sum += Result[i];
        weightedSum += i*Result[i];
    }

    line_middle = (uint8_t)(line_middle + 0.92*((1.0*weightedSum)/(1.0*sum))-line_middle);
    //line_middle = (uint8_t)((1.0*weightedSum)/(1.0*sum));
    ppMidMag = pMidMag;
    pMidMag = midMag;
    midMag = Result[line_middle];

    if(midMag != 0 && pMidMag != 0 && ppMidMag == 0) {
        int mag_avg = (midMag+pMidMag+ppMidMag)/3;
        if(Result[line_middle] < mag_avg) {
            exposureTime -= EXPOSURE_STEP;
        }
        else if(Result[line_middle] > mag_avg) {
            exposureTime += EXPOSURE_STEP;
        }
        if(exposureTime>UPPER_EXPOSURE_TIME)
        {
            exposureTime = UPPER_EXPOSURE_TIME;
        }
        else if(exposureTime<LOWER_EXPOSURE_TIME)
        {
            exposureTime = LOWER_EXPOSURE_TIME;
        }
    }

#endif
#ifdef THREE_POINTS_DETECTION
    uint8_t middlePoint,rightPoint,leftPoint;
    uint16_t maxVariation=0;
    uint8_t maxMiddlePoint=0;

    leftPoint=0;
    middlePoint=RANGE;
    rightPoint=2*RANGE;

    for(i=0; i<128; i++)
    {
        if(maxVariation<((Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint])))
        {
            maxVariation = (Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint]);
            maxMiddlePoint = middlePoint;
        }

        leftPoint++;
        if(leftPoint>127)
            leftPoint=127;

        middlePoint++;
        if(middlePoint>127)
            middlePoint=127;

        rightPoint++;
        if(rightPoint>127)
            rightPoint=127;
    }

    line_middle = (uint8_t)(line_middle + 0.92*(maxMiddlePoint-line_middle));

#endif

#ifdef THREE_POINTS_THRESHOLD_DETECTION

    uint8_t middlePoint,rightPoint,leftPoint;
    uint16_t maxVariation=0;
    uint8_t maxMiddlePoint=0;
    uint8_t byte;
    uint8_t ws,we;
    uint16_t min,max;
    float lineContrast;



    leftPoint=15;
    middlePoint=15+RANGE;
    rightPoint=15+2*RANGE;

    for(i=15; i<128-15; i++)
    {
        if(maxVariation<((Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint])))
        {
            maxVariation = (Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint]);
            maxMiddlePoint = middlePoint;
        }

        leftPoint++;
        if(leftPoint>127-15)
            leftPoint=127-15;

        middlePoint++;
        if(middlePoint>127-15)
            middlePoint=127-15;

        rightPoint++;
        if(rightPoint>127-15)
            rightPoint=127-15;
    }

    lineContrast = CalculateContrast(&min,&max);
    byte = (uint8_t)(100*lineContrast);
    fifo_write(&out->fifo,&byte,1);

    if(((lineContrast>MINIMUM_CONTRAST)&&(Result[maxMiddlePoint]<=dynamicThreshold))||(faultyLine>10))
    {
        line_middle = (uint8_t)(line_middle + 0.92*(maxMiddlePoint-line_middle));
        dynamicThreshold = (uint16_t)(min+0.2*(max-min));
        faultyLine = 0;
    }
    else
    {
        faultyLine++;
    }
#endif

#ifdef THREE_POINTS_WEIGHTED_DETECTION

    uint8_t middlePoint,rightPoint,leftPoint;
    uint32_t maxVariation=0;
    uint32_t newVariation=0;
    uint8_t maxMiddlePoint=0;
    uint8_t byte;
    uint8_t weight;
    float lineContrast;

    leftPoint=15;
    middlePoint=15+RANGE;
    rightPoint=15+2*RANGE;

    for(i=15; i<128-15; i++)
    {
        if(middlePoint > line_middle)
            weight = (uint8_t)(127 - ((int8_t)middlePoint-(int8_t)line_middle));
        else
            weight = 127 - ((int8_t)line_middle-(int8_t)middlePoint);

        newVariation = weight*((Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint]));

        if(maxVariation<newVariation)
        {
            maxVariation = newVariation;
            maxMiddlePoint = middlePoint;
        }

        leftPoint++;
        if(leftPoint>127-15)
            leftPoint=127-15;

        middlePoint++;
        if(middlePoint>127-15)
            middlePoint=127-15;

        rightPoint++;
        if(rightPoint>127-15)
            rightPoint=127-15;
    }

    lineContrast = CalculateContrast(0,0);
    byte = (uint8_t)(100*lineContrast);
    fifo_write(&out->fifo,&byte,1);

    if((lineContrast>MINIMUM_CONTRAST))
    {
        line_middle = (uint8_t)(line_middle + 0.75*(maxMiddlePoint-line_middle));
    }

#endif

#ifdef JEREMY_DETECTION
//        int linesFound = 0;
//        int thresshold = 100;
//        int k;
//        int left, right, middle;
//
//        SendLine();
//
//        for(i=14;i<128-14;i+=2){
//        	if(Result[i+2]-Result[i] < -thresshold){
//        		left = i;
//        		for(j=i+2; j<128-14; j+=2){
//        			if(Result[j+2]-Result[j] > thresshold){
//        				if(linesFound == 0){
//        					middle = j;
//        					linesFound++;
//        				}
//        				else{
//        					if(Result[j] < Result[middle])
//        					middle = j;
//        				}
//        			}
//        			for(k=j+2;k<128-14; k+=2){
//        				if(Result[j+2]-Result[j] < thresshold){
//        					right = k;
//        				}
//        			}
//        		}
//        		i = k+2;
//        	}
//        }

    uint8_t middlePoint,rightPoint,leftPoint;
    uint16_t maxVariation=0;
    uint8_t maxMiddlePoint=0;
    uint8_t byte;
    int thresshold = 100;
    float lineContrast;

    leftPoint=15;
    middlePoint=15+RANGE;
    rightPoint=15+2*RANGE;

    for(i=15; i<128-15; i++)
    {
        if(maxVariation<((Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint])))
        {
            maxVariation = (Result[rightPoint]-Result[middlePoint])-(Result[middlePoint]-Result[leftPoint]);
            maxMiddlePoint = middlePoint;
        }

        leftPoint++;
        if(leftPoint>127-15)
            leftPoint=127-15;

        middlePoint++;
        if(middlePoint>127-15)
            middlePoint=127-15;

        rightPoint++;
        if(rightPoint>127-15)
            rightPoint=127-15;
    }
    if(midMag == 0 || pMidMag == 0 || ppMidMag == 0) {
        ppMidMag = pMidMag;
        pMidMag = midMag;
        midMag = Result[maxMiddlePoint];
        line_middle = (uint8_t)maxMiddlePoint;
    }
    else if(fabsf(Result[maxMiddlePoint] -((midMag+pMidMag+ppMidMag)/3)) <= 4*thresshold) {
        ppMidMag = pMidMag;
        pMidMag = midMag;
        midMag = Result[maxMiddlePoint];
        line_middle = (uint8_t)maxMiddlePoint;
    }
#endif

#ifdef MINIMUM_DETECTION
    float contrast;
    uint8_t minPosition;
    int8_t ws,we;

    ws = (int8_t)(line_middle - SEARCH_WINDOW);
    if(ws<30)
        ws=30;
    we = (int8_t)(line_middle + SEARCH_WINDOW);
    if(we>128-30)
        we=128-30;

    //contrast = CalculateContrast(0,0);

    minPosition = ws;

    for(i=ws; i<we; i++)
    {
        if(Result[minPosition]>Result[i])
            minPosition = i;
    }

    line_middle = (uint8_t)minPosition;
//        if(midMag == 0 || pMidMag == 0 || ppMidMag == 0){
//        	ppMidMag = pMidMag;
//        	pMidMag = midMag;
//        	midMag = Result[minPosition];
//        	line_middle = (uint8_t)minPosition;
//        }
//        else if(fabsf(Result[minPosition] -((midMag+pMidMag+ppMidMag)/3)) <= 500){
//        	ppMidMag = pMidMag;
//        	pMidMag = midMag;
//        	midMag = Result[minPosition];
//        	line_middle = (uint8_t)minPosition;
//        }

#endif

}