Beispiel #1
0
void ssd1306_init(ssd1306_t *ssd, i2c_dev_t *i2c_dev) 
{
   assert(ssd);
   assert(i2c_dev);
   
   ssd->i2c_dev = i2c_dev;
   ssd->width  = 128;
   ssd->height = 64;
   ssd->buf = (uint8_t *)malloc((ssd->width * ssd->height / 8)); 

   ssd1306_cmd1(ssd, SSD_DISPLAY_OFF);
   ssd1306_cmd2(ssd, SSD1306_SETDISPLAYCLOCKDIV, 0x80);
   ssd1306_cmd2(ssd, SSD1306_SETMULTIPLEX, 0x3F);
   ssd1306_cmd2(ssd, SSD1306_SETDISPLAYOFFSET, 0x00);
   ssd1306_cmd1(ssd, SSD1306_SETSTARTLINE);
   ssd1306_cmd2(ssd, SSD1306_CHARGEPUMP, 0x14); 
   ssd1306_cmd2(ssd, SSD1306_MEMORYMODE, 0x00);
   ssd1306_cmd1(ssd, SSD1306_SEGREMAP | 0x1);
   ssd1306_cmd1(ssd, SSD1306_COMSCANDEC);
   ssd1306_cmd2(ssd, SSD1306_SETCOMPINS, 0x12);
   ssd1306_cmd2(ssd, SSD_SET_CONTRAST, 0xCF);
   ssd1306_cmd2(ssd, SSD1306_SETPRECHARGE, 0xF1);
   ssd1306_cmd2(ssd, SSD1306_SETVCOMDETECT, 0x40);
   ssd1306_cmd1(ssd, SSD1306_DISPLAYALLON_RESUME);
   ssd1306_cmd1(ssd, SSD1306_Normal_Display);

   ssd1306_cmd3(ssd, 0x21, 0, 127); 
   ssd1306_cmd3(ssd, 0x22, 0, 7); 
   ssd1306_cmd1(ssd, SSD_DEACTIVATE_SCROLL);
   
   ssd1306_clear(ssd);
   ssd1306_update(ssd);
   ssd1306_cmd1(ssd, SSD_DISPLAY_ON);
}
Beispiel #2
0
void ssd1306_init(I2C_HandleTypeDef* hi2c)
{
    ssd1306_sendCmd(hi2c, SSD1306_DISPLAY_OFF);
    ssd1306_sendCmd(hi2c, SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO);
    ssd1306_sendCmd(hi2c, 0x80);
    ssd1306_sendCmd(hi2c, SSD1306_SET_MULTIPLEX_RATIO);
    ssd1306_sendCmd(hi2c, 0x3F);
    ssd1306_sendCmd(hi2c, SSD1306_SET_DISPLAY_OFFSET);
    ssd1306_sendCmd(hi2c, 0x0);
    ssd1306_sendCmd(hi2c, SSD1306_SET_START_LINE | 0x0);
    ssd1306_sendCmd(hi2c, SSD1306_CHARGE_PUMP);
	ssd1306_sendCmd(hi2c, 0x14);
    ssd1306_sendCmd(hi2c, SSD1306_MEMORY_ADDR_MODE);
    ssd1306_sendCmd(hi2c, 0x00);
    ssd1306_sendCmd(hi2c, SSD1306_SET_SEGMENT_REMAP | 0x1);
    ssd1306_sendCmd(hi2c, SSD1306_COM_SCAN_DIR_DEC);
    ssd1306_sendCmd(hi2c, SSD1306_SET_COM_PINS);
    ssd1306_sendCmd(hi2c, 0x12);
    ssd1306_sendCmd(hi2c, SSD1306_SET_CONTRAST_CONTROL);
	ssd1306_sendCmd(hi2c, 0xCF);
    ssd1306_sendCmd(hi2c, SSD1306_SET_PRECHARGE_PERIOD);
	ssd1306_sendCmd(hi2c, 0xF1);
    ssd1306_sendCmd(hi2c, SSD1306_SET_VCOM_DESELECT);
    ssd1306_sendCmd(hi2c, 0x40);
    ssd1306_sendCmd(hi2c, SSD1306_DISPLAY_ALL_ON_RESUME);
    ssd1306_sendCmd(hi2c, SSD1306_NORMAL_DISPLAY);
	ssd1306_sendCmd(hi2c, SSD1306_DISPLAY_ON);

    ssd1306_clear();
    ssd1306_update(hi2c);
}
Beispiel #3
0
void ssd1306_init(const unsigned char dc, const unsigned char rst)
{
    _dc = dc;
    _rst = rst;

    pinMode(_dc, OUTPUT);
    pinMode(_rst, OUTPUT);

    digitalWrite(_rst, LOW);
    delay(100);
    digitalWrite(_rst, HIGH);

    ssd1306_command(0xAE); // turn off the screen

    ssd1306_command(0xD5); // set display clock divide ratio/oscillator frequency
    ssd1306_command(0x80); // suggested ratio: 0x80

    ssd1306_command(0xA8); // set multiplex
    ssd1306_command(0x3F); // height: 64

    ssd1306_command(0xD3); // set display offset
    ssd1306_command(0x00); // offset: 0

    ssd1306_command(0x40 | 0x0); // set start line

    ssd1306_command(0x8D); // set charge pump
    ssd1306_command(0x14); // charge pump: disable

    ssd1306_command(0x20); // set memory mode
    ssd1306_command(0x00); // mode: 0

    ssd1306_command(0xA0 | 0x1); // set seg/column mapping

    ssd1306_command(0xC8); // set com/row scan direction

    ssd1306_command(0xDA); // set com pins hardware configuration
    ssd1306_command(0x12); // compins: 0x12

    ssd1306_command(0x81); // set contrast control register
    ssd1306_command(0xCF); // brightness: 0xCF

    ssd1306_command(0xD9); // set pre-charge period
    ssd1306_command(0xF1); // pre-charge: 15 clocks / discharge: 1 clock

    ssd1306_command(0XDB); // set vcom detect
    ssd1306_command(0x40); // vcom deselect level: 0x40

    ssd1306_command(0xA4); // disable entire display

    ssd1306_command(0xA6); // disable inverse display (normal display)

    ssd1306_command(0xAF); // turn on the screen

    ssd1306_clear();
    ssd1306_set_coordinate(0, 0);
}
Beispiel #4
0
void ICACHE_FLASH_ATTR magnet_timer_cb() {
	vec3s16 field;
	ak8963_get_field(&field);
	field.x -= calib_bias.x;
	field.y -= calib_bias.y;
	field.z -= calib_bias.z;
	//os_printf("Measurement: { x = %d, y = %d, z = %d }\r\n", (int16_t)(field.x), (int16_t)(field.y), (int16_t)(field.z));
	angles2f north = vec3s16_to_angles2f(field);
	//os_printf("North: { yaw = %d, pitch = %d }\r\n", (int16_t)(north.yaw * 180 / PI), (int16_t)(north.pitch * 180 / PI));
	ak8963_set_opmode(AK8963_SINGLE, AK8963_16BIT);

	ssd1306_clear();
	compass_draw(north.yaw);
	ssd1306_line(70, 0, 70, 63, true);
	ssd1306_text_small(75, 24, "Inclin.:");
	char incdeg[6];
	os_sprintf(incdeg, "%d°", (int16_t)(north.pitch * 180 / PI));
	ssd1306_text_small(75, 32, incdeg);
	ssd1306_flip();
}
Beispiel #5
0
void clear(void)
{
   ASSERT_INIT();
   ssd1306_clear(&ssd);
}
bool ICACHE_FLASH_ATTR ssd1306_init(uint8_t id)
{
    oled_i2c_ctx *ctx = NULL;

    if ((id != 0) && (id != 1))
        goto oled_init_fail;

    // free old context (if any)
    ssd1306_term(id);

    ctx = zalloc(sizeof(oled_i2c_ctx));
    if (ctx == NULL)
    {
        dmsg_err_puts("Alloc OLED context failed.");
        goto oled_init_fail;
    }
    if (id == 0)
    {
#if (PANEL0_TYPE != 0)
  #if (PANEL0_TYPE == SSD1306_128x64)
        ctx->type = SSD1306_128x64;
        ctx->buffer = zalloc(1024); // 128 * 64 / 8
        ctx->width = 128;
        ctx->height = 64;
  #elif (PANEL0_TYPE == SSD1306_128x32)
        ctx->type = SSD1306_128x32;
        ctx->buffer = zalloc(512);  // 128 * 32 / 8
        ctx->width = 128;
        ctx->height = 32;
  #else
    #error "Panel 0 undefined"
  #endif
        if (ctx->buffer == NULL)
        {
            dmsg_err_puts("Alloc OLED buffer failed.");
            goto oled_init_fail;
        }
        ctx->address = PANEL0_ADDR;
  #if PANEL0_USE_RST
        // Panel 0 reset
        PIN_FUNC_SELECT(PANEL0_RST_MUX, PANEL0_RST_FUNC);
        GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | PANEL0_RST_BIT);
        GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, PANEL0_RST_BIT);
        os_delay_us(10000);
        GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, PANEL0_RST_BIT);
  #endif
#else
        dmsg_err_puts("Panel 0 not defined.");
        goto oled_init_fail;
#endif
    }
    else if (id == 1)
    {
#if (PANEL1_PANEL_TYPE != 0)
  #if (PANEL1_PANEL_TYPE ==SSD1306_128x64)
        ctx->type = SSD1306_128x64;
        ctx->buffer = zalloc(1024); // 128 * 64 / 8
        ctx->width = 128;
        ctx->height = 64;
  #elif (PANEL1_PANEL_TYPE == SSD1306_128x32)
        ctx->type = SSD1306_128x32;
        ctx->buffer = zalloc(512);  // 128 * 32 / 8
        ctx->width = 128;
        ctx->height = 32;
  #else
     #error "Unknown Panel 1 type"
  #endif
        if (ctx->buffer == NULL)
        {
            dmsg_err_puts("Alloc OLED buffer failed.");
            goto oled_init_fail;
        }
        ctx->address = PANEL1_ADDR;
  #if PANEL1_USE_RST
        // Panel 1 reset
        PIN_FUNC_SELECT(PANEL1_RST_MUX, PANEL1_RST_FUNC);
        GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | PANEL1_RST_BIT);
        GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, PANEL1_RST_BIT);
        os_delay_us(10000);
        GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, PANEL1_RST_BIT);
  #endif
