Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 18:44 17 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Simple Compass

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1676
Posted: 09:55am 13 Aug 2017
Copy link to clipboard 
Print this post

I'm trying to get a "simple" compass drawn on a 7" display
What I need it the ability to rotate the pointer to any one of 360 degree points
The degree points will be ascertained by a wind vane outside.

I'm totally hopeless at writing code as most of you know
I have used Matherps Compass.Bas code as a starting point but this was written for genuine sensors and not just as a simple compass display choosing one of 360 degrees to point to

I am hopeless at maths and I cannot understand most of the code, ie the bits that rotate the triangle

I have got it to display the compass pointer on the screen BUT IT DISPLAYS 2 pointers one above the other????





I know I have no background image btw*

Anyone able to explain in words a 10 yr old could understand how the pointers bit works and how to get it to rotate clockwise to any one of 360 points 0 to 359 degrees with 0 as North

What I need to know is how to make the triangles rotate to one of the 360 positions.
IF anyone would be so kind as to help explain it to me please

This is how i've got this far
[code]
'Compass code for wind vane "borrowed" from Matherps compass.bas program

option explicit
option default none
CLS

const Y_top = 0 'Y coordinate of top of the image file on the screen
const X_left = 104 'X coordinate of left of the image file on the screen
const Y_point = 136 'Y coordinate of the centre of the pointer relative to the image top
const X_point = 136 'X coordinate of the centre of the pointer relative to the image left
'
const buffersize = 2000 'used to store the image data behind the pointer
dim integer buff(buffersize)
'
'
' Now we define the pointer as the X,Y coordinates of a number of triangles with the rotation point at 0,0
' It is simplest to define this in the 12-oclock position which means the Y-coordinates at the top of the pointer will be negative
' The order of data elements in the array is:
' t1x1,t1y1,t1x2,t1y2,t1x3,t1y3, t2x1,t2y1,t2x2,t2y2,t2x3,t2y3,...tntx1,tnty1,tntx2,tnty2,tntx3,tnty3
'
dim integer nt=2 'Number of triangles used to define the pointer
dim integer ptr(5,nt-1)=(-10,0, 10,0, 0,-120, -10,1,10,1,0,120)
DIM INTEGER pcolour(nt-1)=(rgb(RED),rgb(50,50,255)) 'define the colour of the pointer
DIM float heading=0
const pivot=15 'diameter in pixels of the fulcrum of the pointer '
' Global Variable definitions
'

dim integer xx0(nt-1),yy0(nt-1),xx1(nt-1),yy1(nt-1),xx2(nt-1),yy2(nt-1),tcol(nt-1)
dim integer lc
dim integer x,y,w,h, xp1, yp1, wp1, hp1


cls rgb(white)
'load image "compass",X_left,Y_top
'load image "compass",X_left,Y_top + 272
for lc=0 to nt-1 'load the coordinate arrays
rotatetriangle(lc,pcolour(lc),heading,X_point+X_left,Y_point+Y_top,ptr(0,lc),ptr(1,lc),ptr(2,lc),ptr(3,lc),ptr(4,lc),ptr (5,lc)) 'rotate the pointer into the drawing array
next lc
getlimits(x, y, w, h)
getlimits(xp1, yp1, wp1, hp1)
drawpivot(0)
drawpivot(272)
'blit read #1, x, y, w, h
'blit read #2, xp1, yp1+272, wp1, hp1
for lc=0 to nt-1
triangle xx0(lc), yy0(lc), xx1(lc), yy1(lc), xx2(lc), yy2(lc), tcol(lc),tcol(lc)
triangle xx0(lc), yy0(lc)+272, xx1(lc), yy1(lc)+272, xx2(lc), yy2(lc)+272, tcol(lc),tcol(lc)
next lc

' Main program
'


