Checkpoint on playing around with simple animations

This commit is contained in:
R. Tyler Croy 2018-11-12 09:58:06 -08:00
parent cb0ad190d1
commit cd6d9e87ee
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
6 changed files with 285 additions and 38 deletions

105
src/flappybit-lcd.adb Normal file
View File

@ -0,0 +1,105 @@
with HAL.GPIO;
with MicroBit.IOs;
package body Flappybit.LCD is
procedure Initialize is
begin
-- The SPI controller needs to be initialized before it's used
MicroBit.SPI.Initialize;
-- In order for the display to work, the CS, RS, RST pins need to be set to output
MicroBit.MB_P16.Set_Mode (HAL.GPIO.Output);
MicroBit.MB_P12.Set_Mode (HAL.GPIO.Output);
MicroBit.MB_P8.Set_Mode (HAL.GPIO.Output);
Initialize (Screen);
Screen.Set_Memory_Data_Access (
Color_Order => RGB_Order,
Vertical => Vertical_Refresh_Top_Bottom,
Horizontal => Horizontal_Refresh_Left_Right,
Row_Addr_Order => Row_Address_Bottom_Top,
Column_Addr_Order => Column_Address_Right_Left,
Row_Column_Exchange => False);
Screen.Set_Pixel_Format (Pixel_16bits);
Set_Frame_Rate_Normal (Screen,
RTN => 16#01#,
Front_Porch => 16#2C#,
Back_Porch => 16#2D#);
Set_Frame_Rate_Idle (Screen,
RTN => 16#01#,
Front_Porch => 16#2C#,
Back_Porch => 16#2D#);
Set_Frame_Rate_Partial_Full (Screen,
RTN_Part => 16#01#,
Front_Porch_Part => 16#2C#,
Back_Porch_Part => 16#2D#,
RTN_Full => 16#01#,
Front_Porch_Full => 16#2C#,
Back_Porch_Full => 16#2D#);
Set_Inversion_Control (Screen,
Normal => Line_Inversion,
Idle => Line_Inversion,
Full_Partial => Line_Inversion);
Set_Power_Control_1 (Screen,
AVDD => 2#101#, -- 5
VRHP => 2#0_0010#, -- 4.6
VRHN => 2#0_0010#, -- -4.6
MODE => 2#10#); -- AUTO
Set_Power_Control_2 (Screen,
VGH25 => 2#11#, -- 2.4
VGSEL => 2#01#, -- 3*AVDD
VGHBT => 2#01#); -- -10
Set_Power_Control_3 (Screen, 16#0A#, 16#00#);
Set_Power_Control_4 (Screen, 16#8A#, 16#2A#);
Set_Power_Control_5 (Screen, 16#8A#, 16#EE#);
Set_Vcom (Screen, 16#E#);
Set_Address (Screen,
X_Start => 0,
X_End => 127,
Y_Start => 0,
Y_End => 159);
Turn_On (Screen);
Initialize_Layer (
Display => Screen,
Layer => 1,
Mode => HAL.Bitmap.RGB_565,
X => 0,
Y => 0,
Width => Width,
Height => Height);
Initialized := True;
end Initialize;
procedure On is
begin
MicroBit.IOs.Set (1, True);
end On;
procedure Off is
begin
MicroBit.IOs.Set (1, False);
end Off;
function Framebuffer return HAL.Bitmap.Any_Bitmap_Buffer is
begin
return Screen.Hidden_Buffer (1);
end Framebuffer;
procedure Update is
begin
Screen.Update_Layers;
end Update;
end Flappybit.LCD;

36
src/flappybit-lcd.ads Normal file
View File

@ -0,0 +1,36 @@
with MicroBit;
with MicroBit.SPI;
with MicroBit.Time;
with ST7735R; use ST7735R;
with HAL.Bitmap;
package Flappybit.LCD is
Height : constant Positive := 160;
Width : constant Positive := 128;
Screen : ST7735R.ST7735R_Screen (
Port => MicroBit.SPI.Controller,
CS => MicroBit.MB_P16'Access,
RS => MicroBit.MB_P12'Access,
RST => MicroBit.MB_P8'Access,
Time => MicroBit.Time.HAL_Delay);
Initialized : Boolean := False;
procedure Initialize;
procedure On
with Pre => Initialized;
procedure Off
with Pre => Initialized;
function Framebuffer return HAL.Bitmap.Any_Bitmap_Buffer
with Pre => Initialized;
procedure Update
with Pre => Initialized;
end Flappybit.LCD;

30
src/flappybit-sprites.adb Normal file
View File

@ -0,0 +1,30 @@
package body Flappybit.Sprites is
function Height (B : in Bird) return Positive is
pragma Unreferenced (B);
begin
return 15;
end Height;
function Width (B : in Bird) return Positive is
pragma Unreferenced (B);
begin
return 15;
end Width;
function Position (B : in Bird) return Point is
begin
return (B.X, B.y);
end Position;
procedure Tick (B : in Bird) is
begin
B.Buffer.Fill_Rect (((B.X, B.Y), B.Width, B.Height));
end Tick;
procedure Set_Framebuffer (B : in out Bird; Buffer : in HAL.Bitmap.Any_Bitmap_Buffer) is
begin
B.Buffer := Buffer;
end Set_Framebuffer;
end Flappybit.Sprites;

26
src/flappybit-sprites.ads Normal file
View File

@ -0,0 +1,26 @@
with HAL.Bitmap;
with Flappybit;
package Flappybit.Sprites is
type Bird is new Flappybit.Sprite with private;
function Height (B : in Bird) return Positive;
function Width (B : in Bird) return Positive;
function Position (B : in Bird) return Point;
procedure Tick (B : in Bird);
procedure Set_Framebuffer (B : in out Bird; Buffer : in HAL.Bitmap.Any_Bitmap_Buffer);
private
type Bird is new Flappybit.Sprite with record
Buffer : HAL.Bitmap.Any_Bitmap_Buffer;
Alive : Boolean := True;
Color : HAL.Bitmap.Bitmap_Color := HAL.Bitmap.Blue;
X : Natural;
Y : Natural;
end record;
end Flappybit.Sprites;

22
src/flappybit.ads Normal file
View File

@ -0,0 +1,22 @@
with HAL.Bitmap;
package Flappybit is
type Point is record
X : Natural;
Y : Natural;
end record;
type Sprite is interface;
function Height (S : in Sprite) return Positive is abstract;
function Width (S : in Sprite) return Positive is abstract;
function Position (S : in Sprite) return Point is abstract;
procedure Tick (S : in Sprite) is abstract;
-- Perform the Sprite's "work" during the runloop
procedure Set_Framebuffer (S : in out Sprite; Framebuffer : in HAL.Bitmap.Any_Bitmap_Buffer ) is abstract;
end Flappybit;

View File

@ -1,47 +1,75 @@
------------------------------------------------------------------------------
-- Copyright (C) 2018, AdaCore --
-- --
-- This is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. This software is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
-- This simple example shows a scrolling text on the micro:bit LED matrix.
--
-- It is intended as first project to get familiar with Ada and GNAT
-- Programing Studio on the micro:bit.
--
-- For more advanced micro:bit features please have a look at the
-- Ada_Drivers_Library project: https://github.com/AdaCore/Ada_Drivers_Library
--
-- The micro:bit has a 5 x 5 multiplexed LED matrix, this means only one LED
-- can be lit at a time. To overcome this limitation, the LEDs are lit one
-- after the other very quickly. This process, called scanning, happens so fast
-- that the human eyes sees all the LED lit together (persistence of vision).
-- In this example, the scan is done with a periodic Timing_Event. Timing_Event
-- is an Ada standard feature that allows you to have a callback executed at a
-- give point in time and can be used to create periodic timers. The periodic
-- timers are implemented in the Generic_Timer package.
--
-- The LED matrix initialization and text scrolling are implemented in the
-- Display package.
--
-- The hardware maping for the GPIOs has been generated from a SVD description
-- file using SVD2Ada: https://github.com/AdaCore/svd2ada
with Display;
with Flappybit.LCD; use Flappybit.LCD;
with HAL.Bitmap; use HAL.Bitmap;
with Flappybit.Sprites;
procedure Main is
Bird : Flappybit.Sprites.Bird;
Center : Positive := 20;
Background : constant Bitmap_Color := Black;
Block : constant Bitmap_Color := Green;
Size : constant := 5;
P : Point := (2, 0);
R : Rect := ( P, Size, 10 );
Left_to_Right : Boolean := True;
begin
Initialize;
--On;/
Framebuffer.Set_Source (Background);
Framebuffer.Fill;
Bird.Set_Framebuffer (Framebuffer);
loop
Display.Scroll_Text ("Make with Ada! ");
Bird.TIck;
Update;
Framebuffer.Set_Source (Block);
Framebuffer.Fill_Rect (R);
if R.Position.Y = 150 then
Left_to_Right := False;
end if;
if R.Position.Y = 0 then
Left_to_Right := True;
end if;
Framebuffer.Set_Source (Background);
if Left_to_Right then
Framebuffer.Draw_Line (
(R.Position.X + 0, R.Position.Y),
(R.Position.X + Size, R.Position.Y) );
R.Position.Y := R.Position.Y + 1;
else
Framebuffer.Draw_Line (
(R.Position.X + 0, R.Position.Y + 10),
(R.Position.X + Size, R.Position.Y + 10) );
R.Position.Y := R.Position.Y - 1;
end if;
Update;
Framebuffer.Set_Source (Block);
Framebuffer.Fill_Rect (R);
Update;
-- end if;
-- if State (Button_A) = Pressed then
-- Framebuffer.Set_Source (Hot_Pink);
-- Framebuffer.Fill_Circle ((Center, Center), 20);
-- Center := (Center + 1);
-- Update;
-- end if;
-- MicroBit.Time.Delay_Ms (1);
end loop;
end Main;