示例#1
0
void updateDisp(BYTE side) {

	// check if we are holding text
	if(holdText[side]) {
		// check if the hold time has passed
		if(millis - holdTimer[side] > HOLD_TIME) {
			holdText[side] = FALSE;
		}
		// carry out blinking of text
		else if(millis - blinkTimer[side] > BLINK_TIME) {
			// redisplay the text
			if(blinkStates[side]) {
				blinkStates[side] = FALSE;
				updateText(side, displayStates);
				blinkTimer[side] = millis;
			}
			// blank the displays
			else {
				blinkStates[side] = TRUE;
				blank_display(side);
				blinkTimer[side] = millis;
			}
		}
	}
	// data is being displayed
	else {
		if(millis - refreshTime[side] > REFRESH_TIME) {
			refreshTime[side] = millis;
			write_num(chan[displayStates[side]], d_place_arr[displayStates[side]], side);
		}
	}

	return;
}
示例#2
0
int encrypt_message(
    const char* input_filename,
    const char* output_filename,
    rsa_private_key_t* key
) {
    int status_code = 0;

    // Open input file
    FILE* input_file = fopen(input_filename, "r");
    if(!input_file) {
        fprintf(stderr, "Failed to open input file.\n");
        status_code = 1;
        goto done;
    }

    // Open file for encrypted output
    FILE* output_file = fopen(output_filename, "w");
    if(!output_file) {
        fprintf(stderr, "Failed to open output file.\n");
        status_code = 1;
        goto cleanup_input_file;
    }

    // Initialize GMP data
    mpz_t input_byte, output_num;
    mpz_init(input_byte);
    mpz_init(output_num);

    int input_num = 0;
    while(1) {
        // Read character
        input_num = fgetc(input_file);
        if(feof(input_file)) {
            break;
        }

        // Encrypt
        mpz_set_si(input_byte, input_num);
        mpz_powm(output_num, input_byte, key->d, key->n); // out = (in ^ d) % n

        // Write to output file
        if(write_num(output_file, output_num)) {
            fprintf(stderr, "Failed to write encrypted number to encrypted file.\n");
            status_code = 1;
            goto cleanup_gmp_nums;
        }
    }

    // Clean up
cleanup_gmp_nums:
    mpz_clear(output_num);
    mpz_clear(input_byte);
    fclose(output_file);
cleanup_input_file:
    fclose(input_file);

done:
    return status_code;
}
示例#3
0
void write_num_n_on_level(unsigned int loglevel, long num)
{
	unsigned char c = '\n';

	if (loglevel > current_loglevel)
		return;

	write_num(num);
	sys_write(current_logfd, &c, sizeof(c));
}
示例#4
0
int main(void)
{
  BigNum a, b, c, q, r;
  int count;
  int limit = 100000;

  srand(time(NULL));
  for (count = 0; count < limit; count++) {
    /* generate random a and b */
    int l1, l2, i;
    clear(a);
    clear(b);
    l1 = rand() % (MAX_DIGIT-1);
    l2 = rand() % (MAX_DIGIT-1);
    for (i = 0; i < l1; i++) {
      a[i] = rand() % BASE;
    }
    b[0] = rand() % (BASE-1) + 1;
    for (i = 1; i < l2; i++) {
      b[i] = rand() % BASE;
    }
    if (count % 1000 == 0) {
      printf("count = %d\n", count);
      fflush(stdout);
    }
    divide(a, b, q, r);
    mult(b, q, c);
    add(c, r, c);
    if (compare(a, c) != 0) {
      printf("count = %d\n", count);
      printf("a = "); write_num(a); printf("\n");
      printf("b = "); write_num(b); printf("\n");
      printf("q = "); write_num(q); printf("\n");
      printf("r = "); write_num(r); printf("\n");
      printf("b*q+r = "); write_num(c); printf("\n");
      exit(1);
    }
  }
  printf("All tests passed.\n");
  return 0;
}
void Fragment_CompressCoordinate(CvMat *R, vector<char> &Q, int frag_num)
{
	int unit_len = (int)ceil((double)R->width / frag_num);
	int start_i;
	uchar *ptr;
	bool left_zero;
	int c; //前一次出现非零的位置
	int b;

	for(int h = 0; h < R->height; h++)
	{
		uchar *ptr = (uchar *)(R->data.ptr + h * R->step);

		for(int fi = 0; fi < frag_num; fi++)
		{
			left_zero = true;
			c = -1; //前一次出现非零的位置
			b = 0;

			start_i = fi * unit_len;
			for(int ui = 0; ui < unit_len; ui++)
			{
				int ci = start_i + ui;

				if(ci >= R->width)
					break;//最后一段长度可能不够unit_len

				if(ptr[ci] == 0)
				{
					left_zero = true;
					continue;
				}

				left_zero = false;

				write_num(Q, ptr[ci]);

				if(c == unit_len - 2)//最后两个是编码点,则最后一个不用编码距离
				{
					c = c + 1;
					continue;
				}
				write_dist(Q, b, ui, c, unit_len);

				c = ui;
			}
			if(left_zero == true)
				Q.push_back('0');//段剩余全部为0
		}
	}

	_coor_table_size = Q.size();
}
示例#6
0
文件: menu.cpp 项目: emufreak/iAmiga
void write_num_inv(int x, int y, int v)
{
  SDL_Rect dest;
  int i,l=1;

  for(i=10;i<1000000;i*=10)
	if (v/i)
		l++;
  	else
		break;
  	
  dest.x = (x * 8) -2 ;
  dest.y = (y * 8) /*10*/ - 2;
  dest.w = (l * 8) + 4;
  dest.h = 12;

  SDL_FillRect(text_screen, &dest, menu_inv_color);

  write_num(x, y, v);
}
示例#7
0
文件: gensi.hpp 项目: endriff/sam2p
 inline B &operator <<(unsigned long long n) { write_num(n); return*this; }