#else
        dmsg_err_puts("Panel 1 not defined.");
        goto oled_init_fail;
#endif
    }

    // Panel initialization
    // Try send I2C address check if the panel is connected
    i2c_start();
    if (!i2c_write(ctx->address))
    {
        i2c_stop();
        dmsg_err_puts("OLED I2C bus not responding.");
        goto oled_init_fail;
    }
    i2c_stop();

    // Now we assume all sending will be successful
    if (ctx->type == SSD1306_128x64)
    {
        _command(ctx->address, 0xae); // SSD1306_DISPLAYOFF
        _command(ctx->address, 0xd5); // SSD1306_SETDISPLAYCLOCKDIV
        _command(ctx->address, 0x80); // Suggested value 0x80
        _command(ctx->address, 0xa8); // SSD1306_SETMULTIPLEX
        _command(ctx->address, 0x3f); // 1/64
        _command(ctx->address, 0xd3); // SSD1306_SETDISPLAYOFFSET
        _command(ctx->address, 0x00); // 0 no offset
        _command(ctx->address, 0x40); // SSD1306_SETSTARTLINE line #0
        _command(ctx->address, 0x20); // SSD1306_MEMORYMODE
        _command(ctx->address, 0x00); // 0x0 act like ks0108
        _command(ctx->address, 0xa1); // SSD1306_SEGREMAP | 1
        _command(ctx->address, 0xc8); // SSD1306_COMSCANDEC
        _command(ctx->address, 0xda); // SSD1306_SETCOMPINS
        _command(ctx->address, 0x12);
        _command(ctx->address, 0x81); // SSD1306_SETCONTRAST
        _command(ctx->address, 0xcf);
        _command(ctx->address, 0xd9); // SSD1306_SETPRECHARGE
        _command(ctx->address, 0xf1);
        _command(ctx->address, 0xdb); // SSD1306_SETVCOMDETECT
        _command(ctx->address, 0x30);
        _command(ctx->address, 0x8d); // SSD1306_CHARGEPUMP
        _command(ctx->address, 0x14); // Charge pump on
        _command(ctx->address, 0x2e); // SSD1306_DEACTIVATE_SCROLL
        _command(ctx->address, 0xa4); // SSD1306_DISPLAYALLON_RESUME
        _command(ctx->address, 0xa6); // SSD1306_NORMALDISPLAY
    }
    else if (ctx->type == SSD1306_128x32)
    {
        _command(ctx->address, 0xae); // SSD1306_DISPLAYOFF
        _command(ctx->address, 0xd5); // SSD1306_SETDISPLAYCLOCKDIV
        _command(ctx->address, 0x80); // Suggested value 0x80
        _command(ctx->address, 0xa8); // SSD1306_SETMULTIPLEX
        _command(ctx->address, 0x1f); // 1/32
        _command(ctx->address, 0xd3); // SSD1306_SETDISPLAYOFFSET
        _command(ctx->address, 0x00); // 0 no offset
        _command(ctx->address, 0x40); // SSD1306_SETSTARTLINE line #0
        _command(ctx->address, 0x8d); // SSD1306_CHARGEPUMP
        _command(ctx->address, 0x14); // Charge pump on
        _command(ctx->address, 0x20); // SSD1306_MEMORYMODE
        _command(ctx->address, 0x00); // 0x0 act like ks0108
        _command(ctx->address, 0xa1); // SSD1306_SEGREMAP | 1
        _command(ctx->address, 0xc8); // SSD1306_COMSCANDEC
        _command(ctx->address, 0xda); // SSD1306_SETCOMPINS
        _command(ctx->address, 0x02);
        _command(ctx->address, 0x81); // SSD1306_SETCONTRAST
        _command(ctx->address, 0x2f);
        _command(ctx->address, 0xd9); // SSD1306_SETPRECHARGE
        _command(ctx->address, 0xf1);
        _command(ctx->address, 0xdb); // SSD1306_SETVCOMDETECT
        _command(ctx->address, 0x40);
        _command(ctx->address, 0x2e); // SSD1306_DEACTIVATE_SCROLL
        _command(ctx->address, 0xa4); // SSD1306_DISPLAYALLON_RESUME
        _command(ctx->address, 0xa6); // SSD1306_NORMALDISPLAY
    }
    // Save context
    ctx->id = id;
    _ctxs[id] = ctx;

    ssd1306_clear(id);
    ssd1306_refresh(id, true);

    _command(ctx->address, 0xaf); // SSD1306_DISPLAYON

    return true;