end
'
Sub drawpointer(angle as float, offset as integer)
local integer last_x, last_y, last_w, last_h, i
for i=0 to nt-1
rotatetriangle(i,pcolour(i),angle,X_point+X_left,Y_point+Y_top,ptr(0,i),ptr(1,i),ptr(2,i),ptr(3,i),ptr(4,i),ptr(5,i)) 'rotate the pointer into the drawing array
next i
if(offset=0) then
last_x=x: last_y=y: last_w=w: last_h=h
getlimits(x, y, w, h)
blit WRITE #1, last_x, last_y, last_w, last_h
blit close #1
blit read #1, x, y, w, h
else
last_x=xp1: last_y=yp1: last_w=wp1: last_h=hp1
getlimits(xp1, yp1, wp1, hp1)
blit WRITE #2, last_x, last_y+offset, last_w, last_h
blit close #2
blit read #2, xp1, yp1+offset, wp1, hp1
endif
for i=0 to nt-1
triangle xx0(i), yy0(i)+offset, xx1(i), yy1(i)+offset, xx2(i), yy2(i)+offset, tcol(i),tcol(i)
next i
drawpivot(offset)
End Sub

sub getlimits(x as integer, y as integer, w as integer, h as integer)
Local integer i,max_x=0,min_x=MM.HRES,max_y=0,min_y=MM.VRES
for i=0 to nt-1
if(xx0(i)>max_x)then max_x=xx0(i)
if(xx1(i)>max_x)then max_x=xx1(i)
if(xx2(i)>max_x)then max_x=xx2(i)
if(xx0(i)<min_x)then min_x=xx0(i)
if(xx1(i)<min_x)then min_x=xx1(i)
if(xx2(i)<min_x)then min_x=xx2(i)
if(yy0(i)>max_y)then max_y=yy0(i)
if(yy1(i)>max_y)then max_y=yy1(i)
if(yy2(i)>max_y)then max_y=yy2(i)
if(yy0(i)<min_y)then min_y=yy0(i)
if(yy1(i)<min_y)then min_y=yy1(i)
if(yy2(i)<min_y)then min_y=yy2(i)
next i
x=min_x
y=min_y
w=max_x-min_x+1
h=max_y-min_y+1
end sub

sub rotatetriangle(n as integer, col as integer, angle as float, x as integer, y as integer, x0 as integer, y0 as integer, x1 as integer, y1 as integer, x2 as integer, y2 as integer)
local float sine=sin(rad(angle)),cosine=cos(rad(angle))
local integer x0a,y0a,x1a,y1a,x2a,y2a
x0a= x0*cosine - y0 * sine + x
y0a= y0*cosine + x0 * sine + y
x1a= x1*cosine - y1 * sine + x
y1a= y1*cosine + x1 * sine + y
x2a= x2*cosine - y2 * sine + x
y2a= y2*cosine + x2 * sine + y
xx0(n)=x0a
yy0(n)=y0a
xx1(n)=x1a
yy1(n)=y1a
xx2(n)=x2a
yy2(n)=y2a
tcol(n)=col
end sub

sub drawpivot(offset%) 'put anything you like here to draw the fulcrum as you wish
circle X_point+X_left,Y_point+Y_top+offset%,pivot,0,,,rgb(54,54,54)
circle X_point+X_left,Y_point+Y_top+offset%,pivot*0.7,0,,,rgb(43,43,43)
end sub
[/code]
 
CircuitGizmos

Guru

Joined: 08/09/2011
Location: United States
Posts: 1421
Posted: 02:48pm 13 Aug 2017
Copy link to clipboard 
Print this post

Leave the pointers where they are and rotate the background image!





Micromites and Maximites! - Beginning Maximite
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1676
Posted: 10:26pm 13 Aug 2017
Copy link to clipboard 
Print this post

Nice idea but not doable
I need the pointer to rotate
And I've no idea why there are 2 pointers




Edited by lew247 2017-08-15
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8604
Posted: 10:34pm 13 Aug 2017
Copy link to clipboard 
Print this post

  Quote  And I've no idea why there are 2 pointers


You are using code designed to run on a 480x272 display with a paged driver on a 800x480 display

The original code swaps between the display pages and only updates the hidden page to remove all flicker

See this thread for documented code showing how to construct and move pointersEdited by matherp 2017-08-15
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1676
Posted: 10:45pm 13 Aug 2017
Copy link to clipboard 
Print this post

  matherp said  
You are using code designed to run on a 480x272 display with a paged driver on a 800x480 display
See this thread for documented code showing how to construct and move pointers


Thanks Peter, I've got only one pointer now :)
Just reading that post now
[Edit} I'd totally forgotten I wrote that post, one of the main annoyances with having memory problems!!!Edited by lew247 2017-08-15
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024