示例#8
0
文件: gensi.hpp 项目: endriff/sam2p
 inline B& operator <<(unsigned int   n) { write_num((unsigned long)n); return*this; }
示例#9
0
文件: gensi.hpp 项目: endriff/sam2p
 inline B& operator <<(  signed short n) { write_num((signed long)n); return*this; }
示例#10
0
文件: gensi.hpp 项目: endriff/sam2p
 inline Writable &operator <<(signed long long n) { write_num(n); return*this; }
示例#11
0
文件: gensi.hpp 项目: endriff/sam2p
 inline Writable& operator <<(unsigned short n) { write_num((unsigned long)n); return*this; }
示例#12
0
文件: gensi.hpp 项目: endriff/sam2p
 inline Writable& operator <<(  signed int   n) { write_num((signed long)n); return*this; }
示例#13
0
void main(void) {

	/*************************
	 * Variable Declarations *
	 *************************/

    BYTE radio_sw[2], drs_over_sw[2], fan_over_sw[2], fuel_map_sw[2], paddle_l_sw[2], paddle_r_sw[2];
    BYTE ADLmsg[8];
	BYTE cycleStates[2], intensity;
	unsigned int bounceTimer[2];
	unsigned int CAN_tmr;


    /*********************
     * Oscillator Set-Up *
     *********************/
    #ifdef INTERNAL
            // OSCTUNE
            OSCTUNEbits.INTSRC = 0;		// Internal Oscillator Low-Frequency Source Select (1 for 31.25 kHz from 16MHz/512 or 0 for internal 31kHz)
            OSCTUNEbits.PLLEN = 1;		// Frequency Multiplier PLL Select (1 to enable)
            OSCTUNEbits.TUN5 = 0;		// Fast RC Oscillator Frequency Tuning (seems to be 2's comp encoding)
            OSCTUNEbits.TUN4 = 0;		// 011111 = max
            OSCTUNEbits.TUN3 = 0;		// ... 000001
            OSCTUNEbits.TUN2 = 0;		// 000000 = center (running at calibrated frequency)
            OSCTUNEbits.TUN1 = 0;		// 111111 ...
            OSCTUNEbits.TUN0 = 0;		// 100000

            // OSCCCON
            OSCCONbits.IDLEN = 1;		// Idle Enable Bit (1 to enter idle mode after SLEEP instruction else sleep mode is entered)
            OSCCONbits.IRCF2 = 1;		// Internal Oscillator Frequency Select Bits
            OSCCONbits.IRCF1 = 1;		// When using HF, settings are:
            OSCCONbits.IRCF0 = 1;		// 111 - 16 MHz, 110 - 8MHz (default), 101 - 4MHz, 100 - 2 MHz, 011 - 1 MHz
            OSCCONbits.SCS1 = 0;
            OSCCONbits.SCS0 = 0;

            // OSCCON2
            OSCCON2bits.MFIOSEL = 0;

            while(!OSCCONbits.HFIOFS);	// wait for stable clock

    #else
            // OSCTUNE
            OSCTUNEbits.INTSRC = 0;		// Internal Oscillator Low-Frequency Source Select (1 for 31.25 kHz from 16MHz/512 or 0 for internal 31kHz)
            OSCTUNEbits.PLLEN = 1;		// Frequency Multiplier PLL Select (1 to enable)

            // OSCCCON
            OSCCONbits.SCS1 = 0;		// select configuration chosen oscillator
            OSCCONbits.SCS0 = 0;		// SCS = 00

            // OSCCON2
            OSCCON2bits.MFIOSEL = 0;

            while(!OSCCONbits.OSTS);	// wait for stable external clock
    #endif

    /*********************
     * Peripherals Setup *
     *********************/

	// turn on and configure the A/D converter module
	OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_4_TAD, ADC_CH0 & ADC_INT_OFF, ADC_REF_VDD_VDD & ADC_REF_VDD_VSS & ADC_NEG_CH0);
	ANCON0 = 0b00100111;	// AN0 - 2 and AN5 are analog
	ANCON1 = 0x00;          // rest are digital
	TRISAbits.TRISA0 = INPUT;
	TRISAbits.TRISA1 = INPUT;
	TRISAbits.TRISA2 = INPUT;
	TRISAbits.TRISA5 = INPUT;

	// turn on and configure the TIMER1 oscillator
	OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_128);
	WriteTimer0(0x82);		// load timer register
	millis = 0;				// clear milliseconds count
	INTCONbits.TMR0IE = 1;	// turn on timer0 interupts

    // SPI setup
    SSPSTATbits.CKE = 1;		// SPI Clock Select, 1 = transmit on active to idle
    SSPCON1bits.CKP = 0;		// Clock Polarity Select, 0 = low level is idle state
    SSPCON1bits.SSPM = 0b1010;	// Clk Frequecy (Note: FOSC = 64MHz)
    SSPCON1bits.SSPEN = 1;      // SPI Enable, 1 enables

    // SPI pin I/O setup
    TRISCbits.TRISC3 = OUTPUT;	// SCK
    TRISCbits.TRISC5 = OUTPUT;	// SDO
    TRISDbits.TRISD3 = OUTPUT;	// CS
    CS = 1;

	// driver set up
	intensity = 0x0F;
	driver_write(DISP_MODE, NORMAL);		// leave test mode
	driver_write(SHUTDOWN, SHUTDOWN_OFF);	// leave shutdown mode
	driver_write(INTENSITY, intensity);		// set brightness to highest
    driver_write(SCAN, FULL_SCAN);          // Set scan to all digits
    driver_write(DECODE, NO_DECODE);        // Decoding disabled

	// set displays to display zero
	write_gear(0);
	write_num(0, 2, LEFT);
	write_num(0, 2, RIGHT);

	// intialize states
	cycleStates[LEFT] = CYCLE_L;
	cycleStates[RIGHT] = CYCLE_R;
	holdText[LEFT] = holdText[RIGHT] = TRUE;
	refreshTime[LEFT] = refreshTime[RIGHT] = holdTimer[LEFT] = holdTimer[RIGHT] =
						blinkTimer[LEFT] = blinkTimer[RIGHT] = millis;
	displayStates[LEFT] = OIL_T;
	displayStates[RIGHT] = ENGINE_T;

	ECANInitialize();		// setup ECAN

    // interrupts setup
	INTCONbits.GIE = 1;		// Global Interrupt Enable (1 enables)
	INTCONbits.PEIE = 1;	// Peripheral Interrupt Enable (1 enables)
	RCONbits.IPEN = 0;		// Interrupt Priority Enable (1 enables)

	TRISCbits.TRISC6 = OUTPUT;	// programmable termination
	TERM_LAT = FALSE;

	while(1) {

		// check for change in button state
		if(cycleStates[LEFT] != CYCLE_L & millis - bounceTimer[LEFT] > BOUNCE_TIME) {
			// save new state
			cycleStates[LEFT] = CYCLE_L;
			bounceTimer[LEFT] = millis;
			// only change display if button is low
			if(!cycleStates[LEFT]) {
				if(++displayStates[LEFT] == NUM_CHAN)
					displayStates[LEFT] = 0;
				// put the appropriate text on the displays and
				// get the current time for timing logic
				updateText(LEFT, displayStates);
				holdText[LEFT] = TRUE;
				blinkTimer[LEFT] = holdTimer[LEFT] = millis;
			}
		}
		if(cycleStates[RIGHT] != CYCLE_R  & millis - bounceTimer[RIGHT] > BOUNCE_TIME) {
			cycleStates[RIGHT] = CYCLE_R;
			bounceTimer[RIGHT] = millis;
			if(!cycleStates[RIGHT]) {
				if(++displayStates[RIGHT] == NUM_CHAN)
					displayStates[RIGHT] = 0;
				updateText(RIGHT, displayStates);
				holdText[RIGHT] = TRUE;
                blinkTimer[RIGHT] = holdTimer[RIGHT] = millis;
			}
		}

		// update left and right displays with text or numerical data
		updateDisp(LEFT);
		updateDisp(RIGHT);
		write_gear(gear);

        // radio button
        if(!RADIO) {
            radio_sw[0] = 0x13;
            radio_sw[1] = 0x88;
        }
        else
            *(int *)radio_sw = 0;
#if 0
        // paddle switches
        if(PADDLE_L) {
            paddle_l_sw[0] = 0x13;
            paddle_l_sw[1] = 0x88;
        }
        else
            *(int *)paddle_l_sw = 0;
        if(PADDLE_R) {
            paddle_r_sw[0] = 0x13;
            paddle_r_sw[1] = 0x88;
        }
        else
            *(int *)paddle_r_sw = 0;
#endif
        // DRS override switch
        if(DRS_OVER) {
            drs_over_sw[0] = 0x13;
            drs_over_sw[1] = 0x88;
        }
        else
            *(int *)drs_over_sw = 0;
        // fan override switch
        if(FAN_OVER) {
            fan_over_sw[0] = 0x13;
            fan_over_sw[1] = 0x88;
        }
        else
            *(int *)fan_over_sw = 0;
        // fuel map switch
        if(FUEL_MAP) {
            fuel_map_sw[0] = 0x13;
            fuel_map_sw[1] = 0x88;
        }
        else
            *(int *)fuel_map_sw = 0;

		if(millis - CAN_tmr > CAN_PER) {
			CAN_tmr = millis;
			// send out the first three sampled switches
			ADLmsg[0] = 0x00;
			ADLmsg[1] = 0x00;
			ADLmsg[ADL1] = radio_sw[0];
			ADLmsg[ADL1 + 1] = radio_sw[1];
			ADLmsg[ADL2] = fan_over_sw[0];
			ADLmsg[ADL2 + 1] = fan_over_sw[1];
			ADLmsg[ADL3] = fuel_map_sw[0];
			ADLmsg[ADL3 + 1] = fuel_map_sw[1];
			ECANSendMessage(ADLid, ADLmsg, 8, ECAN_TX_STD_FRAME | ECAN_TX_NO_RTR_FRAME | ECAN_TX_PRIORITY_1);
			// send out first three rotary encoders
			ADLmsg[0] = 0x01;
			ADLmsg[1] = 0x00;
			ADLsample(ADLmsg, ADL4, LAUNCH_ROT);
			ADLsample(ADLmsg, ADL5, TRAC_ROT);
			ADLsample(ADLmsg, ADL6, DRS_ROT);
			ECANSendMessage(ADLid, ADLmsg, 8, ECAN_TX_STD_FRAME | ECAN_TX_NO_RTR_FRAME | ECAN_TX_PRIORITY_1);
		}



	} // end main loop

	return;
}
示例#14
0
void write_num_on_level(unsigned int loglevel, long num)
{
	if (loglevel > current_loglevel)
		return;
	write_num(num);
}
示例#15
0
void LcdStateRunning::drawDefault()
{
    const State &state = State::get();
    const ChargeMonitor &chargeMonitor = ChargeMonitor::get();
    DS3231 &rtc = DS3231::get();

    uint8_t amps = state.max_amps_limit;
    if (ChargeMonitor::get().isCharging())
        amps = chargeMonitor.chargeCurrent() / 1000;

    lcd.move(0,0);
    write_time(lcd, rtc);
    lcd.write(' ');
    write_num(lcd, rtc.readTemp());
    lcd.write(0xDF);

    lcd.write(' ');
    lcd.write(CustomCharacters::SEPARATOR);
    lcd.write(' ');

    if (amps)
        write_num(lcd, amps);
    else
        lcd.write("--");
    lcd.write('A');

    lcd.move(0,1);

    switch (state.j1772)
    {
        case J1772Pilot::UNKNOWN:
        case J1772Pilot::STATE_E:
            lcd.setBacklight(LCD16x2::RED);
            center_P(lcd, STR_ERROR_STATE);
            break;

        case J1772Pilot::STATE_A:
            lcd.setBacklight(LCD16x2::GREEN);
            if (chargeMonitor.chargeDuration() == 0)
            {
                center_P(lcd, STR_NOT_CONNECTED);
            } else {
                lcd.write_P(STR_CHARGED);
                if (display_state.get())
                {
                    lcd.write(' ');
                    write_duration(lcd, chargeMonitor.chargeDuration());
                    spaces(lcd, 3);
                } else {
                    write_kwh(lcd, chargeMonitor.wattHours());
                }
            }
            break;

        case J1772Pilot::STATE_B:
            lcd.setBacklight(LCD16x2::GREEN);
            center_P(lcd, STR_CONNECTED);
            break;

        case J1772Pilot::STATE_C:
        {
            lcd.setBacklight(LCD16x2::CYAN);
            if (display_state.get())
                lcd.write_P(STR_CHARGING);
            else
                write_kwh(lcd, chargeMonitor.wattHours());
            lcd.write(CustomCharacters::SEPARATOR);
            write_duration(lcd, chargeMonitor.chargeDuration());
            break;
        }

        case J1772Pilot::STATE_D:
            lcd.setBacklight(LCD16x2::RED);
            center_P(lcd, STR_VENT_REQUIRED);
            break;

        case J1772Pilot::DIODE_CHECK_FAILED:
            lcd.setBacklight(LCD16x2::RED);
            center_P(lcd, STR_DIODE_CHECK_FAILED);
            break;

        case J1772Pilot::NOT_READY:
        case J1772Pilot::IMPLAUSIBLE:
            break;
    }
}