oled_init_fail:
    if (ctx && ctx->buffer) free(ctx->buffer);
    if (ctx) free(ctx);
    return false;
}
Beispiel #7
0
int
main(int argc, char * argv[])
{
	printf("Starting SSD1306 Test\n");

	int spiChannel = 0;
	int resetPin = 17;
	int dcPin = 21;
	printf("Using SPI Channel %d\n", spiChannel);
	printf("Reset Pin = %d, DC Pin = %d\n", resetPin, dcPin);


	/*
	 * 10 MHz did work on a breadboard
	 * safer to use slower speeds probably
	 * 200 kHz is the approximate minimum
	 * otherwise not all data will be transmitted
	 */

	int clock = 200000;
	SSD1306 * disp = ssd1306_create(spiChannel, resetPin, dcPin, clock);

	printf("Init\n");
	ssd1306_init(disp, SSD1306_ROWS_64, SSD1306_VCCSATE_SWITCHCAP);

	int y = 0;
	int x = 0;
	for (; x < 100; x += 1) {
		for (y = 0; y < 10; ++y) {
			ssd1306_setPixel(disp, x, y, 1);
		}
	}


	for (x = 0; x < 100; x += 10) {
		for (y = 0; y < 10; ++y) {
			ssd1306_setPixel(disp, x, y, 0);
		}
	}

	printf("Display 10 white Boxes\n");
	ssd1306_display(disp);
	ssd1306_delay(1000);

	printf("Clear\n");
	ssd1306_clear(disp, 0);

	printf("Draw Lines\n");
	int i;
	for (i=0; i < 128; i+=4) {
		ssd1306_drawLine(disp, 0, 0, i, 64-1, 1);
		ssd1306_display(disp);
	}

	for (i=0; i < 64; i+=4) {
		ssd1306_drawLine(disp, 0, 0, 128-1, i, 1);
		ssd1306_display(disp);
	}

	printf("Start Scrool Right\n");
	ssd1306_startScrollRight(disp, 0x00, 0x0F);
	ssd1306_delay(2000);
	ssd1306_stopScroll(disp);
	ssd1306_delay(1000);

	printf("Start Scrool Left\n");
	ssd1306_startScrollLeft(disp, 0x00, 0x0F);
	ssd1306_delay(2000);
	ssd1306_stopScroll(disp);
	ssd1306_delay(1000);

	ssd1306_destroy(disp);

	return 0;
}
Beispiel #8
0
PROCESS_THREAD(blink_process, ev, data)
{

  static struct etimer et_blink;
  etimer_set(&et_blink, CLOCK_SECOND);
  PROCESS_BEGIN();
static char lux_value[6];
static char lux_value2[6];
static char raw_value[6];
static char raw_value2[6];
static  char temperature_val[6];
static  uint16_t  adc_data=0;
static  uint16_t  adc_data2=0;
static  float adc_lux=0;
static  float adc_lux2=0;
static  float measured_temp=0;
//  static int blinks = 0;

  ssd1306_clear(); 
  ssd1306_set_page_address(0);
  ssd1306_set_column_address(2);
  ssd1306_write_text("Lumen in Lux:");
   
  ssd1306_set_page_address(1);
  ssd1306_set_column_address(2);
  ssd1306_write_text("Raw ADC out :");
 
  ssd1306_set_page_address(2);
  ssd1306_set_column_address(2);
  ssd1306_write_text("Ambient Temperature measured:");


  while(1) {

    PROCESS_WAIT_EVENT();
if (ev==PROCESS_EVENT_TIMER)
   {
   DDRB |=(1<<PORTB4);
   PORTB ^= (1<<PORTB4);
   adc_init();
   adc_data=get_adc(0);
   adc_lux=adc_data*0.9765625;
     
 /*
   amps=adc_volt/10000.0;
   microamps=amps/1000000;
   lux_data=microamps*2;
*/
  itoa(adc_lux, lux_value, 10);
  ssd1306_set_page_address(0);
  ssd1306_set_column_address(73);
  ssd1306_write_text(lux_value);

  itoa(adc_data, raw_value, 10);
  ssd1306_set_page_address(1);
  ssd1306_set_column_address(73);
  ssd1306_write_text(raw_value);

  adc_init_full(ADC_CHAN_ADC0, ADC_TRIG_FREE_RUN, ADC_REF_AVCC, ADC_PS_64);
  adc_conversion_start();
 
  adc_lux2=adc_data2*0.9765626;

  itoa(adc_lux2, lux_value2, 10);
  ssd1306_set_page_address(0);
  ssd1306_set_column_address(95);
  ssd1306_write_text(lux_value2);

  itoa(adc_data2, raw_value2, 10);
  ssd1306_set_page_address(1);
  ssd1306_set_column_address(95);
  ssd1306_write_text(raw_value2);

  measured_temp=ReadTempVal();
  itoa(measured_temp, temperature_val, 10);
  ssd1306_set_page_address(2);
  ssd1306_set_column_address(95);
  ssd1306_write_text(temperature_val);

  }


  }

  PROCESS_END();
}
Beispiel #9
0
int main(void)
{
	//
	asm("nop");
	rtc_init();


	//changing the clock to 32MHz
	enable_oscillator(OSC_RC32MEN_bm);
	sysclk_prescaler(CLK_PSADIV_1_gc,CLK_PSBCDIV_1_1_gc);
	sysclk_source(CLK_SCLKSEL_RC32M_gc);

	usart_config(&USARTC0,&PORTC,USART_CMODE_ASYNCHRONOUS_gc,USART_PMODE_DISABLED_gc,USART_CHSIZE_8BIT_gc,false,false);
	usart_baud(&USARTC0,9600,-3);
	FILE my_stdio;
	create_file_stream(&my_stdio,&USARTC0_putchar,&USARTC0_getchar);
	set_stdio_stream(&my_stdio);



	port_direction_set_mask(&PORTR,1<<0);

	uint8_t page;
	uint8_t column;

	gfx_mono_set_framebuffer(framebuffer);

	ssd1306_init();
	ssd1306_clear();

	ssd1306_set_display_start_line_address(0);


	for (page = 0; page < GFX_MONO_LCD_PAGES; page++) {
		for (column = 0; column < GFX_MONO_LCD_WIDTH; column++) {
			gfx_mono_ssd1306_put_byte(page, column, 0x00, 1);
		}
	}

	ssd1306_set_page_address(0);
	ssd1306_write_text("EMON");

	rtc_ms_delay(1000);

	lcd_line_print(0,"EMON YOU ROCK");

	esp8266_serial_init();

	printf("starting system\n");
	
	printf("-----------------------------------------------------------\n");

#define mqtt_callback_on_message "function(conn, topic, data) print(topic .. \":\" ) if data ~= nil then print(data) end end"

// 
	//esp8266_getmode();
	esp8266_setmode(ESP8266_WIFI_MODE_STATION);
	esp8266_available_AP_t myAPdata[10];
	int n = esp8266_sta_getap(3000,myAPdata);
	
	for(int i=0;i<n;i++){
		printf("available ssid : %s\n" , myAPdata[i].BSSID);
	}
	
	
	esp8266_sta_config("Emon","19031992",NULL,NULL);
	printf("\nthe wifi mode is %d\n", esp8266_getmode());
	esp8266_sta_getmac();

 
  	//esp8266_uart_setup(0,9600,8,ESP8266_UART_PARITY_NONE,ESP8266_UART_STOPBITS_1,0);
// 
 	esp8266_sta_getip();
 	esp8266_sta_get_status();
	lcd_line_print(0,"HABIBUR RAHMAN");
// 	
	esp8266_mqtt_create_client("m","clientid", 30 , "user", "password");
	esp8266_mqtt_client_connect("m","emon.dlinkddns.com",9000,0,"function(conn) print(\"m:connected\") end");
	esp8266_mqtt_client_subscribe("m","sky2",0,"function(conn) print(\"subscribed\") end");
	esp8266_mqtt_client_callback_on_message("m",mqtt_callback_on_message);
	
	
	esp8266_mqtt_create_client("n","clientid", 30 , "user", "password");
	esp8266_mqtt_client_connect("n","broker.hivemq.com",1883,0,"function(conn) print(\"n:connected\") end");
	esp8266_mqtt_client_subscribe("n","dog",0,"function(conn) print(\"subscribed\") end");
	esp8266_mqtt_client_callback_on_message("n",mqtt_callback_on_message);
	
	int i=0;
	char numstr[30];
	char mydata[100];  
	char mydata2[100]; 
	
	char tempcmd[50]; 
	char tempdata[100];  
	while (1)
    {

		esp8266_rx_buff_receive(mydata);
		if(!strcmp(mydata, "sky2:\r\nkaminey\r\n")){
			printf("\n\n******f**k you*****\n\n");
			
			esp8266_mqtt_client_publish("m","sky","test reply .. by emon",0,0,"function(conn) print(\"sent\") end");
			printf(mydata2);
		}
		else if(!strcmp(mydata, "m:connected\r\n")){
			printf("RECONNECTING NOWWWWW!!!");
			
			esp8266_mqtt_client_subscribe("m","sky2",0,"function(conn) print(\"subscribed\") end");

		}
		else if(!strcmp(mydata, "n:connected\r\n")){
			printf("RECONNECTING NOW!!!");
					
			esp8266_mqtt_client_subscribe("n","dog",0,"function(conn) print(\"subscribed\") end");

		}
		printf(mydata);
		ssd1306_clear();
		ssd1306_set_page_address(0);
		ssd1306_write_text(mydata);
		
		
	}

}
Beispiel #10
0
void lcd_line_print(int line, char *str){
	ssd1306_clear();
	ssd1306_set_page_address(line);
	ssd1306_set_column_address(0);
	ssd1306_write_text(str);
}
Beispiel #11
0
/**
 * \brief The main application.
 */
