// fill a rectangle void ili9340_fillRect(struct ili9340 *self, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t color) { //y = (y + term.scroll_start) % term.screen_height; // rudimentary clipping (drawChar w/big text requires this) //if((x >= t->screen_width) || (y >= t->screen_height)) return; if((x + w - 1) >= self->screen_width) w = self->screen_width - x; if((y + h - 1) >= self->screen_height) h = self->screen_height - y; ili9340_setAddrWindow_WR(self, x, y, x + w - 1, y + h - 1); uint8_t hi = color >> 8, lo = color; DC_HI; CS_LO; for(y=h; y>0; y--) { for(x=w; x>0; x--) { spi_writereadbyte(hi); spi_writereadbyte(lo); } } CS_HI; }
void ili9340_drawLine(struct ili9340 *self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t col){ int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1; int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1; int err = (dx>dy ? dx : -dy)/2, e2; uint8_t hi = col >> 8, lo = col; for(;;){ ili9340_setAddrWindow_WR(self, x0, y0, x0, y0); DC_HI; CS_LO; spi_writereadbyte(hi); spi_writereadbyte(lo); CS_HI; if (x0==x1 && y0==y1) break; e2 = err; if (e2 >-dx) { err -= dy; x0 += sx; } if (e2 < dy) { err += dx; y0 += sy; } } }
/* * read one register */ uint8_t nrf24l01_readregister(uint8_t reg) { nrf24l01_CSNlo; //low CSN spi_writereadbyte(NRF24L01_CMD_R_REGISTER | (NRF24L01_CMD_REGISTER_MASK & reg)); uint8_t result = spi_writereadbyte(NRF24L01_CMD_NOP); //read write register nrf24l01_CSNhi; //high CSN return result; }
/* * write many registers */ void nrf24l01_writeregisters(uint8_t reg, uint8_t *value, uint8_t len) { uint8_t i = 0; nrf24l01_CSNlo; //low CSN spi_writereadbyte(NRF24L01_CMD_W_REGISTER | (NRF24L01_CMD_REGISTER_MASK & reg)); for(i=0; i<len; i++) spi_writereadbyte(value[i]); //write register nrf24l01_CSNhi; //high CSN }
void nrf24l01_write_ack_payload(uint8_t * data) { uint8_t i = 0; nrf24l01_CSNlo; //low CSN spi_writereadbyte(0b10101000); for(i=0; i<31; i++) spi_writereadbyte(65 + i); //write register spi_writereadbyte(0); nrf24l01_CSNhi; //high CSN }
/* * get data */ void nrf24l01_read(uint8_t *data) { uint8_t i = 0; //read rx register nrf24l01_CSNlo; //low CSN spi_writereadbyte(NRF24L01_CMD_R_RX_PAYLOAD); for(i=0; i<NRF24L01_PAYLOAD; i++) data[i] = spi_writereadbyte(NRF24L01_CMD_NOP); nrf24l01_CSNhi; //high CSN //reset register nrf24l01_writeregister(NRF24L01_REG_STATUS, (1<<NRF24L01_REG_RX_DR)); //handle ack payload receipt if (nrf24l01_getstatus() & (1<<NRF24L01_REG_TX_DS)) nrf24l01_writeregister(NRF24L01_REG_STATUS, (1<<NRF24L01_REG_TX_DS)); }
static uint8_t _wr_data(struct ili9340 *self, uint8_t c) { DC_HI; CS_LO; uint8_t r = spi_writereadbyte(c); CS_HI; return r; }
/* * get status register */ uint8_t nrf24l01_getstatus() { uint8_t status = 0; nrf24l01_CSNlo; //low CSN status = spi_writereadbyte(NRF24L01_CMD_NOP); //get status, send NOP request nrf24l01_CSNhi; //high CSN return status; }
void nrf24l01_command(uint8_t * data, uint8_t len) { uint8_t i = 0; nrf24l01_CSNlo; //low CSN for(i=0; i<len; i++) spi_writereadbyte(data[i]); //write register nrf24l01_CSNhi; //high CSN }
/* static struct ili9340 { uint16_t screen_width, screen_height; int16_t cursor_x, cursor_y; int8_t char_width, char_height; uint16_t back_color, front_color; uint16_t scroll_start; } term; */ static void _wr_command(struct ili9340 *self, uint8_t c) { DC_LO; CS_LO; spi_writereadbyte(c); CS_HI; }
static uint16_t _wr_data16(struct ili9340 *self, uint16_t c){ DC_HI; CS_LO; uint16_t r = spi_writereadbyte(c >> 8); r <<= 8; r |= spi_writereadbyte(c & 0xff); CS_HI; return r; }
void ili9340_drawFastHLine(struct ili9340 *self, uint16_t x, uint16_t y, uint16_t w, uint16_t color) { // Rudimentary clipping //y = (y + term.scroll_start) % term.screen_height; if((x >= self->screen_width) || (y >= self->screen_height)) return; if((x+w-1) >= self->screen_width) w = self->screen_width-x; ili9340_setAddrWindow_WR(self, x, y, x+w-1, y); uint8_t hi = color >> 8, lo = color; DC_HI; CS_LO; while (w--) { spi_writereadbyte(hi); spi_writereadbyte(lo); } CS_HI; }
/* * put data */ uint8_t nrf24l01_write(uint8_t *data) { uint8_t i = 0; uint8_t ret = 0; //set tx mode nrf24l01_setTX(); //write data nrf24l01_CSNlo; //low CSN spi_writereadbyte(NRF24L01_CMD_W_TX_PAYLOAD); for (i=0; i<NRF24L01_PAYLOAD; i++) spi_writereadbyte(data[i]); nrf24l01_CSNhi; //high CSN //start transmission nrf24l01_CEhi; //high CE _delay_us(15); nrf24l01_CElo; //low CE //stop if max_retries reached or send is ok do { _delay_us(10); } while( !(nrf24l01_getstatus() & (1<<NRF24L01_REG_MAX_RT | 1<<NRF24L01_REG_TX_DS)) ); if(nrf24l01_getstatus() & 1<<NRF24L01_REG_TX_DS) ret = 1; //reset PLOS_CNT nrf24l01_writeregister(NRF24L01_REG_RF_CH, NRF24L01_CH); //power down nrf24l01_writeregister(NRF24L01_REG_CONFIG, nrf24l01_readregister(NRF24L01_REG_CONFIG) & ~(1<<NRF24L01_REG_PWR_UP)); //set rx mode nrf24l01_setRX(); return ret; }
void ili9340_readRect(struct ili9340 *self, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t *data){ _ili9340_setAddrWindow(self, x, y, x + w - 1, y + h - 1); //_wr_command(self, ILI9340_RAMRD); // read RAM CS_LO; DC_LO; spi_writereadbyte(ILI9340_RAMRD); //spi_writereadbyte(0); DC_HI; spi_writereadbyte(0); uint16_t cnt = w * h; for(uint16_t c = 0; c < cnt; c++){ // DAMN! this took a while to figure out :) // The data is one byte per color! NOT packed! data[c] = RGB16(spi_writereadbyte(0), spi_writereadbyte(0), spi_writereadbyte(0)); } CS_HI; }
void ili9340_writeRect(struct ili9340 *self,uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t *data){ _ili9340_setAddrWindow(self, x, y, x + w - 1, y + h - 1); /*_wr_command(self, ILI9340_RAMWR); // read RAM DC_HI; CS_LO; */ CS_LO; DC_LO; spi_writereadbyte(ILI9340_RAMWR); //spi_writereadbyte(0); DC_HI; uint16_t cnt = w * h; for(uint16_t c = 0; c < cnt; c++){ uint16_t d = *data++; spi_writereadbyte(d >> 8); spi_writereadbyte(d); } CS_HI; }
/* * flush RX fifo */ void nrf24l01_flushTXfifo() { nrf24l01_CSNlo; //low CSN spi_writereadbyte(NRF24L01_CMD_FLUSH_TX); nrf24l01_CSNhi; //high CSN }
/* * write one register */ void nrf24l01_writeregister(uint8_t reg, uint8_t value) { nrf24l01_CSNlo; //low CSN spi_writereadbyte(NRF24L01_CMD_W_REGISTER | (NRF24L01_CMD_REGISTER_MASK & reg)); spi_writereadbyte(value); //write register nrf24l01_CSNhi; //high CSN }