Example #1
0
int main(void)
{
    sys_init();
    oled_init();

    timer_init(TIMER1);
    timer_set_ms(TIMER1,16);
    
    // Initial parameters
    uint8_t x = OLED_WIDTH / 2;     // x initial position
    uint8_t y = OLED_HEIGHT / 2;    // y initial position
    uint8_t dx = 1;                 // x speed
    uint8_t dy = 1;                 // y speed

    
    oled_clearDisplay();
    oled_drawPixel(x,y);
    oled_render();
    
    timer_start(TIMER1);

    while(1)
    {
        if(oledUpdate)
        {
            LED ^= 1;
            oled_clearDisplay();
            move(&x, &y, &dx, &dy);
            oled_render();
            oledUpdate = 0;
        }
    }
    return (0);
}
Example #2
0
int main(int argc, char* argv[])
{
  if( !oled_init() )
  {
    printf("could not find oled\n");
    return 1;
  }

  //send logo
  oled_send_fb( up3dwifilogo );


//TEST
  oled_fb_clear();
  oled_fb_writestring( 0, 0, "Hello World!", 1 );
  oled_fb_writestring( 0,16, "Hello World!", 1 );
  oled_fb_writestring( 0,32, "Hello World!", 1 );
  oled_fb_writestring( 0,48, "Hello World!", 1 );
/*
  oled_fb_setfont(ArialMT_Plain_10);
  oled_fb_writestring( 0,0, "Hello World!", 1 );
  oled_fb_setfont(ArialMT_Plain_16);
  oled_fb_writestring( 0,11, "Hello World!", 1 );
  oled_fb_setfont(DejaVu_Sans_Condensed_Plain_12);
  oled_fb_writestring( 0,30, "Hello World!", 1 );
*/
  oled_fb_update();

  //oled_deinit();
  return 0;
}
void ICACHE_FLASH_ATTR user_init(void) {
	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	os_printf("SDK version:%s\n", system_get_sdk_version());
	i2c_init();
#ifdef ESP8266OLED
	oled_init();
#endif
	key_and_gpio_init();
#ifdef ESP8266DOOR
	door_switch_init();
#endif
	wifi_set_event_handler_cb(wifi_handle_event_cb);
	mqttstart();
#ifdef ESP8266ENVINFO
	task_send_environment_info_init();
#endif
#ifdef ESP8266CARDREAD
	pn532_init();
	pn532_cb_event_init();
#endif
	struct station_config s_staconf;
	wifi_station_get_config_default(&s_staconf);
	if (os_strlen(s_staconf.ssid) == 0) {
		wifi_set_opmode(STATION_MODE);
		smartconfig_set_type(SC_TYPE_ESPTOUCH);
		smartconfig_start(smartconfig_done);
	}
}
Example #4
0
int main (void)
{
  draw_lcd_t lcd;

  // outputs
  GPIOSetDir(OLED_SSEL_PORT, OLED_SSEL_PIN, GPIO_OUTPUT);
  GPIOSetDir(OLED_DC_PORT, OLED_DC_PIN, GPIO_OUTPUT);
  GPIOSetDir(OLED_RESET_PORT, OLED_RESET_PIN, GPIO_OUTPUT);

  GPIOSetValue(OLED_SSEL_PORT, OLED_SSEL_PIN, 1);
  GPIOSetValue(OLED_DC_PORT, OLED_DC_PIN, 1);
  GPIOSetValue(OLED_RESET_PORT, OLED_RESET_PIN, 1);

  SSP0Init(6000000);

  printf("\nInitializing oled driver...");
  oled_init(&lcd);

  rainbow(&lcd);

  //enter forever loop -
  while (1) {
  }

  return 0;
}
Example #5
0
void SYS_Initialize ( void* data ) {
    /* Core Processor Initialization */
    SYS_CLK_Initialize( NULL );
    sysObj.sysDevcon = SYS_DEVCON_Initialize(SYS_DEVCON_INDEX_0, (SYS_MODULE_INIT*) & sysDevconInit);
    SYS_DEVCON_PerformanceConfig(SYS_CLK_SystemFrequencyGet());
    SYS_DEVCON_JTAGDisable();
    SYS_PORTS_Initialize();
    ANSELB = SYS_ANSELB;    //JT - old ansel doesn't do shit

    /* Board Support Package Initialization */
    BSP_Initialize();

    

    /* Initialize Drivers */
    sysObj.drvUsart0 = DRV_USART_Initialize(DRV_USART_INDEX_0, (SYS_MODULE_INIT *) & drvUsart0InitData);
    SYS_INT_VectorPrioritySet(INT_VECTOR_UART2, INT_PRIORITY_LEVEL4);
    SYS_INT_VectorSubprioritySet(INT_VECTOR_UART2, INT_SUBPRIORITY_LEVEL0);

    /* Initialize System Services */
    SYS_INT_Initialize();

    /* Initialize Middleware */
    /* Set priority of USB interrupt source */
    SYS_INT_VectorPrioritySet(INT_VECTOR_USB1, INT_PRIORITY_LEVEL4);

    /* Set Sub-priority of USB interrupt source */
    SYS_INT_VectorSubprioritySet(INT_VECTOR_USB1, INT_SUBPRIORITY_LEVEL0);


    /* Initialize the USB device layer */
    sysObj.usbDevObject0 = USB_DEVICE_Initialize (USB_DEVICE_INDEX_0 , ( SYS_MODULE_INIT* ) & usbDevInitData);
    /* Enable Global Interrupts */
    SYS_INT_Enable();

    //startup OLED
    //if first power-up, wait for OLED to warm up 
    int waitii;
    if (RCONbits.POR)
        for (waitii = 0; waitii < 2000000; waitii++) {
        }
    RCONbits.POR = 0;
    oled_init();

    robot_config_ports();

    /* Initialize the Application */
    APP_Initialize();

}
Example #6
0
static void init()
{
//	OSCCAL = 71;

	clock_prescale_set(CPU_DIV);

//	power_twi_disable();
//	power_usart0_disable();
//	power_timer0_disable();
//	power_timer1_disable();
//	power_timer2_disable();
//	power_adc_disable();

#if !UART_ENABLE
	power_usart0_disable();
#endif

	// Pull-up on unused pins
	pinPullup(D0, PULLUP_ENABLE);
	pinPullup(D1, PULLUP_ENABLE);
	pinPullup(D3, PULLUP_ENABLE);
	pinPullup(D4, PULLUP_ENABLE);

	pinPullup(B7, PULLUP_ENABLE);

#if PIN_DEBUG != PIN_DEBUG_NONE
	pinMode(PIN_DEBUG_PIN, OUTPUT);
#endif

	// Pin change interrupt on USB power sense pin
	PCICR |= _BV(PCIE0);
	PCMSK0 |= _BV(PCINT6);

	// Everything else
	uart_init();
	spi_init();
	i2c_init();
	watchconfig_init();
	led_init();
	buzzer_init();
	battery_init();
	ds3231_init();
	buttons_init();
	millis_init();
	pwrmgr_init();
	time_init();
	alarm_init();
	oled_init();
}
Example #7
0
int main(void)
{
    oled_init();
    OLED_Display('G');
    switchline(0);
	//writestring("Hello,World!");
	writestring("The Advanced Encryption Standard is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology in 2001");
    while(1)
    {
	//writestring("Hello,World!");
		
        //TODO:: Please write your application code 
    }
	return 0;
}
Example #8
0
int main(void)
{
    sys_init();
    oled_init();

    oled_clearDisplay();
    oled_prints(35, 4, "Hello world");  // print on center of the screen
    oled_render();

    while(1)
    {
        LED ^= 1;
        __delay_ms(100);
    }
    return (0);
}
Example #9
0
// ------------------------------------------------------------
int main(void)
{ 
	oled_port_prepare();
	oled_reset();
	oled_init();
	oled_clear();
	oled_flip_horizontal(0);
	oled_flip_vertical(0);
	oled_brightness(0xFF);

/*
	oled_print(0,0,0,"1234567890");
	oled_print(1,0,0,"ABCDEFGHIJKLMN");
	oled_print(2,0,0,"OPQRSTUVWXYZ");

	oled_print(3,0,0,"abcdefghijklmn");
	oled_print(4,0,0,"opqrstuvwxyz");

	oled_print(6,0,0,"^`_{|}:'<=>?@");
*/

/*
	for(r=0; r<7; r++) // KAZKODEL SUSIGADINA STATIC VAIZDAS!!!
	{
		oled_write_cmd(0x10);
		oled_write_cmd(0x00);
		oled_write_cmd(0xB0+r);
		for (i=0;i<128;i++)
		{
			oled_write_data(picwilibox[pos++]);
		}
	}
*/


	oled_print2(0, 0, "Wilibox");
	oled_print2(0, 2, "-------");
	oled_print2(0, 4, "1234567890");
	oled_print(7,0,0,"123456789012345678901");


	read_bit(BUTTON);
	write_bit(LED2_GRN, 0);
	
	oled_close();
	return  0;
}
Example #10
0
File: oled.c Project: uniite/imd
int main (int argc, char **argv) {
	int i, i2, row = 127;
	char bmp_hdr[54];
  unsigned char buff[3];
	FILE *file;
	
  oled_init();
  oled_write_c(0xae); /* Display off */
	oled_clear();
	
	file = fopen("/root/splash.bmp", "rb");
	fread(&bmp_hdr, 1, 54, file);	
	
	for (i = 0; i < 128; i++) {
		for (i2 = 0; i2 < 128; i2++) {
			fread(&buff, 1, 3, file);
			frame_buffer[row][i2][0] = buff[2] / 4;
			frame_buffer[row][i2][1] = buff[1] / 4;
			frame_buffer[row][i2][2] = buff[0] / 4;
		}
		row--;
	}
	
	gui_textbox_t txtLoading;
	strcpy(txtLoading.text, "Loading...");
	txtLoading.x = oled_screenwidth / 2;
	txtLoading.y = 100;
	txtLoading.maxwidth = 0;
	txtLoading.align = 2;
	txtLoading.mode = 0;
	txtLoading.color[0] = 17;
	txtLoading.color[1] = 27;
	txtLoading.color[2] = 45;
	gui_textbox_init(&txtLoading);
	//gui_textbox_draw(&txtLoading);
	
	oled_flush();
	
	oled_write_c(0xaf); /* Display on */
	
	return 0;
}
Example #11
0
int main(void)
{	
	// Activate external memory
	MCUCR |= (1 << SRE);
		
	// Initialize	
	USART_init(MYUBRR);
	joy_init();
	oled_init();
	can_init();
	menu_init();
	
	
	// Starts the FSM
	while(1)
	{
		menu_fsm();
		_delay_ms(20);
	}	
}
Example #12
0
// ------------------------------------------------------------
int main(int argc, char *argv[])
{ 
	int i;

	oled_port_prepare();
	oled_reset();
	oled_init();
	oled_clear();
	oled_flip_horizontal(0);
	oled_flip_vertical(0);
	oled_brightness(0xFF);

	if (argc < 2) {
		fprintf(stderr, "Usage: %s \"string1\" \"string2\"..\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	for (i = 1; i < argc; i++) {
		oled_print2(0, 2 * (i - 1), argv[i]);
	}

	oled_close();	
	exit(EXIT_SUCCESS);
}
Example #13
0
// Application Start
void main(void)
{
    // Prepare Application; MCC generated code
    SYSTEM_Initialize();
    // Custom Initializations
    
    
    
    /*   
     *   watchdog timer 
     *   configured for 131s timeout
     * 
     */
    //WDTCONbits.SWDTEN = 1; // enable watchdog
    //ClrWdt();
    
    
    
    oled_init();         // Dispaly
    USBDeviceInit();	// usb_device.c.  Initializes USB module SFRs and firmware
                        // variables to known states.
    
    
    
    
    USBDeviceAttach();
    // Interrupts Enabled
    PEIE = 1;
    GIE = 1;
    
    // Test Hardware
    moteApp_delayms(300);    
    modemResync();
    moteApp_delayms(300);
//    moteApp_clearCommand();
//    moteApp_delayms(100);   
    
    powerOnStatus = DATAEE_ReadByte(0x00);
#if 0
    if ( powerOnStatus != 0x55)
    {
        test_powerup();
        DATAEE_WriteByte(0x00, 0x55);
    }
#endif

    // Default to USB
    operationType = USB;   // Mode State
    USBapp_handlerState(USB_STARTUP);
    
    
    
    // Initial Display Print
    oled_clear();
    oled_putString("Scriptr IoT-X",0,0);
    oled_putString(" Dev: S1 ",0,1);
    oled_putString("   build 006   ",0,3);
    moteApp_delayms(700);
   
    // Application Loop
    while(1)
    {
        // Handle USB or Solo Mode
        switch (operationType)
        {
            default:        // Invalid
                while(1);   // Hold For Error;
            break;
            case USB:
                USBapp_Handler();   // It is held in here.
                // USB escaped; Cable is unplugged; Change OPERATION mode.
                operationType = MOTE;
                MOTEapp_handlerState(MOTE_STARTUP);   // Initial state for startup
            break;
            case MOTE:
                if (moteHandler() == MOTE_SWAP)
                {
                    // Return to USB Operation
                    operationType = USB;   // Mode State
                    USBapp_handlerState(USB_STARTUP);
                }
            break;
        }
    }
}
Example #14
0
void oled_main(void)
{
    int fd;
    int result;
    uint8_t mode = SPI_MODE_0;
    uint8_t bits = 8;
    //uint32_t speed = 4000000;
    uint32_t speed = 8000000; // on the spec,SCLK max freq=4[MHz]
    uint16_t delay;

    fd = open("/dev/spidev0.0",O_RDWR);
    if(fd == -1){
	printf("Device open error\n");
	return;
    }

    /*
      set SPI read/write mode
    */
    result = ioctl(fd,SPI_IOC_WR_MODE,&mode);
    if(result < 0){
	printf("error(L%d)\n",__LINE__);
	return;
    }
    result = ioctl(fd,SPI_IOC_RD_MODE,&mode);
    if(result < 0){
	printf("error(L%d)\n",__LINE__);
	return;
    }

    result = ioctl(fd,SPI_IOC_WR_BITS_PER_WORD,&bits);
    if(result < 0){
	printf("error(L%d)\n",__LINE__);
	return;
    }
    result = ioctl(fd,SPI_IOC_RD_BITS_PER_WORD,&bits);
    if(result < 0){
	printf("error(L%d)\n",__LINE__);
	return;
    }

    result = ioctl(fd,SPI_IOC_WR_MAX_SPEED_HZ,&speed);
    if(result < 0){
	printf("error(L%d)\n",__LINE__);
	return;
    }
    result = ioctl(fd,SPI_IOC_RD_MAX_SPEED_HZ,&speed);
    if(result < 0){
	printf("error(L%d)\n",__LINE__);
	return;
    }

    oled_init(fd);

    {
	unsigned char buff[96*64*2];
	bool r;


	while(1){
	    r = fb_getTopLeftBuff_for_OLED(buff,96,64);
	    if(r == false){
		printf("%s : fb_getTopLeftBuffer_for_OLED returns error\n",__func__);
		return;
	    }
	    r = oled_transmitFullScreen(fd,buff);
	    if(r == false){
		printf("%s : oled_transmitFullScreen returns error\n",__func__);
		return;
	    }
	}
    }

    close(fd);
}
Example #15
0
void main(void) {
byte i,bear_count,dog_count;

	M8C_DisableGInt;

	PORTA = 0x00;
	DDRA  = 0x00;
	PORTB = 0x00;
	DDRB  = 0x00;
	PORTC = 0x00; //m103 output only
	DDRC  = 0x00;
	PORTD = 0x00;
	DDRD  = 0x00;

	MCUCR = 0x00;
	GICR  = 0x00;

	GLCD_SHDN_Dir	|= GLCD_SHDN_bit;
	GLCD_RES_Dir	|= GLCD_RES_bit;

	GLCD_DC_Dir		|= GLCD_DC_bit;
	GLCD_CS_Dir		|= GLCD_CS_bit;
	GLCD_WR_Dir		|= GLCD_WR_bit;
	GLCD_EN_Dir		|= GLCD_EN_bit;

	GLCD_SHDN_oPort	&= ~GLCD_SHDN_bit;
	GLCD_RES_oPort	|= GLCD_RES_bit;

	GLCD_DC_oPort	&= ~GLCD_DC_bit;
	GLCD_CS_oPort	|= GLCD_CS_bit;
	GLCD_WR_oPort	|= GLCD_WR_bit;
	GLCD_EN_oPort	|= GLCD_EN_bit;

	OLED_interface = BS_iPort & (BS_2|BS_1);

	SPIM_1_Start(SPIM_1_SPIM_MODE_3); // Init
///	SPI1M_Speed(1); // 1-250, 2-62, 3-31
	SPI1M_Speed(0); // 1-250, 2-62, 3-31
	GLCD_Data_oPort = 0; GLCD_Data_Dir = 0;

	M8C_EnableGInt;
	GLCD_Data_Dir |= 0xFF;
	GLCD_WR_oPort &= ~GLCD_WR_bit;		// WR=0 - write
	oled_init();

	while (1) {
		bear_count++; if (bear_count >= 13) bear_count = 0;
		img_out(32,8,&bear[bear_count][0]);
		delay_ms(100);
		dog_count++;
		if (dog_count < 20) i = 0; else
		if (dog_count < 21) i = 1; else
		if (dog_count < 27) i = 2; else
		if (dog_count < 28) i = 3; else
		if (dog_count < 29) i = 4; else
		if (dog_count < 54) i = 5; else
		if (dog_count < 55) i = 6; else
		if (dog_count < 56) i = 7; else
		if (dog_count < 57) i = 7; else dog_count = 0;
		img_out(0,14,&dog[i][0]);
		delay_ms(100);
	}
}
Example #16
0
uint16 SensorTag_ProcessEvent(uint8 task_id, uint16 events)
{
	VOID	task_id;	// OSAL required parameter that isn't used in this function


	///////////////////////////////////////////////////////////////////////
	// system event handle                                               //
	///////////////////////////////////////////////////////////////////////
	if (events & SYS_EVENT_MSG) {
		uint8	*msg;

		if ((msg = osal_msg_receive(sensorTag_TaskID)) != NULL) {
			sensorTag_ProcessOSALMsg((osal_event_hdr_t *) msg);

			// release the OSAL message
			osal_msg_deallocate(msg);
		}

		// return unprocessed events
		return (events ^ SYS_EVENT_MSG);
	}


	///////////////////////////////////////////////////////////////////////
	// start device event                                                //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_START_DEVICE) {
		// start the device
		GAPRole_StartDevice(&sensorTag_PeripheralCBs);

		// start bond manager
#if !defined(GAPBONDMGR_NO_SUPPORT)
		GAPBondMgr_Register(&sensorTag_BondMgrCBs);
#endif
		// start peripheral device
		oled_init();
		adxl345_softrst();
//		adxl345_self_calibration();

		steps	     = 0;
		BATCD_PXIFG  = ~(BATCD_BV);
		BATCD_IEN   |=  BATCD_IENBIT;

		osal_start_reload_timer(sensorTag_TaskID, EVT_RTC, PERIOD_RTC);
		pwmgr_state_change(PWMGR_S0, 0);

		fmsg(("\033[40;32m\n[power on]\033[0m\n"));
		return (events ^ EVT_START_DEVICE);
	}


	///////////////////////////////////////////////////////////////////////
	// key long press handle                                             //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_MODE) {
		if (key1_press) {
			oled_clr_screen();
			if ((opmode & 0xF0) == MODE_NORMAL) {
				opmode        = MODE_WORKOUT | MODE_TIME;
				workout.steps = normal.steps;
				workout.time  = osal_getClock();
				fmsg(("\033[40;32m[workout mode]\033[0m\n"));
			} else {
				opmode        = MODE_NORMAL | MODE_TIME;
				fmsg(("\033[40;32m[normal mode]\033[0m\n"));
			}

			pwmgr_state_change(pwmgr, TIME_OLED_OFF);
		}
		return (events ^ EVT_MODE);
	}

	if (events & EVT_SLEEP) {
		if (key1_press) {
			oled_clr_screen();
			opmode = MODE_SLEEP;
			fmsg(("\033[40;32m[sleep mode]\033[0m\n"));

			pwmgr_state_change(pwmgr, TIME_OLED_OFF);
		}
		return (events ^ EVT_SLEEP);
	}

	if (events & EVT_SYSRST) {
		if (key1_press) {
			fmsg(("\033[40;32m[system reset]\033[0m\n"));
			HAL_SYSTEM_RESET();
//			adxl345_self_calibration();
		}
		return (events ^ EVT_SYSRST);
	}


	///////////////////////////////////////////////////////////////////////
	// display handle                                                    //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_DISP) {
		if (pwmgr == PWMGR_S1) {
			sensorTag_HandleDisp(opmode, acc);
		} else {
			// display battery only
			sensorTag_BattDisp(batt_get_level());
		}
		if (pwmgr != PWMGR_S6)  {
			osal_start_timerEx(sensorTag_TaskID, EVT_DISP, PERIOD_DISP);
		}
		return (events ^ EVT_DISP);
	}


	///////////////////////////////////////////////////////////////////////
	// g-sensor handle                                                   //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_GSNINT1) {
		adxl345_exit_sleep();
		pwmgr_state_change(PWMGR_S3, TIME_GSEN_OFF);
		return (events ^ EVT_GSNINT1);
	}

	if (events & EVT_GSNINT2) {
		unsigned char	sampling;
		unsigned char	i;

		sampling = adxl345_chk_fifo();
		for (i=0; i<sampling; i++) {
			adxl345_read(acc);
#if (DEBUG_MESSAGE & MSG_STEPS)
			{
				unsigned long	tmp = algo_step(acc);
				if (normal.steps != tmp) {
					stepmsg(("\033[1;33mstep=%0lu\n\033[0m", tmp));
				}
				normal.steps = tmp;
			}
#else
			normal.steps = algo_step(acc);
#endif
		}

		normal.distance  = calc_distance(normal.steps, pi.stride);
		workout.distance = calc_distance((normal.steps - workout.steps), pi.stride);
		normal.calorie   = calc_calorie(normal.distance, pi.weight);
		workout.calorie  = calc_calorie(workout.distance, pi.weight);
		return (events ^ EVT_GSNINT2);
	}

	if (events & EVT_GSENSOR) {
		adxl345_exit_sleep();
		return (events ^ EVT_GSENSOR);
	}


	///////////////////////////////////////////////////////////////////////
	// RTC handle                                                        //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_RTC) {
		// performed once per second


		// record data
		if ((pwmgr != PWMGR_S5) && (pwmgr != PWMGR_S6)) {
#if defined(HAL_IMAGE_A) || defined(HAL_IMAGE_B)
			if ((osal_getClock() - mark.time) >= (12UL*60UL)) {
#else
			if ((osal_getClock() - mark.time) >= (12UL)) {
#endif
				if (!hash_is_full()) {
					unsigned short	tmp = normal.steps - mark.steps;

					switch (opmode & 0xF0) {
					case MODE_WORKOUT: tmp |= 0x8000; break;
					case MODE_SLEEP:   tmp |= 0x4000; break;
					}
					hash_put(&tmp);
				}
				mark.time  = osal_getClock();
#if defined(HAL_IMAGE_A) || defined(HAL_IMAGE_B)
				if ((mark.time % (24UL*60UL*60UL)) <= (13UL*60UL)) {
#else
				if ((mark.time % (24UL*60UL*60UL)) <= (13UL)) {
#endif
					dmsg(("reset steps...\n"));
					normal.steps  = 0;
					workout.steps = 0;
					STEPS         = 0;
				}
				mark.steps = normal.steps;
			}
		}

		// power management
		switch (pwmgr) {
		case PWMGR_S0:
			pmsg(("\033[40;35mS0 (power on)\033[0m\n"));
			if (pwmgr_saving_timer()) {
				adxl345_enter_sleep();

				osal_pwrmgr_device(PWRMGR_BATTERY);
				pwmgr_state_change(PWMGR_S4, 0);
			}
			break;

		case PWMGR_S1:
			pmsg(("\033[40;35mS1 (rtc+gsen+ble+oled)\033[0m\n"));
			if (pwmgr_saving_timer()) {
				oled_enter_sleep();
				osal_stop_timerEx(sensorTag_TaskID, EVT_MODE);
				osal_stop_timerEx(sensorTag_TaskID, EVT_SLEEP);
				osal_stop_timerEx(sensorTag_TaskID, EVT_SYSRST);

				pwmgr_state_change(PWMGR_S3, TIME_GSEN_OFF);
			}
			break;

		case PWMGR_S2:
			pmsg(("\033[40;35mS2 (rtc+gsen+ble)\033[0m\n"));
			if (gapProfileState == GAPROLE_WAITING) {
				// enable key interrupt mode
				InitBoard(OB_READY);
				pwmgr_state_change(PWMGR_S3, TIME_GSEN_OFF);
			}
			break;

		case PWMGR_S3:
			pmsg(("\033[40;35mS3 (rtc+gsen)\033[0m\n"));
			if (steps == normal.steps) {
				if (pwmgr_saving_timer()) {
					adxl345_enter_sleep();
					pwmgr_state_change(PWMGR_S4, 0);
				}
			} else {
				steps = normal.steps;
				pwmgr_state_change(pwmgr, TIME_GSEN_OFF);
			}
			break;

		case PWMGR_S4:
			pmsg(("\033[40;35mS4 (rtc)\033[0m\n"));
			dmsg(("$"));
			break;

		default:
		case PWMGR_S5:
			pmsg(("\033[40;35mS5 (shutdown)\033[0m\n"));
			adxl345_shutdown();
			osal_stop_timerEx(sensorTag_TaskID, EVT_RTC);
			break;

		case PWMGR_S6:
			pmsg(("\033[40;35mS6 (rtc+oled)\033[0m\n"));
			if (pwmgr_saving_timer()) {
				oled_enter_sleep();

				// enable key interrupt mode
				InitBoard(OB_READY);
				pwmgr_state_change(PWMGR_S5, 0);
			}
			break;
		}

		// battery measure
		if ((!batt_measure()) && (pwmgr != PWMGR_S6)) {
			pwmgr_state_change(PWMGR_S5, 0);
		}
		return (events ^ EVT_RTC);
	}


	///////////////////////////////////////////////////////////////////////
	// battery charge detect handle                                      //
	///////////////////////////////////////////////////////////////////////
	if (events & EVT_CHARGING) {
		if (pwmgr != PWMGR_S1) {
			if (!BATCD_SBIT) {
				dmsg(("[charging]\n"));
				oled_exit_sleep();
				if ((pwmgr == PWMGR_S5) || (pwmgr == PWMGR_S6)) {
					osal_start_reload_timer(sensorTag_TaskID, EVT_RTC, PERIOD_RTC);
					pwmgr_state_change(PWMGR_S4, 0);
				}
			} else {
				dmsg(("[no charge]\n"));
				oled_enter_sleep();
			}
		}
		return (events ^ EVT_CHARGING);
	}


	///////////////////////////////////////////////////////////////////////
	// discard unknown events                                            //
	///////////////////////////////////////////////////////////////////////

	return 0;
}
/**
 *  \brief  Audio Class intialization function
 *
 *  \param  None
 *
 *  \return None
 */