int main(void)
{
	uint8_t i;
	uint8_t temperature[BUFFER_SIZE];
	uint8_t light[BUFFER_SIZE];
	char value_disp[5];
	uint32_t adc_value;
	uint32_t light_value;
	double temp;

	/* Initialize clocks. */
	sysclk_init();

	/* Initialize GPIO states. */
	board_init();

	/* Configure ADC for light sensor. */
	configure_adc();

	/* Initialize at30tse. */
	at30tse_init();

	/* Configure IO1 buttons. */
	configure_buttons();

	/* Initialize SPI and SSD1306 controller. */
	ssd1306_init();
	ssd1306_clear();

	/* Clear internal buffers. */
	for (i = 0; i < BUFFER_SIZE; ++i) {
		temperature[i] = 0;
		light[i] = 0;
	}

	/* Show the start info. */
	multi_language_show_start_info();

	/* Wait 3 seconds to show the above message. */
	delay_s(3);

	/* Check for valid firmware in SD card. */
	check_valid_firmware();

	while (true) {
		/* Set the trigger and jump to bootloader */
		if (reset_flag) {
			jump_to_bootloader();
		}

		/* Refresh page title only if necessary. */
		if (app_mode_switch > 0) {
			app_mode = (app_mode + 1) % 3;

			/* Clear screen. */
			ssd1306_clear();
			ssd1306_set_page_address(0);
			ssd1306_set_column_address(0);

			if (app_mode == 0) {
				/* Temperature mode. */
				ioport_set_pin_level(OLED1_LED1_PIN, OLED1_LED1_ACTIVE);
				ioport_set_pin_level(OLED1_LED2_PIN, !OLED1_LED2_ACTIVE);
				ioport_set_pin_level(OLED1_LED3_PIN, !OLED1_LED3_ACTIVE);
				multi_language_show_temperature_info();
			} else if (app_mode == 1) {
				/* Light mode. */
				ioport_set_pin_level(OLED1_LED2_PIN, OLED1_LED2_ACTIVE);
				ioport_set_pin_level(OLED1_LED1_PIN, !OLED1_LED1_ACTIVE);
				ioport_set_pin_level(OLED1_LED3_PIN, !OLED1_LED3_ACTIVE);
				multi_language_show_light_info();
			} else {
				/* SD mode. */
				ioport_set_pin_level(OLED1_LED3_PIN, OLED1_LED3_ACTIVE);
				ioport_set_pin_level(OLED1_LED1_PIN, !OLED1_LED1_ACTIVE);
				ioport_set_pin_level(OLED1_LED2_PIN, !OLED1_LED2_ACTIVE);

				sd_listing_pos = 0;
				/* Show SD card info. */
				display_sd_info();
			}

			app_mode_switch = 0;
		}

		/* Shift graph buffers. */
		for (i = 0; i < (BUFFER_SIZE - 1); ++i) {
			temperature[i] = temperature[i + 1];
			light[i] = light[i + 1];
		}

		/* Get temperature. */
		if (at30tse_read_temperature(&temp) == TWI_SUCCESS) {
			/* Don't care about negative temperature. */
			if (temp < 0) {
				temp = 0;
			}

			/* Update temperature for display. */
			/* Note: rescale to 0~24 for better rendering. */
			if (temp > 40) {
				temperature[BUFFER_SIZE - 1] = 24;
			} else {
				temperature[BUFFER_SIZE - 1] = (uint8_t)temp * 24 / 40;
			}
		} else {
			/* Error print zero values. */
			temperature[BUFFER_SIZE - 1] = 0;
		}

		/* Get light sensor information. */
		/* Rescale to 0~24 for better rendering. */
		adc_start_software_conversion(ADC);
		adc_value = adc_channel_get_value(ADC, ADC_CHANNEL_0);
		light[BUFFER_SIZE - 1] = 24 - adc_value * 24 / 1024;

		if (app_mode == 0) {
			/* Display temperature in text format. */
			sprintf(value_disp, "%d", (uint8_t)temp);
			ssd1306_set_column_address(98);
			ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0));
			ssd1306_write_text(" ");
			/* Avoid character overlapping. */
			if (temp < 10) {
				ssd1306_clear_char();
			}

			ssd1306_write_text(value_disp);
			/* Display degree symbol. */
			ssd1306_write_data(0x06);
			ssd1306_write_data(0x06);
			ssd1306_write_text("c");

			/* Refresh graph. */
			ssd1306_draw_graph(0, 2, BUFFER_SIZE, 2, temperature);
		} else if (app_mode == 1) {
			light_value = 100 - (adc_value * 100 / 1024);
			sprintf(value_disp, "%lu", light_value);
			ssd1306_set_column_address(98);
			ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0));
			ssd1306_write_text(" ");
			/* Avoid character overlapping. */
			if (light_value < 10) {
				ssd1306_clear_char();
			}

			ssd1306_write_text(value_disp);
			ssd1306_write_text("%");
			/* Avoid character overlapping. */
			if (light_value < 100) {
				ssd1306_clear_char();
			}

			/* Refresh graph. */
			ssd1306_draw_graph(0, 2, BUFFER_SIZE, 2, light);
		} else {
			/**
			 * Refresh screen if card was inserted/removed or
			 * browsing content.
			 */
			if (sd_update == 1) {
				/* Clear screen. */
				ssd1306_clear();
				ssd1306_set_page_address(0);
				ssd1306_set_column_address(0);

				if (sd_listing_pos == 0) {
					/* Show SD card info. */
					display_sd_info();
				} else {
					/* List SD card files. */
					display_sd_files_unicode();
				}

				sd_update = 0;
			}
		}

		/* Wait and stop screen flickers. */
		delay_ms(150);

		if (app_mode_switch == 0) {
			pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_1_PIO,
					OLED1_PIN_PUSHBUTTON_1_MASK);
		}
		if (sd_update == 0) {
			pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_2_PIO,
					OLED1_PIN_PUSHBUTTON_2_MASK);
			pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_3_PIO,
					OLED1_PIN_PUSHBUTTON_3_MASK);
		}

	}
}
Beispiel #12
0
/**
 * \brief Set the trigger for bootloader and perform a software reset to let
 * the bootloader to perform firmware update process.
 */
