-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor debugging console to use a queue, fix damn race condition in…
… screen buffer
- Loading branch information
1 parent
ecb4e5a
commit c124755
Showing
7 changed files
with
136 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,98 @@ | ||
with Bitmapped_Drawing; | ||
with STM32.Board; | ||
with GUI; | ||
|
||
package body Debug is | ||
|
||
procedure Print (Debug_Level : in Debug; Msg : in String) is | ||
M : Message; | ||
|
||
-- These line variables shouldn't be confused with the array of lines that are drawn to the screen | ||
-- This is specifically for strings that are too long to fit on a single line, so they can be broken up | ||
Lines_Count : constant Positive := (Msg'Length + (Max_Characters_Per_Line - 1)) / Max_Characters_Per_Line; | ||
Lines : array(1 .. Lines_Count) of String(1 .. Max_Characters_Per_Line); | ||
Line_Start : Positive := Lines'First; | ||
begin | ||
for I in 1 .. Lines_Count loop | ||
-- Calculate the start of the next line segment. If it's the last segment, it may be shorter. | ||
if Line_Start + Max_Characters_Per_Line - 1 <= Msg'Length then | ||
Lines(I) := Msg(Line_Start .. Line_Start + Max_Characters_Per_Line - 1); | ||
else | ||
-- Last segment, potentially shorter than Max_Characters_Per_Line | ||
declare | ||
Last_Segment : constant String := Msg(Line_Start .. Msg'Last); | ||
Padded_Last_Segment : constant String(1 .. Max_Characters_Per_Line) := (Last_Segment & (Last_Segment'Length + 1 .. Max_Characters_Per_Line => ' ')); | ||
begin | ||
Lines(I) := Padded_Last_Segment; | ||
end; | ||
|
||
-- Damn why can't it be this simple | ||
-- Lines(I) := Msg(Line_Start .. Msg'Length) & (others => ' '); -- Fill the rest with spaces | ||
end if; | ||
-- Prepare for the next segment | ||
Line_Start := Line_Start + Max_Characters_Per_Line; | ||
end loop; | ||
|
||
for I in Lines'Range loop | ||
M.Msg := Lines(I); | ||
M.Level := Debug_Level; | ||
|
||
-- Any lines after the first don't have the debug heading | ||
if I > 1 then | ||
M.Cont := True; | ||
end if; | ||
|
||
if Messages.Full then | ||
Messages.Pop; | ||
end if; | ||
|
||
Messages.Push (M); | ||
end loop; | ||
end Print; | ||
|
||
protected body Console is | ||
procedure Print (Debug_Level : in Debug; Msg : in String) is | ||
currentLine : constant Natural := | ||
(CURRENT_CONSOLE_POSITION.Y - 100) / FONT_HEIGHT; -- zero indexed | ||
Foreground : HAL.Bitmap.Bitmap_Color; | ||
Background : HAL.Bitmap.Bitmap_Color; | ||
begin | ||
procedure Render is | ||
Foreground : HAL.Bitmap.Bitmap_Color; | ||
Background : HAL.Bitmap.Bitmap_Color; | ||
Pos : HAL.Bitmap.Point := Starting_Point; | ||
Msg : Message; | ||
begin | ||
for I in 0 .. Messages.Size - 1 loop | ||
Msg := Messages.Peek (Queue_Max(I)); | ||
Pos := (Starting_Point.X, Starting_Point.Y + I * (Font_Height + Line_Padding)); | ||
|
||
Foreground := | ||
(case Debug_Level is when Info | Error => HAL.Bitmap.White, | ||
(case Msg.Level is when Info | Error => HAL.Bitmap.White, | ||
when Warning => HAL.Bitmap.Black); | ||
|
||
Background := | ||
(case Debug_Level is when Info => HAL.Bitmap.Blue, | ||
(case Msg.Level is when Info => HAL.Bitmap.Blue, | ||
when Warning => HAL.Bitmap.Yellow, when Error => HAL.Bitmap.Red); | ||
|
||
-- Restart X position to start | ||
Pos.X := Starting_Point.X; | ||
|
||
if currentLine > MAX_LINES then | ||
-- Shift console lines up | ||
for I in 1 .. MAX_LINES loop | ||
CopyLine (I, I - 1, I = MAX_LINES); | ||
end loop; | ||
|
||
-- Adjust Y position to overwrite the last line | ||
CURRENT_CONSOLE_POSITION.Y := | ||
CURRENT_CONSOLE_POSITION.Y - FONT_HEIGHT - LINE_PADDING; | ||
if not Msg.Cont then | ||
-- Draw debug prefix | ||
Bitmapped_Drawing.Draw_String | ||
(Buffer => GUI.Screen_Buffer.all, | ||
Start => | ||
GUI.Scale | ||
((Pos.X, Pos.Y)), | ||
Msg => Msg.Level'Image, Font => BMP_Fonts.Font8x8, | ||
Foreground => Foreground, Background => Background); | ||
|
||
-- Offset X position for debug level header | ||
Pos.X := Pos.X + GUI.MeasureText (Msg.Level'Image, BMP_Fonts.Font8x8).Width; | ||
end if; | ||
|
||
-- Draw debug prefix | ||
Bitmapped_Drawing.Draw_String | ||
(Buffer => GUI.Screen_Buffer.all, | ||
Start => | ||
GUI.Scale | ||
((CURRENT_CONSOLE_POSITION.X, CURRENT_CONSOLE_POSITION.Y)), | ||
Msg => Debug_Level'Image, Font => BMP_Fonts.Font8x8, | ||
Foreground => Foreground, Background => Background); | ||
|
||
-- Draw actual message text | ||
Bitmapped_Drawing.Draw_String | ||
(Buffer => GUI.Screen_Buffer.all, | ||
Start => | ||
GUI.Scale | ||
((CURRENT_CONSOLE_POSITION.X + | ||
GUI.MeasureText (Debug_Level'Image, BMP_Fonts.Font8x8).Width + | ||
LINE_PADDING * 2, | ||
CURRENT_CONSOLE_POSITION.Y)), | ||
Msg => Msg, Font => BMP_Fonts.Font8x8, Foreground => HAL.Bitmap.White, | ||
((Pos.X + Line_Padding, | ||
Pos.Y)), | ||
Msg => Msg.Msg, Font => BMP_Fonts.Font8x8, Foreground => HAL.Bitmap.White, | ||
Background => HAL.Bitmap.Black); | ||
|
||
-- Move the Y position down for the next message | ||
CURRENT_CONSOLE_POSITION.Y := | ||
CURRENT_CONSOLE_POSITION.Y + FONT_HEIGHT + LINE_PADDING; | ||
|
||
STM32.Board.Display.Update_Layer (1, True); | ||
end Print; | ||
|
||
procedure CopyLine | ||
(SrcLineNumber : in Natural; DstLineNumer : in Natural; | ||
DeleteSrc : in Boolean) | ||
is | ||
-- TODO: Cleanup later... | ||
srcToPoint : constant HAL.Bitmap.Point := | ||
(0, | ||
Natural'Min | ||
(CONSOLE_STARTING_POINT.Y + | ||
(FONT_HEIGHT + LINE_PADDING) * SrcLineNumber, | ||
GUI.Board_Resolution.Height - 2)); | ||
dstToPoint : constant HAL.Bitmap.Point := | ||
(0, | ||
Natural'Min | ||
(CONSOLE_STARTING_POINT.Y + | ||
(FONT_HEIGHT + LINE_PADDING) * DstLineNumer, | ||
GUI.Board_Resolution.Height - 2)); | ||
srcRect : constant HAL.Bitmap.Rect := | ||
(Position => srcToPoint, Width => GUI.Board_Resolution.Width, | ||
Height => FONT_HEIGHT + LINE_PADDING); | ||
begin | ||
HAL.Bitmap.Copy_Rect | ||
(Src_Buffer => GUI.Screen_Buffer.all, Src_Pt => srcToPoint, | ||
Dst_Buffer => GUI.Screen_Buffer.all, Dst_Pt => dstToPoint, | ||
Width => GUI.Board_Resolution.Width, | ||
Height => FONT_HEIGHT + LINE_PADDING, Synchronous => True); | ||
if DeleteSrc then | ||
HAL.Bitmap.Fill_Rect | ||
(Buffer => GUI.Screen_Buffer.all, Area => srcRect); | ||
end if; | ||
STM32.Board.Display.Update_Layer (1, True); | ||
end CopyLine; | ||
end Console; | ||
|
||
end loop; | ||
end Render; | ||
end Debug; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
with HAL.Bitmap; | ||
with BMP_Fonts; | ||
with Queues; | ||
|
||
package Debug is | ||
type Debug is (Error, Warning, Info); | ||
|
||
FONT_HEIGHT : constant Natural := | ||
Font_Height : constant Natural := | ||
BMP_Fonts.Char_Height (Font => BMP_Fonts.Font8x8); | ||
MAX_LINES : constant Natural := 20; | ||
LINE_PADDING : constant Natural := 2; | ||
Max_Characters_Per_Line : constant Positive := 50; | ||
Line_Padding : constant Natural := 4; | ||
Starting_Point : constant HAL.Bitmap.Point := (0, 100); | ||
Max_Messages : constant := 14; | ||
|
||
protected Console is | ||
procedure Print (Debug_Level : in Debug; Msg : in String); | ||
procedure CopyLine | ||
(SrcLineNumber : in Natural; DstLineNumer : in Natural; | ||
DeleteSrc : in Boolean); | ||
private | ||
CONSOLE_STARTING_POINT : HAL.Bitmap.Point := (0, 100); | ||
CURRENT_CONSOLE_POSITION : HAL.Bitmap.Point := (0, 100); | ||
end Console; | ||
type Message is record | ||
Msg : String(1 .. Max_Characters_Per_Line); | ||
Level : Debug; | ||
Cont : Boolean := False; | ||
end record; | ||
|
||
type Queue_Max is mod Max_Messages; | ||
package Message_Queue is new Queues (Queue_Max, Message); | ||
Messages : Message_Queue.Queue; | ||
|
||
procedure Print (Debug_Level : in Debug; Msg : in String); | ||
procedure Render; | ||
end Debug; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters