Appendix: Code
mouse.c
#include <Mega32.h>
#include <delay.h>
#include <stdio.h>
void send(unsigned char);
void init(void);
char i;
char a;
unsigned char data,pos=0,packet[3];
unsigned char dataready=0, packetcount=0, packetready=0, datareporting=0;
unsigned char x,y;
//Receive data from mouse triggered by mouse starting clock
interrupt [EXT_INT0] void receive(void) {
unsigned char shamt=0, count=0, parity=0;
data = 0;
while(PIND & 0x08); //wait for clock low
//Read 8 data bits
for(count=0;count<8;count++){
while(!(PIND & 0x08)); // wait for clock high
while(PIND & 0x08); // wait for clock low
data=data | ((PIND & 0x04)>>2)<125 && x<220)
x=125;
if(x<1 || x>220)
x=1;
y=y-packet[2]; //update and restrict y position
if(y>0x58 && y<220)
y=0x58;
if(y<12 || y>220)
y=12;
PORTA=x; //send out data packets to the ports
PORTB=y;
PORTC=packet[0];
packetcount=0;
}
else packetcount++;
}
}
void init_receive() {
MCUCR = 0b00000000; // INT0 interrupt enable
GICR = 0b01000000;
#asm
sei
#endasm
}
void disable_receive() {
GICR = 0; //Disable interrupts
}
void init() {
DDRA = 0xff; //PORTA as output
DDRB = 0xff; //PORTB as output
DDRC = 0xff; //PORTC as output
DDRD = 0x13; //D.0, D.1, D.4 (for testing) as output
send(0xFF); //Reset mouse
init_receive();
// wait for codes
while(data != 0xfa); //Acknowledge
while(data != 0xaa); //Self test passed
while(data != 0x00); //Mouse ID
disable_receive();
send(0xF4); // enable data reporting
init_receive();
while(data != 0xfa);
datareporting=1; //Ready to receive movement data
pos = 0;
}
void main(void){
init();
while(1) {}
}
//Send a command to the mouse
void send(unsigned char command) {
char parity=0;
char mask = 0x01;
parity = (command & 0x01) ^ //Calculate odd parity
((command & 0x02) >> 1) ^
((command & 0x04) >> 2) ^
((command & 0x08) >> 3) ^
((command & 0x10) >> 4) ^
((command & 0x20) >> 5) ^
((command & 0x40) >> 6) ^
((command & 0x80) >> 7);
PORTD.0 = 1; // Pull clock low
delay_us(120);
PORTD.1 = 1; // data low
PORTD.0 = 0; // Release clock
while(!(PIND & 0x08)); // wait for clock high
for (i=0; i<8; i++) {
while(PIND & 0x08); // wait for low clock
PORTD.1 = !(command & mask); // send inverted data bit to data line
while(!(PIND & 0x08)); // wait for clock high
mask = mask<<1; // update mask
}
while(PIND & 0x08); // wait for low clock
PORTD.1 = parity; //send parity bit to data line
while(!(PIND & 0x08)); // wait for high
while(PIND & 0x08); // wait for low clock
while(!(PIND & 0x08)); // wait for clock high
PORTD.1 = 0; //release the data line
while((PIND & 0x04)); // wait for data go low
while(!(PIND & 0x04)); // wait for data go hi
}
video.c
//video gen and sound
//D.5 is sync:1000 ohm + diode to 75 ohm resistor
//D.6 is video:330 ohm + diode to 75 ohm resistor
//B.3 is sound and should have a 10k resistor to gnd
#pragma regalloc- //I allocate the registers myself
#pragma optsize- //optimize for speed
#include <Mega32.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>
//cycles = 63.625 * 16 Note NTSC is 63.55
//but this line duration makes each frame exactly 1/60 sec
//which is nice for keeping a realtime clock
#define lineTime 1018
#define begin {
#define end }
#define ScreenTop 30
#define ScreenBot 230
#define totaltools 9
#define menutop 0x59
#define menubottom 0x63
#define menuwidth 0xE
#define leftlimit 1
#define rightlimit 125
#define toplimit 11
#define bottomlimit 0x59
#define maxsize 5
//NOTE that v1 to v8 and i must be in registers!
register char v1 @4;
register char v2 @5;
register char v3 @6;
register char v4 @7;
register char v5 @8;
register char v6 @9;
register char v7 @10;
register char v8 @11;
register int i @12;
#pragma regalloc+
unsigned char outx[16], outy[16], outc[16];
char syncON, syncOFF;
int LineCount;
int time;
//animation
char x, y, s, vx, vy;
//paint tools
char posx,posy,prevx,prevy,posc,prevstate, current, movex,movey,startx,starty;
char clickcount, cutcount, spraycount, set, linex[2],liney[2];
char tooltype, rightstate, leftstate, leftreleased, middlereleased, prevtool;
char middlestate, brushsize;
char c,d;
int a,b;
char screen[1600], t, ts[10];
char cu1[]="MOUSE";
char cu2[]="PAINTER";
char cu3[]="PRO";
char clr[]="C";
char erase[] = "E";
//Point plot lookup table
//One bit masks
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//define some character bitmaps
//5x7 characters
flash char bitmap[38][7]={
//0
0b01110000,
0b10001000,
0b10011000,
0b10101000,
0b11001000,
0b10001000,
0b01110000,
//1
0b00100000,
0b01100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b01110000,
//2
0b01110000,
0b10001000,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b11111000,
//3
0b11111000,
0b00010000,
0b00100000,
0b00010000,
0b00001000,
0b10001000,
0b01110000,
//4
0b00010000,
0b00110000,
0b01010000,
0b10010000,
0b11111000,
0b00010000,
0b00010000,
//5
0b11111000,
0b10000000,
0b11110000,
0b00001000,
0b00001000,
0b10001000,
0b01110000,
//6
0b01000000,
0b10000000,
0b10000000,
0b11110000,
0b10001000,
0b10001000,
0b01110000,
//7
0b11111000,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b10000000,
//8
0b01110000,
0b10001000,
0b10001000,
0b01110000,
0b10001000,
0b10001000,
0b01110000,
//9
0b01110000,
0b10001000,
0b10001000,
0b01111000,
0b00001000,
0b00001000,
0b00010000,
//A
0b01110000,
0b10001000,
0b10001000,
0b10001000,
0b11111000,
0b10001000,
0b10001000,
//B
0b11110000,
0b10001000,
0b10001000,
0b11110000,
0b10001000,
0b10001000,
0b11110000,
//C
0b01110000,
0b10001000,
0b10000000,
0b10000000,
0b10000000,
0b10001000,
0b01110000,
//D
0b11110000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b11110000,
//E
0b11111000,
0b10000000,
0b10000000,
0b11111000,
0b10000000,
0b10000000,
0b11111000,
//F
0b11111000,
0b10000000,
0b10000000,
0b11111000,
0b10000000,
0b10000000,
0b10000000,
//G
0b01110000,
0b10001000,
0b10000000,
0b10011000,
0b10001000,
0b10001000,
0b01110000,
//H
0b10001000,
0b10001000,
0b10001000,
0b11111000,
0b10001000,
0b10001000,
0b10001000,
//I
0b01110000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b01110000,
//J
0b00111000,
0b00010000,
0b00010000,
0b00010000,
0b00010000,
0b10010000,
0b01100000,
//K
0b10001000,
0b10010000,
0b10100000,
0b11000000,
0b10100000,
0b10010000,
0b10001000,
//L
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b10000000,
0b11111000,
//M
0b10001000,
0b11011000,
0b10101000,
0b10101000,
0b10001000,
0b10001000,
0b10001000,
//N
0b10001000,
0b10001000,
0b11001000,
0b10101000,
0b10011000,
0b10001000,
0b10001000,
//O
0b01110000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b01110000,
//P
0b11110000,
0b10001000,
0b10001000,
0b11110000,
0b10000000,
0b10000000,
0b10000000,
//Q
0b01110000,
0b10001000,
0b10001000,
0b10001000,
0b10101000,
0b10010000,
0b01101000,
//R
0b11110000,
0b10001000,
0b10001000,
0b11110000,
0b10100000,
0b10010000,
0b10001000,
//S
0b01111000,
0b10000000,
0b10000000,
0b01110000,
0b00001000,
0b00001000,
0b11110000,
//T
0b11111000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
//U
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b01110000,
//V
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b01010000,
0b00100000,
//W
0b10001000,
0b10001000,
0b10001000,
0b10101000,
0b10101000,
0b10101000,
0b01010000,
//X
0b10001000,
0b10001000,
0b01010000,
0b00100000,
0b01010000,
0b10001000,
0b10001000,
//Y
0b10001000,
0b10001000,
0b10001000,
0b01010000,
0b00100000,
0b00100000,
0b00100000,
//Z
0b11111000,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b11111000,
//figure1
0b01110000,
0b00100000,
0b01110000,
0b10101000,
0b00100000,
0b01010000,
0b10001000,
//figure2
0b01110000,
0b10101000,
0b01110000,
0b00100000,
0b00100000,
0b01010000,
0b10001000};
//================================
//3x5 font numbers, then letters
//packed two per definition for fast
//copy to the screen at x-position divisible by 4
flash char smallbitmap[39][5]={
//0
0b11101110,
0b10101010,
0b10101010,
0b10101010,
0b11101110,
//1
0b01000100,
0b11001100,
0b01000100,
0b01000100,
0b11101110,
//2
0b11101110,
0b00100010,
0b11101110,
0b10001000,
0b11101110,
//3
0b11101110,
0b00100010,
0b11101110,
0b00100010,
0b11101110,
//4
0b10101010,
0b10101010,
0b11101110,
0b00100010,
0b00100010,
//5
0b11101110,
0b10001000,
0b11101110,
0b00100010,
0b11101110,
//6
0b11001100,
0b10001000,
0b11101110,
0b10101010,
0b11101110,
//7
0b11101110,
0b00100010,
0b01000100,
0b10001000,
0b10001000,
//8
0b11101110,
0b10101010,
0b11101110,
0b10101010,
0b11101110,
//9
0b11101110,
0b10101010,
0b11101110,
0b00100010,
0b01100110,
//:
0b00000000,
0b01000100,
0b00000000,
0b01000100,
0b00000000,
//=
0b00000000,
0b11101110,
0b00000000,
0b11101110,
0b00000000,
//blank
0b00000000,
0b00000000,
0b00000000,
0b00000000,
0b00000000,
//A
0b11101110,
0b10101010,
0b11101110,
0b10101010,
0b10101010,
//B
0b11001100,
0b10101010,
0b11101110,
0b10101010,
0b11001100,
//C
0b11101110,
0b10001000,
0b10001000,
0b10001000,
0b11101110,
//D
0b11001100,
0b10101010,
0b10101010,
0b10101010,
0b11001100,
//E
0b11101110,
0b10001000,
0b11101110,
0b10001000,
0b11101110,
//F
0b11101110,
0b10001000,
0b11101110,
0b10001000,
0b10001000,
//G
0b11101110,
0b10001000,
0b10001000,
0b10101010,
0b11101110,
//H
0b10101010,
0b10101010,
0b11101110,
0b10101010,
0b10101010,
//I
0b11101110,
0b01000100,
0b01000100,
0b01000100,
0b11101110,
//J
0b00100010,
0b00100010,
0b00100010,
0b10101010,
0b11101110,
//K
0b10001000,
0b10101010,
0b11001100,
0b11001100,
0b10101010,
//L
0b10001000,
0b10001000,
0b10001000,
0b10001000,
0b11101110,
//M
0b10101010,
0b11101110,
0b11101110,
0b10101010,
0b10101010,
//N
0b00000000,
0b11001100,
0b10101010,
0b10101010,
0b10101010,
//O
0b01000100,
0b10101010,
0b10101010,
0b10101010,
0b01000100,
//P
0b11101110,
0b10101010,
0b11101110,
0b10001000,
0b10001000,
//Q
0b01000100,
0b10101010,
0b10101010,
0b11101110,
0b01100110,
//R
0b11101110,
0b10101010,
0b11001100,
0b11101110,
0b10101010,
//S
0b11101110,
0b10001000,
0b11101110,
0b00100010,
0b11101110,
//T
0b11101110,
0b01000100,
0b01000100,
0b01000100,
0b01000100,
//U
0b10101010,
0b10101010,
0b10101010,
0b10101010,
0b11101110,
//V
0b10101010,
0b10101010,
0b10101010,
0b10101010,
0b01000100,
//W
0b10101010,
0b10101010,
0b11101110,
0b11101110,
0b10101010,
//X
0b00000000,
0b10101010,
0b01000100,
0b01000100,
0b10101010,
//Y
0b10101010,
0b10101010,
0b01000100,
0b01000100,
0b01000100,
//Z
0b11101110,
0b00100010,
0b01000100,
0b10001000,
0b11101110
};
//==================================
//This is the sync generator and raster generator. It MUST be entered from
//sleep mode to get accurate timing of the sync pulses
#pragma warn-
interrupt [TIM1_COMPA] void t1_cmpA(void)
begin
//start the Horizontal sync pulse
PORTD = syncON;
//update the curent scanline number
LineCount ++ ;
//begin inverted (Vertical) synch after line 247
if (LineCount==248)
begin
syncON = 0b00100000;
syncOFF = 0;
end
//back to regular sync after line 250
if (LineCount==251)
begin
syncON = 0;
syncOFF = 0b00100000;
end
//start new frame after line 262
if (LineCount==263)
begin
LineCount = 1;
end
delay_us(2); //adjust to make 5 us pulses
//end sync pulse
PORTD = syncOFF;
if (LineCount=ScreenTop)
begin
//compute byte index for beginning of the next line
//left-shift 4 would be individual lines
// <<3 means line-double the pixels
//The 0xfff8 truncates the odd line bit
//i=(LineCount-ScreenTop)<<3 & 0xfff8; //
#asm
push r16
lds r12, _LineCount
lds r13, _Linecount+1
ldi r16, 30
sub r12, r16
ldi r16,0
sbc r13, r16
lsl r12
rol r13
lsl r12
rol r13
lsl r12
rol r13
mov r16,r12
andi r16,0xf0
mov r12,r16
pop r16
#endasm
//load 16 registers with screen info
#asm
push r14
push r15
push r16
push r17
push r18
push r19
push r26
push r27
ldi r26,low(_screen) ;base address of screen
ldi r27,high(_screen)
add r26,r12 ;offset into screen (add i)
adc r27,r13
ld r4,x+ ;load 16 registers and inc pointer
ld r5,x+
ld r6,x+
ld r7,x+
ld r8,x+
ld r9,x+
ld r10,x+
ld r11,x+
ld r12,x+
ld r13,x+
ld r14,x+
ld r15,x+
ld r16,x+
ld r17,x+
ld r18,x+
ld r19,x
pop r27
pop r26
#endasm
delay_us(4); //adjust to center image on screen
//blast 16 bytes to the screen
#asm
;but first a macro to make the code shorter
;the macro takes a register number as a parameter
;and dumps its bits serially to portD.6
;the nop can be eliminated to make the display narrower
.macro videobits ;regnum
BST @0,7
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,6
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,5
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,4
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,3
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,2
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,1
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
BST @0,0
IN R30,0x12
BLD R30,6
nop
OUT 0x12,R30
.endm
videobits r4 ;video line -- byte 1
videobits r5 ;byte 2
videobits r6 ;byte 3
videobits r7 ;byte 4
videobits r8 ;byte 5
videobits r9 ;byte 6
videobits r10 ;byte 7
videobits r11 ;byte 8
videobits r12 ;byte 9
videobits r13 ;byte 10
videobits r14 ;byte 11
videobits r15 ;byte 12
videobits r16 ;byte 13
videobits r17 ;byte 14
videobits r18 ;byte 15
videobits r19 ;byte 16
clt ;clear video after the last pixel on the line
IN R30,0x12
BLD R30,6
OUT 0x12,R30
pop r19
pop r18
pop r17
pop r16
pop r15
pop r14
#endasm
end
end
#pragma warn+
//==================================
//plot one point
//at x,y with color 1=white 0=black 2=invert
#pragma warn-
void video_pt(char x, char y, char c)
begin
#asm
; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it
push r16
ldd r30,y+2 ;get x
lsr r30
lsr r30
lsr r30 ;divide x by 8
ldd r12,y+1 ;get y
lsl r12 ;mult y by 16
clr r13
lsl r12
rol r13
lsl r12
rol r13
lsl r12
rol r13
add r12, r30 ;add in x/8
;v2 = screen[i]; r5
;v3 = pos[x & 7]; r6
;v4 = c r7
ldi r30,low(_screen)
ldi r31,high(_screen)
add r30, r12
adc r31, r13
ld r5,Z ;get screen byte
ldd r26,y+2 ;get x
ldi r27,0
andi r26,0x07 ;form x & 7
ldi r30,low(_pos*2)
ldi r31,high(_pos*2)
add r30,r26
adc r31,r27
lpm r6,Z
ld r16,y ;get c
;if (v4==1) screen[i] = v2 | v3 ;
;if (v4==0) screen[i] = v2 & ~v3;
;if (v4==2) screen[i] = v2 ^ v3 ;
cpi r16,1
brne tst0
or r5,r6
tst0:
cpi r16,0
brne tst2
com r6
and r5,r6
tst2:
cpi r16,2
brne writescrn
eor r5,r6
writescrn:
ldi r30,low(_screen)
ldi r31,high(_screen)
add r30, r12
adc r31, r13
st Z, r5 ;write the byte back to the screen
pop r16
#endasm
end
#pragma warn+
//==================================
// put a big character on the screen
// c is index into bitmap
void video_putchar(char x, char y, char c)
begin
v7 = x;
for (v6=0;v6<7;v6++)
begin
v1 = bitmap[c][v6];
v8 = y+v6;
video_pt(v7, v8, (v1 & 0x80)==0x80);
video_pt(v7+1, v8, (v1 & 0x40)==0x40);
video_pt(v7+2, v8, (v1 & 0x20)==0x20);
video_pt(v7+3, v8, (v1 & 0x10)==0x10);
video_pt(v7+4, v8, (v1 & 0x08)==0x08);
end
end
//==================================
// put a string of big characters on the screen
void video_puts(char x, char y, char *str)
begin
char i ;
for (i=0; str[i]!=0; i++)
begin
if (str[i]>=0x30 && str[i]<=0x3a)
video_putchar(x,y,str[i]-0x30);
else video_putchar(x,y,str[i]-0x40+9);
x = x+6;
end
end
//==================================
// put a small character on the screen
// x-cood must be on divisible by 4
// c is index into bitmap
void video_smallchar(char x, char y, char c)
begin
char mask;
i=((int)x>>3) + ((int)y<<4) ;
if (x == (x & 0xf8)) mask = 0x0f; //f8
else mask = 0xf0;
screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask);
screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask);
screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask);
screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask);
screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask);
end
//==================================
// put a string of small characters on the screen
// x-cood must be on divisible by 4
void video_putsmalls(char x, char y, char *str)
begin
char i ;
for (i=0; str[i]!=0; i++)
begin
if (str[i]>=0x30 && str[i]<=0x3a)
video_smallchar(x,y,str[i]-0x30);
else video_smallchar(x,y,str[i]-0x40+12);
x = x+4;
end
end
void video_circle(char x1, char y1, char x2, char y2,char cond) {
float r,t,n=7;
unsigned char circlex,circley;
TIMSK = 0x0;
r=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
for(t=0;t<2*3.14;t=t+2*3.14/(r*n)){
circlex=x1+r*cos(t);
circley=y1+r*sin(t);
if((!((circlexrightlimit)||(circleybottomlimit))) || cond)
{
video_pt(circlex,circley,1);
}
}
TIMSK = 0x10;
}
//==================================
//plot a line
//at x1,y1 to x2,y2 with color 1=white 0=black 2=invert
//NOTE: this function requires signed chars
//Code is from David Rodgers,
//"Procedural Elements of Computer Graphics",1985
void video_line(char x1, char y1, char x2, char y2, char c)
begin
int e;
signed char dx,dy,j, temp;
signed char s1,s2, xchange;
signed char x,y;
x = x1;
y = y1;
dx = cabs(x2-x1);
dy = cabs(y2-y1);
s1 = csign(x2-x1);
s2 = csign(y2-y1);
xchange = 0;
if (dy>dx)
begin
temp = dx;
dx = dy;
dy = temp;
xchange = 1;
end
e = ((int)dy<<1) - dx;
for (j=0; j<=dx; j++)
begin
video_pt(x,y,c) ;
if (e>=0)
begin
if (xchange==1) x = x + s1;
else y = y + s2;
e = e - ((int)dx<<1);
end
if (xchange==1) y = y + s2;
else x = x + s1;
e = e + ((int)dy<<1);
end
end
//==================================
//return the value of one point
//at x,y with color 1=white 0=black 2=invert
char video_set(char x, char y)
begin
//The following construction
//detects exactly one bit at the x,y location
i=((int)x>>3) + ((int)y<<4) ;
return ( screen[i] & 1<<(7-(x & 0x7)));
end
//==================================
// set up the ports and timers
void main(void)
begin
//init timer 1 to generate sync
OCR1A = lineTime; //One NTSC line
TCCR1B = 9; //full speed; clear-on-match
TCCR1A = 0x00; //turn off pwm and oc lines
TIMSK = 0x10; //enable interrupt T1 cmp
//init ports
DDRD = 0xf0; //video out and switches
DDRA = 0x00; //PORTA as input
DDRB = 0x00; //PORTB as input
DDRC = 0x00; //PORTC as input
//D.5 is sync:1000 ohm + diode to 75 ohm resistor
//D.6 is video:330 ohm + diode to 75 ohm resistor
rightstate = 0;
leftstate = 0;
//initialize synch constants
LineCount = 1;
syncON = 0b00000000;
syncOFF = 0b00100000;
vx=1;
vy=1;
//Print title
video_puts(10,3,cu1);
video_puts(48,3,cu2);
video_puts(97,3,cu3);
//side lines
#define width 126
video_pt(0,0,1);
video_line(0,0,0,99,1);
video_line(width,0,width,99,1);
video_line(0,0,width,0,1);
video_line(0,99,width,99,1);
video_line(0,11,width,11,1);
video_line(0,89,width,89,1);
//top line & bottom lines
//menu lines
video_pt(0x6,0x5D,1);
video_pt(0x7,0x5D,1);
video_pt(0x6,0x5E,1);
video_pt(0x7,0x5E,1);
video_pt(0x6,0x5F,1);
video_pt(0x7,0x5F,1);
for(c=1;c (rightlimit-brushsize))
posx = rightlimit-brushsize;
else if(posx < (leftlimit + brushsize))
posx = leftlimit+brushsize;
if(posy > ((bottomlimit-brushsize)-1))
posy = bottomlimit-brushsize-1;
else if(posy < ((toplimit + brushsize)+1))
posy = toplimit+brushsize+1;
}
//Either the left or middle buttons are pressed
if(leftreleased||(middlereleased&&(tooltype==6||tooltype==8))) {
switch(tooltype) {
// freedraw
case 0:
set=0;
prevstate=1; //Leave a trail of set points
break;
case 1:
linex[clickcount] = posx; //Gather x and y positions
liney[clickcount] = posy;
if(clickcount == 1) {
video_line(linex[0],liney[0],linex[1],liney[1],1); //Draw line
clickcount = 0;
prevstate = 1;
}
else
clickcount++;
break;
// square draw
case 2:
linex[clickcount] = posx; //Gather x and y positions
liney[clickcount] = posy;
//prevstate = 1;
if(clickcount == 1) {
video_line(linex[0],liney[0],linex[0],liney[1],1); //Draw square
video_line(linex[1],liney[0],linex[1],liney[1],1);
video_line(linex[0],liney[0],linex[1],liney[0],1);
video_line(linex[0],liney[1],linex[1],liney[1],1);
clickcount = 0;
prevstate = 1;
}
else
clickcount++;
break;
// erase
case 3:
prevstate=0; //Leave a trail of deleted points
break;
//circle tool
case 4:
linex[clickcount] = posx; //Gather x and y positions
liney[clickcount] = posy;
if(clickcount == 1) {
video_circle(linex[0],liney[0],linex[1],liney[1],0); //Draw circle
clickcount = 0;
prevstate = 1;
}
else
clickcount++;
break;
//clear screen
case 5:
//reset
#asm
jmp 0
#endasm
break;
//Cut/copy and paste
case 6:
if(cutcount<2){
linex[cutcount] = posx;
liney[cutcount] = posy;
}
//prevstate = 1;
if(cutcount == 1) { //Box has been defined
if(linex[0]>linex[1]){
c=linex[0];
linex[0]=linex[1];
linex[1]=c;
}
if(liney[0]>liney[1]){
c=liney[0];
liney[0]=liney[1];
liney[1]=c;
}
//draw box around area
video_line(linex[0],liney[0],linex[0],liney[1],2);
video_line(linex[1],liney[0],linex[1],liney[1],2);
video_line(linex[0]+1,liney[0],linex[1]-1,liney[0],2);
video_line(linex[0]+1,liney[1],linex[1]-1,liney[1],2);
cutcount = 2;
prevstate = 1;
}
else if (cutcount==2){ //new position has been defined
TIMSK = 0x00; //Turn off video to prevent timing problems
movex=posx;
movey=posy;
//erase box
video_line(linex[0],liney[0],linex[0],liney[1],2);
video_line(linex[1],liney[0],linex[1],liney[1],2);
video_line(linex[0]+1,liney[0],linex[1]-1,liney[0],2);
video_line(linex[0]+1,liney[1],linex[1]-1,liney[1],2);
//Make sure not to overwrite data if positions overlap
if(movexrightlimit)||(movey+dbottomlimit-1))))
video_pt(movex+c,movey+d,current); //Move point
if(!middlereleased) //Do not erase if middle button clicked (copy)
video_pt(linex[0]+c,liney[0]+d,0);
}
}
}
//Moving to the right
else if(movex>linex[0]){
for(c=(linex[1]-linex[0]-1);c>0;c--){
for(d=1;d<(liney[1]-liney[0]);d++){
current=!(!(video_set(linex[0]+c,liney[0]+d)));
if((!((movex+crightlimit)||(movey+dbottomlimit-1))))
video_pt(movex+c,movey+d,current); //Move point
if(!middlereleased) //Do not erase if middle button clicked (copy)
video_pt(linex[0]+c,liney[0]+d,0);
}
}
}
//Moving up
else if(movey0;c--){
current=!(!(video_set(linex[0]+c,liney[0]+d)));
if((!((movex+crightlimit)||(movey+dbottomlimit-1))))
video_pt(movex+c,movey+d,current); //Move point
if(!middlereleased) //Do not erase if middle button clicked (copy)
video_pt(linex[0]+c,liney[0]+d,0);
}
}
}
else if(movey>liney[0]){
for(d=(liney[1]-liney[0]-1);d>0;d--){
for(c=(linex[1]-linex[0]);c>0;c--){
current=!(!(video_set(linex[0]+c,liney[0]+d)));
if((!((movex+crightlimit)||(movey+dbottomlimit-1))))
video_pt(movex+c,movey+d,current); //Move point
if(!middlereleased) //Do not erase if middle button clicked (copy)
video_pt(linex[0]+c,liney[0]+d,0);
}
}
}
cutcount=0;
TIMSK = 0x10;
}
else
cutcount++;
break;
//Polygon tool
case 8:
linex[clickcount] = posx;
liney[clickcount] = posy;
if(clickcount == 1) {
if(!middlereleased){ //Middle button sets last line
video_line(linex[0],liney[0],linex[1],liney[1],1); //Draw line
linex[0]=linex[1]; //last end point is new beginning point
liney[0]=liney[1];
prevstate = 1;
}
else{
clickcount=0;
video_line(startx,starty,linex[0],liney[0],1); //draw line connecting start to finish
//prevstate = 1;
}
}
else{
clickcount++;
startx=posx; //remember starting positions
starty=posy;
}
break;
}
leftreleased = 0;
}
//Spray tool
if(tooltype==7&&PINC.0){
//Wait for 5 screen refreshes
if(spraycount==5){
//Draw box of random points around cursor
for(c=0;c<5;c++){
for(d=0;d<5;d++){
a=rand()%5;
//Make sure points are on the screen
if(a==3&&(!((posx-2+crightlimit)||(posy-2+dbottomlimit-1))))
video_pt(posx-2+c,posy-2+d,1);
}
}
spraycount=0;
}
else
spraycount++;
}
// right mouse button state machine
switch(rightstate) {
// 0 - not clicked
// 1 - clicked
case 0:
if(PINC.1)
rightstate = 1;
break;
case 1:
if(~PINC.1) {
//change brush size back to default
if(tooltype == 0 || tooltype == 3) {
for(c=-brushsize; c<=brushsize; c++)
for(d=-brushsize;d<=brushsize;d++)
video_pt(posx+c,posy+d,2);
video_pt(posx,posy,2);
}
//cycle through tools
tooltype = (tooltype + 1) % totaltools;
if(tooltype == 0)
prevtool = totaltools-1;
else {
brushsize = 0;
prevtool = tooltype-1;
}
//invert current selection
for(c=1+tooltype*menuwidth; c