Paint.cpp
 1  // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  #include "Paint.h"
 6  
 7  Paint::Paint(int8_t x, int8_t y){
 8    cursorX=x;
 9    cursorY=y;
10    for (uint8_t i=0;i<16;i++) canvas[i]=0;
11  }
12  
13  void Paint::turnOffCursor(){
14    activeCanvas[cursorY]&=~_BV(7-cursorX);
15  }
16  
17  void Paint::turnOnCursor(){
18    activeCanvas[cursorY]|=_BV(7-cursorX);
19  }
20  
21  void Paint::flashCursor(){
22    if (flashDisableCounter>0){
23      flashDisableCounter--;
24      return;
25    }
26    cursorVisible=!cursorVisible;  
27  }
28  
29  void Paint::moveCursor(int8_t dx, int8_t dy){
30    int8_t cursorX1 = cursorX+dx;
31    int8_t cursorY1 = cursorY+dy;
32    if (cursorX1<X_MIN || cursorX1>X_MAX || cursorY1<Y_MIN || cursorY1>Y_MAX) return;
33    cursorX=cursorX1;
34    cursorY=cursorY1;
35    // Do not flash the cursor for 0.5s just after it's moved to a new location
36    flashDisableCounter=1;
37    cursorVisible=true;
38  }
39  
40  bool Paint::readCanvas(uint8_t x, uint8_t y){
41    return (canvas[y]>>(7-x))&1;
42  }
43  
44  void Paint::clearCanvas(){
45    for (uint8_t i=0;i<=Y_MAX;i++) canvas[i]=0;
46  }
47  
48  void Paint::draw(){
49    if (readCanvas(cursorX,cursorY)) canvas[cursorY]&=~_BV(7-cursorX);  // if (cursorX,cursorY) is 1 on the canvas, replace it with 0
50    else canvas[cursorY]|=_BV(7-cursorX);                               // if (cursorX,cursorY) is 0 on the canvas, replace it with 1
51  }
52  
53  uint8_t* Paint::getActiveCanvas(){
54    for (uint8_t i=0;i<16;i++) activeCanvas[i]=canvas[i];
55    if (cursorVisible) turnOnCursor();
56    else turnOffCursor();
57    return activeCanvas;
58  }