Étape 12 : Comment dessiner un cercle ?
Nous pouvons utiliser le code suivant pour générer un cercle sur notre écran :
#include //Include Wire library for I2C communication#define HEIGHT 64 #define WIDTH 128 const int RST = 10; //Assign pin 10 for Reset int i; //Set variable i as integer static unsigned char array[1024]; //Buffer array void setup() { pin_init(); //Initialize pins initialize_OLED(); //Initialize screen memset(array, 0, sizeof(array)); //Initialize array with 0s circle(63,31,10); //Draw circle, (x,y,R) Flush(); //Send data } void loop() { } void pin_init(){ Serial.begin(9600); //Set baud for serial transmission pinMode(RST, OUTPUT); //Set RST as output } void initialize_OLED(){ Wire.begin(); //Initialize I2C interface digitalWrite(RST, LOW); //Set RST pin low delay(100); //Wait 100 ms digitalWrite(RST, HIGH); //Set RST pin high Wire.beginTransmission(0x3D); // Start communication with slave Wire.write(0x00); //Command stream Wire.write(0xAE); //Set display Off Wire.write(0xD5); //Set display clock divide ratio/oscillator frequency Wire.write(0x80); Wire.write(0xA8); //Set multiplex ratio Wire.write(0x3F); Wire.write(0xD3); //Set display offset Wire.write(0x00); Wire.write(0x40); //Set display start line Wire.write(0x8D); //Set charge pump Wire.write(0x14); //VCC generated by internal DC/DC circuit Wire.write(0xA1); //Set segment re-map Wire.write(0xC0); //Set COM output scan direction Wire.write(0xDA); //Set COM pins hardware configuration Wire.write(0x12); Wire.write(0x81); //Set contrast control Wire.write(0xCF); Wire.write(0xD9); //Set pre-changed period Wire.write(0xF1); Wire.write(0xDB); //Set VCOMH Deselected level Wire.write(0x40); Wire.write(0xA4); //Set entire display on/off Wire.write(0xA6); //Set normal/inverse display Wire.write(0x20); //Set memory address mode Wire.write(0x00); //Horizontal Wire.write(0xAF); //Set display on Wire.endTransmission(); //End communication with slave } /*Midpoint circle algorithm*/ void circle(int x0, int y0, int R){ int x = R; //Set x equal to radius int y = 0; int de = 1-x; while(x>=y){ draw_pixel(x+x0, y+y0); //First octant draw_pixel(y+x0, x+y0); //Second octant draw_pixel(-y+x0, x+y0); //Third octant draw_pixel(-x+x0, y+y0); //Fourth octant draw_pixel(-x+x0, -y+y0); //Fifth octant draw_pixel(-y+x0, -x+y0); //Sixth octant draw_pixel(y+x0, -x+y0); //Seventh octant draw_pixel(x+x0, -y+y0); //Eight octant y++; if(de<=0){ de += 2*y+1; } else{ x--; de += 2*(y-x)+1; } } } void draw_pixel(int x, int y){ if((x<0) || (x>=WIDTH) || (y<0) || (y>=HEIGHT)){ //Check for boundaries return; } else{ array[x+(y/8)*WIDTH] |= _BV((y%8)); //Store pixel in array } } void Flush(){ Wire.beginTransmission(0x3D); //Start communication with slave Wire.write(0x00); //Command stream Wire.write(0x00); //Set lower column start address for page addressing mode Wire.write(0x10); //Set higher column start address for page addressing mode Wire.write(0x40); //Set display start line Wire.endTransmission(); //End communication with slave unsigned char twbrbackup = TWBR; //Two wire bit rate register TWBR = 12; //Set to 400kHz for(unsigned short q=0; q<(WIDTH*HEIGHT/8); q++){ Wire.beginTransmission(0x3D); //Start communication with slave Wire.write(0x40); //Data stream for(unsigned char w=0; w<16; w++){ Wire.write(array[q]); //Transmit data to be displayed q++; } q--; Wire.endTransmission(); //End communication with slave } TWBR = twbrbackup; }
La fonction de cercle est basée sur Médiane cercle algorithme utilisé pour générer des cercles avec des pixels. L’algorithme dessine un pixel à la fois dans chaque octant du cercle au moyen de symétrie jusqu'à ce que la circonférence est terminée. Une fois de plus, la fonction de pixel est utilisée pour dessiner chaque pixel du cercle, chaque fois qu’elle est appelée dans le programme. Consulter le lien suivant pour plus d’informations sur l’algorithme :
https://en.wikipedia.org/wiki/Midpoint_circle_algo...
En donnant notre cercle fonctionner trois arguments (origine à (x, y) et radius), nous pouvons maintenant tracer un cercle comme celui ci-dessus.
Dans cet exemple, origine du cercle est à (63,31) et a un rayon de 10, générant ainsi un cercle au centre de la page avec un rayon de 10 pixels.