static void jump_to_bootloader(void)
{
	uint32_t ul_test_page_addr = TEST_PAGE_ADDRESS;
	uint32_t *pul_test_page = (uint32_t *)ul_test_page_addr;
	uint32_t ul_rc;
	uint32_t ul_idx;
	uint32_t temp_data;
	uint32_t i;

	for (ul_idx = 0; ul_idx < (IFLASH_PAGE_SIZE / 4); ul_idx++) {
		temp_data = pul_test_page[ul_idx];
		trigger_page[ul_idx * 4] = (uint8_t)(temp_data & 0xFF);
		trigger_page[ul_idx * 4 + 1] = (uint8_t)((temp_data >> 8) & 0xFF);
		trigger_page[ul_idx * 4 + 2] = (uint8_t)((temp_data >> 16) & 0xFF);
		trigger_page[ul_idx * 4 + 3] = (uint8_t)((temp_data >> 24) & 0xFF);
	}

	/* Set the trigger */
	trigger_page[0x14] = 0x01;
	trigger_page[0x15] = 0x00;
	trigger_page[0x16] = 0x00;
	trigger_page[0x17] = 0x00;

	for (i = 0; i < LANGUAGE_NUMBER; i++) {
		app_language++;
		app_language = app_language % LANGUAGE_NUMBER;
		/* Fill in firmware type used in bootloader */
		if (firmware_type & (0x01 << app_language)) {
			trigger_page[0x18] = file_name_ascii[app_language][0];
			trigger_page[0x19] = file_name_ascii[app_language][1];
			trigger_page[0x1A] = file_name_ascii[app_language][2];
			trigger_page[0x1B] = file_name_ascii[app_language][3];
			trigger_page[0x1C] = file_name_ascii[app_language][4];
			trigger_page[0x1D] = file_name_ascii[app_language][5];
			trigger_page[0x1E] = file_name_ascii[app_language][6];
			trigger_page[0x1F] = file_name_ascii[app_language][7];
			trigger_page[0x20] = file_name_ascii[app_language][8];
			trigger_page[0x21] = file_name_ascii[app_language][9];
			trigger_page[0x22] = file_name_ascii[app_language][10];
			trigger_page[0x23] = file_name_ascii[app_language][11];
			break;
		}
	}

	ul_rc = flash_unlock(ul_test_page_addr,
			ul_test_page_addr + IFLASH_PAGE_SIZE, NULL, NULL);

	ul_rc = flash_erase_page(ul_test_page_addr, IFLASH_ERASE_PAGES_8);
	if (ul_rc != FLASH_RC_OK) {
		return;
	}

	ul_rc = flash_write(ul_test_page_addr, trigger_page,
			IFLASH_PAGE_SIZE, 0);
	if (ul_rc != FLASH_RC_OK) {
		return;
	}

	/* Clear screen. */
	ssd1306_clear();

	/* End message on the screen. */
	multi_language_show_end_info();

	/* Perform the software reset. */
	rstc_start_software_reset(RSTC);

	/* Wait for reset. */
	delay_ms(100);
}
Beispiel #13
0
int main(void)
{
	uint8_t i;
	uint8_t temperature[BUFFER_SIZE];
	uint8_t light[BUFFER_SIZE];
	uint8_t value_disp[5];
	uint32_t adc_value;
	double temp;

	// Initialize clocks.
	sysclk_init();

	// Initialize GPIO states.
	board_init();

	// Configure ADC for light sensor.
	configure_adc();

	// Initialize at30tse.
	at30tse_init();

	// Configure IO1 buttons.
	configure_buttons();

	// Initialize SPI and SSD1306 controller.
	ssd1306_init();
	ssd1306_clear();

	// Clear internal buffers.
	for (i = 0; i < BUFFER_SIZE; ++i)
	{
		temperature[i] = 0;
		light[i] = 0;
	}

	while (true)
	{
		/* Refresh page title only if necessary. */
		if (app_mode_switch > 0)
		{
			app_mode = app_mode_switch - 1;

			// Clear screen.
			ssd1306_clear();
			ssd1306_set_page_address(0);
			ssd1306_set_column_address(0);

			/* Temperature mode. */
			if (app_mode == 0)
			{
				ioport_set_pin_level(IO1_LED1_PIN, IO1_LED1_ACTIVE);
				ioport_set_pin_level(IO1_LED2_PIN, !IO1_LED2_ACTIVE);
				ioport_set_pin_level(IO1_LED3_PIN, !IO1_LED3_ACTIVE);
				ssd1306_write_text("Temperature sensor:");
			}
			/* Light mode. */
			else if (app_mode == 1)
			{
				ioport_set_pin_level(IO1_LED2_PIN, IO1_LED2_ACTIVE);
				ioport_set_pin_level(IO1_LED1_PIN, !IO1_LED1_ACTIVE);
				ioport_set_pin_level(IO1_LED3_PIN, !IO1_LED3_ACTIVE);
				ssd1306_write_text("Light sensor:");
			}
			/* SD mode. */
			else
			{
				ioport_set_pin_level(IO1_LED3_PIN, IO1_LED3_ACTIVE);
				ioport_set_pin_level(IO1_LED1_PIN, !IO1_LED1_ACTIVE);
				ioport_set_pin_level(IO1_LED2_PIN, !IO1_LED2_ACTIVE);

				display_sd_info();
			}
			app_mode_switch = 0;
		}

		// Shift graph buffers.
		for (i = 0; i < BUFFER_SIZE - 1; ++i)
		{
			temperature[i] = temperature[i + 1];
			light[i] = light[i + 1];
		}

		// Get temperature in a range from 0 to 40 degrees.
		if (at30tse_read_temperature(&temp) == TWI_SUCCESS)
		{
			// Don't care about negative temperature.
			if (temp < 0)
				temp = 0;

			// Update temperature for display.
			// Note: -12 in order to rescale for better rendering.
			if (temp < 12)
				temperature[BUFFER_SIZE - 1] = 0;
			else
				temperature[BUFFER_SIZE - 1] = temp - 12;
		}
		else
		{
			// Error print zero values.
			temperature[BUFFER_SIZE - 1] = 0;
		}

		// Get light sensor information.
		// Rescale for better rendering.
		adc_start(ADC);
		adc_value = adc_get_channel_value(ADC, ADC_CHANNEL_4);
		light[BUFFER_SIZE - 1] = 24 - adc_value * 24 / 4096;

		// Print temperature in text format.
		if (app_mode == 0)
		{
			sprintf(value_disp, "%d", (uint8_t)temp);
			ssd1306_set_column_address(95);
			ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0));
			ssd1306_write_text(" ");
			ssd1306_write_text(value_disp);
			// Display degree symbol.
			ssd1306_write_data(0x06);
			ssd1306_write_data(0x06);
			ssd1306_write_text("c");

			// Refresh graph.
			ssd1306_draw_graph(0, 1, BUFFER_SIZE, 3, temperature);
		}
		else if (app_mode == 1)
		{
			sprintf(value_disp, "%lu", 100 - (adc_value * 100 / 4096));
			ssd1306_set_column_address(98);
			ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0));
			ssd1306_write_text(" ");
			ssd1306_write_text(value_disp);
			ssd1306_write_text("%");

			// Refresh graph.
			ssd1306_draw_graph(0, 1, BUFFER_SIZE, 3, light);
		}
		else
		{
			// Is card has been inserted or removed?
			if (sd_status_update == 1)
			{
				// Clear screen.
				ssd1306_clear();
				ssd1306_set_page_address(0);
				ssd1306_set_column_address(0);

				// Show SD card info.
				display_sd_info();

				sd_status_update = 0;
			}

		}

		/* Wait and stop screen flickers. */
		delay_ms(50);
	}
}