void CSL_acTest(void)
{
    I2sInitPrms i2sInitPrms;
    CSL_UsbConfig usbConfig;
    PSP_Result result;
    Int16 status;
    HWI_Attrs attrs;


    LOG_printf(&trace, "USB ISO FULL SPEED MODE\n");

    /* Initialize audio module */
    result = AIC3254_init();
    if(result != 0)
    {
        LOG_printf(&trace, "ERROR: Unable to configure audio codec");
    }
    else
    {
#if !defined(SAMPLE_BY_SAMPLE_PB) || !defined(SAMPLE_BY_SAMPLE_REC)
        DMA_HwInit();
        DMA_DrvInit();
#endif

        /* Initialize I2S and associated DMA channels for Playback and Record */
        i2sInitPrms.enablePlayback = TRUE;
        i2sInitPrms.enableStereoPb = TRUE;
#ifdef SAMPLE_BY_SAMPLE_PB
        i2sInitPrms.sampleBySamplePb = TRUE;
#else /* Configuration untested since ASRC only works with I2S in sample-by-sample mode */
        i2sInitPrms.sampleBySamplePb = FALSE;
        i2sInitPrms.enableDmaPingPongPb = FALSE;
        i2sInitPrms.pingI2sTxLeftBuf = ping_i2sTxLeftBuf;
        i2sInitPrms.pongI2sTxLeftBuf = pong_i2sTxLeftBuf;
        i2sInitPrms.pingI2sTxRightBuf = ping_i2sTxRightBuf;
        i2sInitPrms.pongI2sTxRightBuf = pong_i2sTxRightBuf;
        i2sInitPrms.zeroBuf = ZeroBuf;
#endif
        i2sInitPrms.i2sPb = PSP_I2S_TX_INST_ID;
        i2sInitPrms.enableRecord = TRUE;
        i2sInitPrms.enableStereoRec = FALSE;
#ifdef SAMPLE_BY_SAMPLE_REC
        i2sInitPrms.sampleBySampleRec = TRUE;
#else
        i2sInitPrms.sampleBySampleRec = FALSE;
        i2sInitPrms.enableDmaPingPongRec = TRUE;
        i2sInitPrms.pingI2sRxLeftBuf = (Int16 *)ping_pong_i2sRxLeftBuf;
        i2sInitPrms.pongI2sRxLeftBuf = NULL;
        i2sInitPrms.pingI2sRxRightBuf = (Int16 *)ping_pong_i2sRxRightBuf;
        i2sInitPrms.pongI2sRxRightBuf = NULL;
#endif
        i2sInitPrms.i2sRec = PSP_I2S_RX_INST_ID;
        status = i2sInit(&i2sInitPrms);
        if (status != I2SSAMPLE_SOK)
        {
            LOG_printf(&trace, "ERROR: Unable to initialize I2S");
        }

#ifdef C5535_EZDSP_DEMO
		// initialize the OLED display        
        oled_init();
#endif
        
        /* Initialising the Pointer to the Audio Class Handle to the Buffer Allocated */
        AC_AppHandle.pAcObj = &ACAppBuffer[0];

        usbConfig.devNum                = CSL_USB0;
        usbConfig.opMode                = CSL_USB_OPMODE_POLLED;
#ifdef APP_USB_SELF_POWERED
        usbConfig.selfPowered           = TRUE;
#else
        usbConfig.selfPowered           = FALSE;
#endif
        usbConfig.maxCurrent            = APP_USB_MAX_CURRENT;
        usbConfig.appSuspendCallBack    = (CSL_USB_APP_CALLBACK)CSL_suspendCallBack;
        usbConfig.appWakeupCallBack     = (CSL_USB_APP_CALLBACK)CSL_selfWakeupCallBack;
        usbConfig.startTransferCallback  = StartTransfer;
        usbConfig.completeTransferCallback = CompleteTransfer;

        USB_init(&usbConfig);

        USB_setFullSpeedMode(0x40); /* parameter is EP0 data size in bytes */

        USB_resetDev(CSL_USB0);

        /* Calling init routine */
        /* Giving all the table hanldes and the buffers to the Audio Class module */
        AC_AppHandle.strDescrApp = (char **)&string_descriptor[0];
        AC_AppHandle.lbaBufferPbApp = &lbaBufferPbApp[0];
        AC_AppHandle.lbaBufferRecApp = &lbaBufferRecApp[0];
        AC_AppHandle.lbaBufferHidReportApp = &lbaBufferHidReportApp[0];
        AC_AppHandle.acReqTableApp = USB_ReqTable;
        AC_AppHandle.pId = pId;
        AC_AppHandle.vId = vId;

#ifndef ENABLE_PLAYBACK_TWO_SAMPLE_RATES
        #ifdef SAMPLE_RATE_TX_48kHz
        LOG_printf(&trace, "PLAYBACK: 48KHZ ");
        #ifdef ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "STEREO\n");
        AC_AppHandle.rxPktSize = EP_PB_MAXP; // max packet size for 48K stereo
        #else // ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "MONO\n");
        AC_AppHandle.rxPktSize = 0x60; // max packet size for 48K mono
        #endif // ENABLE_STEREO_PLAYBACK
        #endif // SAMPLE_RATE_TX_48kHz

        #ifdef SAMPLE_RATE_TX_44_1kHz
        LOG_printf(&trace, "PLAYBACK: 44.1KHZ ");
        #ifdef ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "STEREO\n");
        AC_AppHandle.rxPktSize = 0xB0; // max packet size for 44.1 stereo
        #else // ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "MONO\n");
        AC_AppHandle.rxPktSize = 0x58; // max packet size for 44.1 mono
        #endif // ENABLE_STEREO_PLAYBACK
        #endif // SAMPLE_RATE_TX_44_1kHz

        #ifdef SAMPLE_RATE_TX_32kHz
        LOG_printf(&trace, "PLAYBACK: 32KHZ ");
        #ifdef ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "STEREO\n");
        AC_AppHandle.rxPktSize = 0x80; // max packet size for 32K stereo
        #else // ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "MONO\n");
        AC_AppHandle.rxPktSize = 0x40; // max packet size for 32K mono
        #endif // ENABLE_STEREO_PLAYBACK
        #endif // SAMPLE_RATE_TX_32kHz

        #ifdef SAMPLE_RATE_TX_16kHz
        LOG_printf(&trace, "PLAYBACK: 16KHZ ");
        #ifdef ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "STEREO\n");
        AC_AppHandle.rxPktSize = RX_PKT_SIZE_16K_PLAYBACK_STEREO; // max packet size for 16K stereo
        rx_pkt_size_16K_playback = RX_PKT_SIZE_16K_PLAYBACK_STEREO; // max packet size for 16K stereo
        #else // ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "MONO\n");
        AC_AppHandle.rxPktSize = RX_PKT_SIZE_16K_PLAYBACK_MONO; // max packet size for 16K mono
        rx_pkt_size_16K_playback = RX_PKT_SIZE_16K_PLAYBACK_MONO;  // max packet size for 16K mono
        #endif // ENABLE_STEREO_PLAYBACK
        #endif // SAMPLE_RATE_TX_16kHz

#else /* ENABLE_PLAYBACK_TWO_SAMPLE_RATES */
        LOG_printf(&trace, "PLAYBACK: 48KHZ ");
        #ifdef ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "STEREO\n");
        AC_AppHandle.rxPktSize = EP_PB_MAXP; // max packet size for 48K stereo
        #else // ENABLE_STEREO_PLAYBACK
        LOG_printf(&trace, "MONO\n");
        AC_AppHandle.rxPktSize = 0x60; // max packet size for 48K mono
        #endif // ENABLE_STEREO_PLAYBACK

        LOG_printf(&trace, "PLAYBACK: 16KHZ ");
        #ifdef ENABLE_STEREO_PLAYBACK
        rx_pkt_size_16K_playback = RX_PKT_SIZE_16K_PLAYBACK_STEREO; // max packet size for 16K stereo
        LOG_printf(&trace, "STEREO\n");
        #else // ENABLE_STEREO_PLAYBACK
        rx_pkt_size_16K_playback = RX_PKT_SIZE_16K_PLAYBACK_MONO;  // max packet size for 16K mono
        LOG_printf(&trace, "MONO\n");
        #endif // ENABLE_STEREO_PLAYBACK
#endif /* ENABLE_PLAYBACK_TWO_SAMPLE_RATES */

        AC_AppHandle.txPktSize = EP_REC_MAXP; // max packet size for 16K mono
        AC_AppHandle.hidTxPktSize = EP_HID_MAXP; // max packet size for HID output report

        /* All Function Handlers need to be Initialised */
        AC_AppHandle.playAudioApp = appPlayAudio;
        AC_AppHandle.recordAudioApp = appRecordAudio;
        AC_AppHandle.initPlayAudioApp = appInitPlayAudio;
        AC_AppHandle.initRecordAudioApp = appInitRecordAudio;
        AC_AppHandle.stopPlayAudioApp = appStopPlayAudio;
        AC_AppHandle.stopRecordAudioApp = appStopRecordAudio;
        AC_AppHandle.mediaGetPresentStateApp = AppGetMediaStatus;
        AC_AppHandle.mediaInitApp = AppMediaInit;
        AC_AppHandle.mediaEjectApp = AppMediaEject;
        AC_AppHandle.mediaLockUnitApp = AppLockMedia;
        AC_AppHandle.getMediaSizeApp = AppGetMediaSize;
        AC_AppHandle.getHidReportApp = appGetHidReport;
        AC_AppHandle.ctrlHandler  = appCtrlFxn;
        AC_AppHandle.isoHandler   = appIsoFxn;
        AC_AppHandle.hidHandler = appHidFxn;

        AC_AppHandle.numLun = 2;

        /* Initialize End point descriptors */
        AC_initDescriptors(AC_AppHandle.pAcObj, (Uint16 *)deviceDescriptorB,
                            CSL_AC_DEVICE_DESCR, sizeof(deviceDescriptorB));

        AC_initDescriptors(AC_AppHandle.pAcObj, (Uint16 *)deviceQualifierDescr,
                            CSL_AC_DEVICE_QUAL_DESCR, 10);

        AC_initDescriptors(AC_AppHandle.pAcObj, (Uint16 *)configDescriptor,
                            CSL_AC_CONFIG_DESCR, sizeof(configDescriptor));

        AC_initDescriptors(AC_AppHandle.pAcObj, (Uint16 *)stringLanId,
                            CSL_AC_STRING_LANGID_DESC, 6);

        AC_initDescriptors(AC_AppHandle.pAcObj, (Uint16 *)acHidReportDescriptor,
                            CSL_AC_HID_REPORT_DESC, sizeof(acHidReportDescriptor));

        /* Initialize HID */
        AC_AppHandle.acHidIfNum = IF_NUM_HID; // HID interface number
        AC_AppHandle.acHidReportId = HID_REPORT_ID; // HID report ID
        AC_AppHandle.acHidReportLen = HID_REPORT_SIZE_BYTES; // HID report length (bytes)
        genHidReport(UI_PUSH_BUTTON_NONE, gHidReport); // init. HID report for Get Report 

        /* Call Init API */
        AC_Open(&AC_AppHandle);

        /* Enable CPU USB interrupts */
        CSL_FINST(CSL_CPU_REGS->IER1, CPU_IER1_USB, ENABLE);

        /* Initialize active sample rate */
        initSampleRate(RATE_48_KHZ, &active_sample_rate, 
            &i2sTxBuffSz);

        /* Initialize ASRC */ 
        Init_Sample_Rate_Converter(active_sample_rate);

        /* Reset codec output buffer */
        reset_codec_output_buffer();

        #ifdef ENABLE_RECORD
        #ifdef SAMPLE_RATE_RX_48kHz
        LOG_printf(&trace, "RECORD: 48KHZ ");
        #else
        LOG_printf(&trace, "RECORD: 16KHZ ");
        #endif // SAMPLE_RATE_RX_48kHz
        // start the rx DMAs
        DMA_StartTransfer(hDmaRxLeft);

        #ifdef ENABLE_STEREO_RECORD
        LOG_printf(&trace, "STEREO  NOT SUPPORTED - RECORD WILL BE MONO\n");
        DMA_StartTransfer(hDmaRxRight);
         #else
        LOG_printf(&trace, "MONO\n");
        #endif

        #endif // ENABLE_RECORD

#ifdef STORE_PARAMETERS_TO_SDRAM
        initSdram(FALSE, 0x0000);
#endif // STORE_PARAMETERS_TO_SDRAM
#ifdef SAMPLE_BY_SAMPLE_PB
        /* SampleBySample, init interrupt */       
        /* Use with compiler "interrupt" keyword */
        //IRQ_plug(I2S_TX_EVENT, i2s_txIsr);
        
        /* Use with dispatcher, no "interrupt" keyword */
        attrs.ier0mask = 0xFFFF;
        attrs.ier1mask = 0xFFFF;
        HWI_dispatchPlug(I2S_TX_EVENT, (Fxn)i2s_txIsr, &attrs);

        IRQ_enable(I2S_TX_EVENT);   /* SampleBySample, enable IRQ for I2S Tx */
#endif
#if defined(SAMPLE_BY_SAMPLE_REC) && !defined(COMBINE_I2S_TX_RX_ISR)
        /* SampleBySample, init interrupt */
        /* Use with compiler "interrupt" keyword */
        IRQ_plug(I2S_RX_EVENT, i2s_rxIsr);
        
        /* Use with dispatcher, no "interrupt" keyword */
        //attrs.ier0mask = 0xFFFF;
        //attrs.ier1mask = 0xFFFF;
        //HWI_dispatchPlug(I2S_RX_EVENT, (Fxn)i2s_rxIsr, &attrs);

        IRQ_enable(I2S_RX_EVENT);    /* SampleBySample, enable IRQ for I2S Rx */
#endif
#if defined(SAMPLE_BY_SAMPLE_PB) || defined(SAMPLE_BY_SAMLE_REC)
        DDC_I2S_transEnable((DDC_I2SHandle)i2sHandleTx, TRUE);    /* SampleBySample, enable I2S transmit and receive */
#endif
#ifndef SAMPLE_BY_SAMPLE_PB
        i2sTxStart(); // - moved from appPlayAudio()
#endif

#ifdef C5535_EZDSP_DEMO
        // clock gating usused peripherals
		ClockGating();
#endif
    }
}
Example #18
0
static uint8_t moteApp_GoToSleep(void)
{
    uint8_t status = 0;
    while (SWITCH_2_PORT == 0);     // Hold for Button Release
    modemSleep();
    oled_displayOff(true); 
    ADCON0bits.ADON = 0;       // ADC Off
    INTCONbits.GIE = 0;
    GIE = 0;
    PEIE = 0;
    // Prepare USB Detect
    USB_DET_ANSEL = 1;
    // Prepare IOC to wake us up
    USB_DET_EN = 1;      // Enables CMP1 interrupt.
    SWITCH_1_IOC = 1;
    SWITCH_2_IOC = 1;
    MODEM_WAKE_IOC = 1;
    IOC_ENABLE = 1;
    // Dummy Reads to Establish IOC level
    status = SWITCH_1_PORT;
    status = SWITCH_2_PORT;
    status = MODEM_WAKE_PORT;
    if (periodicState)
    {
        //WDTCONbits.SWDTEN = 1;
    }
    IOC_FLAG = 0;
    USB_DET_FLAG = 0;
    NOP();
    SLEEP();
    NOP();
    // Reconfigure Critical peripherals
    //WDTCONbits.SWDTEN = 0;
    EUSART_FlushBuffer();
    // Disable IOC
    IOC_ENABLE = 0;
    SWITCH_1_IOC = 0;
    SWITCH_2_IOC = 0;
    MODEM_WAKE_IOC = 0;
    PIE2bits.C1IE = 0;
    GIE = 1;

    PIN_MANAGER_Initialize();
    EUSART_Initialize();
    // Process Wake Up Event
    if (IOC_FLAG)
    {   // PushButton or Module IOC woke us
        IOC_FLAG = 0;
        status = 1;
    }
// TODO:
//    else if (Comparator)    // USB EVENT
//    {
//        status = 2;
//    }
    else
    {   // Time woke us. Reload; so wake from sleep is short
        secTicker = 10;
        status = 0;
    }
    USB_DET_ANSEL = 0;
    oled_displayOff(false);
    oled_init();         // Dispaly

    modemResync();
    return status;
}
Example #19
0
File: main.cpp Project: ryuz/jelly
int main()
{
//	return i2c_test();
	
	UioMmap um_pl_peri("my_pl_peri", 0x00200000);
	if ( !um_pl_peri.IsMapped() ) {
		printf("map error : my_pl_peri\n");
		return 1;
	}
	
	UioMmap um_pl_mem("my_pl_ddr3", 0x10000000);
	if ( !um_pl_mem.IsMapped() ) {
		printf("map error : my_pl_ddr3\n");
		return 1;
	}
	
	// normalizer stop
	um_pl_peri.WriteWord32(0x00011000, 0);
	usleep(1000);
	while ( um_pl_peri.ReadWord32(0x00011004) != 0 ) {
		usleep(1000);
	}
	
	
	// demosaic param_phase
	um_pl_peri.WriteWord32(0x00012000, 0);
	
	
	oled_init(&um_pl_peri);
	oled_main();
	
	
//	volatile uint32_t *peri_addr = (volatile uint32_t *)um_pl_peri.GetAddress();
//	printf("hello:%x\n", peri_addr[0]);
	
	
//	cv::Mat img(IMAGE_HEIGHT, IMAGE_WIDTH, CV_16U);
	
	
	int w = 640;
	int h = 132;
	
	I2cAccess	i2c;
	
	if ( !i2c.Open("/dev/i2c-0", 0x10) ) {
		printf("I2C open error\n");
		return 1;
	}
	
//	printf("0x00 : %02x\n", i2c.ReadAddr16Byte(0x00));
//	printf("0x01 : %02x\n", i2c.ReadAddr16Byte(0x01));
	
	i2c.WriteAddr16Byte(0x0103, 0x01);
	usleep(10000);
	i2c.WriteAddr16Byte(0x0103, 0x00);
	usleep(10000);
	printf("0x00 : %02x\n", i2c.ReadAddr16Byte(0x00));
	printf("0x01 : %02x\n", i2c.ReadAddr16Byte(0x01));
	printf("%02x\n", i2c.ReadAddr16Byte(0x0103));
	
	
	i2c.WriteAddr16Byte(0x0102, 0x01  );   // ???? (Reserved)
//	i2c.WriteAddr16Word();
	i2c.WriteAddr16Byte(0x0100, 0x00  );   // mode_select [4:0]  (0: SW standby, 1: Streaming)
	i2c.WriteAddr16Word(0x6620, 0x0101);   // ????
	i2c.WriteAddr16Word(0x6622, 0x0101);
	
	/*
	i2c.WriteAddr16Byte(0x30EB, 0x0C  );   // Access command sequence Seq. No. 2
	i2c.WriteAddr16Byte(0x30EB, 0x05);
	i2c.WriteAddr16Word(0x300A, 0xFFFF);
	i2c.WriteAddr16Byte(0x30EB, 0x05);
	i2c.WriteAddr16Byte(0x30EB, 0x09);
	*/
	
	i2c.WriteAddr16Byte(0x30EB, 0x05);   // Access command sequence Seq.
	i2c.WriteAddr16Byte(0x30EB, 0x0C);
	i2c.WriteAddr16Byte(0x300A, 0xFF);
	i2c.WriteAddr16Byte(0x300B, 0xFF);
	i2c.WriteAddr16Byte(0x30EB, 0x05);
	i2c.WriteAddr16Byte(0x30EB, 0x09);
	
	i2c.WriteAddr16Byte(0x0114, 0x01  );   // * CSI_LANE_MODE (03: 4Lane 01: 2Lane)
	i2c.WriteAddr16Byte(0x0128, 0x00  );   //   DPHY_CTRL (MIPI Global timing setting 0: auto mode, 1: manual mode)
	i2c.WriteAddr16Word(0x012a, 0x1800);   // * INCK frequency [MHz] 6,144MHz
	i2c.WriteAddr16Byte(0x0157, 0x00  );   //      ANA_GAIN_GLOBAL_A
	i2c.WriteAddr16Word(0x015A, 0x09BD);   // 0x9bd=2493     COARSE_INTEGRATION_TIME_A
	i2c.WriteAddr16Word(0x0160, 0x0372);   // 0x372= 882     FRM_LENGTH_A
	
#if 0
	i2c.WriteAddr16Word(0x0162, 0x0D78);   // 0xD78=3448     LINE_LENGTH_A (line_length_pck Units: Pixels)  
	i2c.WriteAddr16Word(0x0164, 0x0000);   //      X_ADD_STA_A  x_addr_start  X-address of the top left corner of the visible pixel data Units: Pixels
	i2c.WriteAddr16Word(0x0166, 0x0CCF);   // 0xccf=3279     X_ADD_END_A
	i2c.WriteAddr16Word(0x0168, 0x0000);   //      Y_ADD_STA_A
	i2c.WriteAddr16Word(0x016A, 0x099F);   // 0x99f=2463     Y_ADD_END_A
	i2c.WriteAddr16Word(0x016C, 0x0668);   // 0x668=1640     x_output_size
	i2c.WriteAddr16Word(0x016E, 0x04D0);   // 0x4d0=1232     y_output_size
#else
	i2c.WriteAddr16Word(0x0164, 3280/2 - w);    //      X_ADD_STA_A  x_addr_start  X-address of the top left corner of the visible pixel data Units: Pixels
	i2c.WriteAddr16Word(0x0166, 3280/2 + w-1);  // 0xccf=3279     X_ADD_END_A
	i2c.WriteAddr16Word(0x0168, 2464/2 - h);    //      Y_ADD_STA_A
	i2c.WriteAddr16Word(0x016A, 2464/2 + h-1);  // 0x99f=2463     Y_ADD_END_A
	i2c.WriteAddr16Word(0x016C, w);   // 0x668=1640     x_output_size
	i2c.WriteAddr16Word(0x016E, h);   // 0x4d0=1232     y_output_size
#endif
	
	
	i2c.WriteAddr16Word(0x0170, 0x0101);   //      X_ODD_INC_A  Increment for odd pixels 1, 3
	
//	i2c.WriteAddr16Word(0x0170, 0x0303);   // r     X_ODD_INC_A  Increment for odd pixels 1, 3
//	i2c.WriteAddr16Word(0x0174, 0x0101);   //      BINNING_MODE_H_A  0: no-binning, 1: x2-binning, 2: x4-binning, 3: x2-analog (special) binning
	i2c.WriteAddr16Word(0x0174, 0x0303);   // r     BINNING_MODE_H_A  0: no-binning, 1: x2-binning, 2: x4-binning, 3: x2-analog (special) binning
	i2c.WriteAddr16Word(0x018C, 0x0A0A);   //      CSI_DATA_FORMAT_A   CSI-2 data format
	i2c.WriteAddr16Byte(0x0301, 0x05  );   // * VTPXCK_DIV  Video Timing Pixel Clock Divider Value
	i2c.WriteAddr16Word(0x0303, 0x0103);   // * VTSYCK_DIV  PREPLLCK_VT_DIV(3: EXCK_FREQ 24 MHz to 27 MHz)
	i2c.WriteAddr16Word(0x0305, 0x0300);   // * PREPLLCK_OP_DIV(3: EXCK_FREQ 24 MHz to 27 MHz)  / PLL_VT_MPY 区切りがおかしい次に続く
//	i2c.WriteAddr16Byte(0x0307, 0x39  );   // * PLL_VT_MPY
//	i2c.WriteAddr16Byte(0x0307, 84  );   // r PLL_VT_MPY
	i2c.WriteAddr16Byte(0x0307, 87  );   // r PLL_VT_MPY
	i2c.WriteAddr16Byte(0x0309, 0x0A  );   // * OPPXCK_DIV
	i2c.WriteAddr16Word(0x030B, 0x0100);   // * OPSYCK_DIV PLL_OP_MPY[10:8] / 区切りがおかしい次に続く
	i2c.WriteAddr16Byte(0x030D, 0x72  );   // * PLL_OP_MPY[10:8]
	
	i2c.WriteAddr16Byte(0x455E, 0x00  );   //
	i2c.WriteAddr16Byte(0x471E, 0x4B  );   //
	i2c.WriteAddr16Byte(0x4767, 0x0F  );   //
	i2c.WriteAddr16Byte(0x4750, 0x14  );   //
	i2c.WriteAddr16Byte(0x4540, 0x00  );   //
	i2c.WriteAddr16Byte(0x47B4, 0x14  );   //
	i2c.WriteAddr16Byte(0x4713, 0x30  );   //
	i2c.WriteAddr16Byte(0x478B, 0x10  );   //
	i2c.WriteAddr16Byte(0x478F, 0x10  );   //
	i2c.WriteAddr16Byte(0x4793, 0x10  );   //
	i2c.WriteAddr16Byte(0x4797, 0x0E  );   //
	i2c.WriteAddr16Byte(0x479B, 0x0E  );   //

	i2c.WriteAddr16Byte(0x0172, 0x00  );   //      IMG_ORIENTATION_A
	
//	i2c.WriteAddr16Word(0x0160, 0x06E3);   //      FRM_LENGTH_A[15:8]
//	i2c.WriteAddr16Word(0x0162, 0x0D78);   //      LINE_LENGTH_A
//	i2c.WriteAddr16Word(0x015A, 0x0422);   //      COARSE_INTEGRATION_TIME_A
//	i2c.WriteAddr16Byte(0x0157, 0x00  );   //      ANA_GAIN_GLOBAL_A

//	i2c.WriteAddr16Byte(0x0157, 0x00  );   //      ANA_GAIN_GLOBAL_A
//	i2c.WriteAddr16Word(0x0160, 0x06E3);   //      FRM_LENGTH_A
//	i2c.WriteAddr16Word(0x0162, 0x0D78);   //      LINE_LENGTH_A (line_length_pck Units: Pixels)
//	i2c.WriteAddr16Word(0x015A, 0x0422);   //      COARSE_INTEGRATION_TIME_A

	i2c.WriteAddr16Byte(0x0100, 0x01  );   //      mode_select [4:0] 0: SW standby, 1: Streaming

//	i2c.WriteAddr16Byte(0x0157, 0x00  );   //      ANA_GAIN_GLOBAL_A
//	i2c.WriteAddr16Word(0x0160, 0x06E3);   // 0x06E3=3330   FRM_LENGTH_A
//	i2c.WriteAddr16Word(0x0162, 0x0D78);   // 0x0D78=3448   LINE_LENGTH_A
//	i2c.WriteAddr16Word(0x015A, 0x0421);   // 0x0421=1057   COARSE_INTEGRATION_TIME_A

#if 0
	i2c.WriteAddr16Byte(0x0157, 0x00  );   //      ANA_GAIN_GLOBAL_A
	i2c.WriteAddr16Word(0x0160, 0x0D02);   // 0x0D02=3330   FRM_LENGTH_A
	i2c.WriteAddr16Word(0x0162, 0x0D78);   // 0x0D78=3448   INE_LENGTH_A (line_length_pck Units: Pixels)
	i2c.WriteAddr16Word(0x015A, 0x0D02);   // 0x0D02=3330   COARSE_INTEGRATION_TIME_A
	i2c.WriteAddr16Byte(0x0157, 0xE0  );   //      ANA_GAIN_GLOBAL_A
#else
	i2c.WriteAddr16Byte(0x0157, 0x00  );   //      ANA_GAIN_GLOBAL_A
	i2c.WriteAddr16Word(0x0160, 80);       // 0x0D02=3330   FRM_LENGTH_A
	i2c.WriteAddr16Word(0x0162, 0x0D78);   // 0x0D78=3448   LINE_LENGTH_A (line_length_pck Units: Pixels)
	i2c.WriteAddr16Word(0x015A, 50);       // 0x0D02=3330   COARSE_INTEGRATION_TIME_A
	i2c.WriteAddr16Byte(0x0157, 0xE0  );   //      ANA_GAIN_GLOBAL_A
//	i2c.WriteAddr16Byte(0x0157, 0xFF  );   //      ANA_GAIN_GLOBAL_A
	i2c.WriteAddr16Word(0x0158, 0x0FFF);   //      ANA_GAIN_GLOBAL_A
#endif
	
	
	
//	int width  = 640; // IMAGE_WIDTH;
//	int height = 120; // IMAGE_HEIGHT;
//	int width  = 0x48e; // 640; // IMAGE_WIDTH;
//	int height = 0xD78; // 120; // IMAGE_HEIGHT;
	int width  = w;
	int height = h; // IMAGE_HEIGHT / 2;
	
	int mnist_th  = 127;
	int mcol_mode = 2;
	int mcol_th   = 3;
	
	void* mem_addr = um_pl_mem.GetAddress();
	
	{
		int		frame_num = 1;
		int		key;
		while ( (key = (cv::waitKey(10) & 0xff)) != 0x1b ) {
			cv::Mat img(height*frame_num, width, CV_8UC4);
			memcpy(img.data, (void *)mem_addr, width * height * 4 * frame_num);
			cv::imshow("img", img);
			cv::imwrite("img.png", img);
			cv::createTrackbar("width",  "img", &width,     IMAGE_WIDTH);
			cv::createTrackbar("height", "img", &height,    IMAGE_HEIGHT);
	//		cv::createTrackbar("frame",  "img", &frame_num, 10);
			cv::createTrackbar("m_th",   "img", &mnist_th, 255);
			cv::createTrackbar("mode",   "img", &mcol_mode, 3);
			cv::createTrackbar("col_th", "img", &mcol_th, 8);
			
			width &= 0xfffffff0;
			if ( width  < 16 ) { width  = 16; }
			if ( height < 2 )  { height = 2; }
			
			capture_still_image(um_pl_peri, width, height, frame_num);
			
			um_pl_peri.WriteWord32(0x00018000, mnist_th);
			um_pl_peri.WriteWord32(0x00019000, mcol_mode);
			um_pl_peri.WriteWord32(0x00019004, mcol_th);
			
			if ( key == 'r' ) {
				printf("record\n");
				capture_still_image(um_pl_peri, width, height, 100);
				char* p = (char*)mem_addr;
				for ( int i = 0; i< 100; i++ ) {
					char fname[64];
					sprintf(fname, "rec_%04d.png", i);
					cv::Mat imgRec(height, width, CV_8UC4);
					memcpy(imgRec.data, p, width * height * 4);	p += width * height * 4;
					cv::Mat imgRgb;
					cv::cvtColor(imgRec, imgRgb, CV_BGRA2BGR);
					cv::imwrite(fname, imgRgb);
				}
			}
		}
	}
	
	return 0;
}