Add template for error handling and fix console printing
This commit is contained in:
parent
3fff14bd0b
commit
291abf3249
5 changed files with 57 additions and 158 deletions
|
|
@ -2,7 +2,7 @@ import sets, strutils
|
||||||
import libnx/[graphics, console, account, input, app]
|
import libnx/[graphics, console, account, input, app]
|
||||||
import libnx/ext/integer128
|
import libnx/ext/integer128
|
||||||
|
|
||||||
proc main() =
|
mainFunction:
|
||||||
graphics.initDefault()
|
graphics.initDefault()
|
||||||
console.init()
|
console.init()
|
||||||
|
|
||||||
|
|
@ -43,5 +43,3 @@ proc main() =
|
||||||
|
|
||||||
if ControllerKey.Plus in keysDown:
|
if ControllerKey.Plus in keysDown:
|
||||||
break
|
break
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import sets
|
||||||
import libnx/[graphics, console, app, input]
|
import libnx/[graphics, console, app, input]
|
||||||
|
|
||||||
|
|
||||||
proc main() =
|
mainFunction:
|
||||||
initDefault()
|
initDefault()
|
||||||
console.init()
|
console.init()
|
||||||
|
|
||||||
|
|
@ -15,5 +15,3 @@ proc main() =
|
||||||
|
|
||||||
if ControllerKey.Plus in keysDown:
|
if ControllerKey.Plus in keysDown:
|
||||||
break
|
break
|
||||||
|
|
||||||
main()
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,40 @@
|
||||||
import libnx/graphics, libnx/wrapper/hid, libnx/wrapper/applet
|
import libnx/graphics, libnx/input, libnx/wrapper/applet
|
||||||
|
|
||||||
|
template mainFunction*(code: untyped): untyped =
|
||||||
|
proc main() =
|
||||||
|
try:
|
||||||
|
code
|
||||||
|
except:
|
||||||
|
let e = getCurrentException()
|
||||||
|
echo ""
|
||||||
|
echo e.getStackTrace()
|
||||||
|
echo getCurrentExceptionMsg()
|
||||||
|
echo ""
|
||||||
|
echo "Your program has crashed. Press + on Controller 1 to exit safely."
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
while appletMainLoop():
|
||||||
|
scanInput()
|
||||||
|
|
||||||
|
let keysDown = keysDown(Controller.P1_AUTO)
|
||||||
|
|
||||||
|
if ControllerKey.Plus in keysDown:
|
||||||
|
break
|
||||||
|
|
||||||
|
flushBuffers()
|
||||||
|
swapBuffers()
|
||||||
|
waitForVSync()
|
||||||
|
try:
|
||||||
|
graphics.exit()
|
||||||
|
quit(0)
|
||||||
|
except:
|
||||||
|
quit(0)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
template mainLoop*(code: untyped): untyped =
|
template mainLoop*(code: untyped): untyped =
|
||||||
while appletMainLoop():
|
while appletMainLoop():
|
||||||
hidScanInput()
|
scanInput()
|
||||||
|
|
||||||
code
|
code
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,18 +37,19 @@ type
|
||||||
tabSize*: int
|
tabSize*: int
|
||||||
fg*: int
|
fg*: int
|
||||||
bg*: int
|
bg*: int
|
||||||
flags*: set[Style]
|
flags*: HashSet[Style]
|
||||||
printCharCallback*: PrintCallback
|
printCharCallback*: PrintCallback
|
||||||
initialised*: bool
|
initialised*: bool
|
||||||
|
|
||||||
DebugDevice* {.size: sizeof(cint), pure.} = enum
|
DebugDevice* {.size: sizeof(cint), pure.} = enum
|
||||||
Null, Service, Console, ThreeDMOO
|
Null, Service, Console, ThreeDMOO
|
||||||
|
|
||||||
|
var currentConsole {.threadvar.}: Console
|
||||||
|
|
||||||
proc toConsole(pconsole: ptr PrintConsole): Console =
|
proc toConsole(pconsole: ptr PrintConsole): Console =
|
||||||
result = new(Console)
|
result = new(Console)
|
||||||
result.pcon = pconsole
|
result.pcon = pconsole
|
||||||
result.font = pconsole.font
|
result.font = pconsole.font
|
||||||
result.cursorX = pconsole.cursorX
|
|
||||||
result.frameBuffer = cast[ptr UncheckedArray[uint32]](pconsole.frameBuffer)
|
result.frameBuffer = cast[ptr UncheckedArray[uint32]](pconsole.frameBuffer)
|
||||||
result.frameBuffer2 = cast[ptr UncheckedArray[uint32]](pconsole.frameBuffer2)
|
result.frameBuffer2 = cast[ptr UncheckedArray[uint32]](pconsole.frameBuffer2)
|
||||||
result.cursorX = pconsole.cursorX
|
result.cursorX = pconsole.cursorX
|
||||||
|
|
@ -86,20 +87,21 @@ proc setWindow*(console: Console, x, y, width, height: int) =
|
||||||
console.windowY = console.pcon.windowY.int
|
console.windowY = console.pcon.windowY.int
|
||||||
|
|
||||||
proc getDefault*(): Console =
|
proc getDefault*(): Console =
|
||||||
|
## Gets the default console with default values
|
||||||
let pcon = consoleGetDefault()
|
let pcon = consoleGetDefault()
|
||||||
result = pcon.toConsole()
|
result = pcon.toConsole()
|
||||||
|
|
||||||
proc select*(console: Console): Console =
|
proc select*(console: Console): Console =
|
||||||
consoleSelect(console.pcon).toConsole()
|
currentConsole = consoleInit(console.pcon).toConsole()
|
||||||
|
return currentConsole
|
||||||
|
|
||||||
proc init*(console: Console = nil): Console {.discardable.} =
|
proc init*(console: Console): Console {.discardable.} =
|
||||||
var newCon = console
|
currentConsole = consoleInit(console.pcon).toConsole()
|
||||||
if newCon.isNil:
|
return currentConsole
|
||||||
newCon = new(Console)
|
|
||||||
|
|
||||||
let pcon = consoleInit(newCon.pcon)
|
proc init*(): Console {.discardable.} =
|
||||||
newCon = pcon.toConsole()
|
currentConsole = consoleInit(nil).toConsole()
|
||||||
return newCon
|
return currentConsole
|
||||||
|
|
||||||
proc debugInit*(device: DebugDevice) =
|
proc debugInit*(device: DebugDevice) =
|
||||||
if device == ThreeDMOO:
|
if device == ThreeDMOO:
|
||||||
|
|
@ -110,17 +112,17 @@ proc debugInit*(device: DebugDevice) =
|
||||||
proc clear*() =
|
proc clear*() =
|
||||||
consoleClear()
|
consoleClear()
|
||||||
|
|
||||||
var printPos: tuple[row: int, col: int] = (0, 0)
|
|
||||||
|
|
||||||
proc setPrintPos*(pos: tuple[row: int, col: int]) =
|
|
||||||
printPos = pos
|
|
||||||
|
|
||||||
proc printAt*(pos: tuple[row: int, col: int], args: varargs[string, `$`]) =
|
proc printAt*(pos: tuple[row: int, col: int], args: varargs[string, `$`]) =
|
||||||
setPrintPos(pos)
|
echo CONSOLE_ESC("2K"), (CONSOLE_ESC("$#;$#H") % [$pos.row, $pos.col]), args.join("")
|
||||||
echo ("\x1b[$#;$#H" % [$pos[0], $pos[1]]), args.join("")
|
currentConsole.pcon.cursorX = pos.col.int32
|
||||||
|
currentConsole.pcon.cursorY = pos.row.int32
|
||||||
|
currentConsole.cursorX = pos.col
|
||||||
|
currentConsole.cursorY = pos.row
|
||||||
|
|
||||||
proc print*(args: varargs[string, `$`]) =
|
proc print*(args: varargs[string, `$`]) =
|
||||||
## Will print at the previously set printPos using ``printAt`` or ``setPrintPos``
|
## Will print at the previously set printPos using ``printAt``
|
||||||
## and then increment the row by one
|
## and then increment the row by one
|
||||||
|
let
|
||||||
|
pcon = currentConsole.pcon
|
||||||
|
printPos = (pcon.cursorY.int+1, pcon.cursorX.int)
|
||||||
printAt printPos, args
|
printAt printPos, args
|
||||||
printPos.row += 1
|
|
||||||
|
|
|
||||||
|
|
@ -26,136 +26,6 @@ type
|
||||||
VibrationInitError* = object of InputError
|
VibrationInitError* = object of InputError
|
||||||
ControllerMergeError* = object of InputError
|
ControllerMergeError* = object of InputError
|
||||||
|
|
||||||
####################### Shared memory data ################################
|
|
||||||
TouchScreenHeader* = ref object
|
|
||||||
timestampTicks*: uint64
|
|
||||||
numEntries*: uint64
|
|
||||||
latestEntry*: uint64
|
|
||||||
maxEntryIndex*: uint64
|
|
||||||
timestamp*: uint64
|
|
||||||
|
|
||||||
TouchScreenEntryHeader* = ref object
|
|
||||||
timestamp*: uint64
|
|
||||||
numTouches*: uint64
|
|
||||||
|
|
||||||
TouchScreenEntryTouch* = ref object
|
|
||||||
timestamp*: uint64
|
|
||||||
padding*: uint32
|
|
||||||
touchIndex*: uint32
|
|
||||||
x*: uint32
|
|
||||||
y*: uint32
|
|
||||||
diameterX*: uint32
|
|
||||||
diameterY*: uint32
|
|
||||||
angle*: uint32
|
|
||||||
padding2*: uint32
|
|
||||||
|
|
||||||
TouchScreenEntry* = ref object
|
|
||||||
header*: TouchScreenEntryHeader
|
|
||||||
touches*: Buffer[HidTouchScreenEntryTouch]
|
|
||||||
unk*: uint64
|
|
||||||
|
|
||||||
TouchScreen* = ref object
|
|
||||||
header*: TouchScreenHeader
|
|
||||||
entries*: Buffer[HidTouchScreenEntry]
|
|
||||||
padding*: Buffer[uint8]
|
|
||||||
|
|
||||||
MouseHeader* = ref object
|
|
||||||
timestampTicks*: uint64
|
|
||||||
numEntries*: uint64
|
|
||||||
latestEntry*: uint64
|
|
||||||
maxEntryIndex*: uint64
|
|
||||||
|
|
||||||
|
|
||||||
type
|
|
||||||
MouseEntry* = ref object
|
|
||||||
timestamp*: uint64
|
|
||||||
timestamp2*: uint64
|
|
||||||
position*: MousePosition
|
|
||||||
buttons*: uint64
|
|
||||||
|
|
||||||
|
|
||||||
type
|
|
||||||
Mouse* = ref object
|
|
||||||
header*: MouseHeader
|
|
||||||
entries*: Buffer[MouseEntry]
|
|
||||||
padding*: Buffer[uint8]
|
|
||||||
|
|
||||||
KeyboardHeader* = ref object
|
|
||||||
timestampTicks*: uint64
|
|
||||||
numEntries*: uint64
|
|
||||||
latestEntry*: uint64
|
|
||||||
maxEntryIndex*: uint64
|
|
||||||
|
|
||||||
KeyboardEntry* = ref object
|
|
||||||
timestamp*: uint64
|
|
||||||
timestamp2*: uint64
|
|
||||||
modifier*: uint64
|
|
||||||
keys*: Buffer[uint32]
|
|
||||||
|
|
||||||
|
|
||||||
KeyboardSection* = ref object
|
|
||||||
header*: KeyboardHeader
|
|
||||||
entries*: array[17, KeyboardEntry]
|
|
||||||
padding*: array[0x00000028, uint8]
|
|
||||||
|
|
||||||
ControllerMAC* = object
|
|
||||||
timestamp*: uint64
|
|
||||||
mac*: array[0x00000008, uint8]
|
|
||||||
unk*: uint64
|
|
||||||
timestamp2*: uint64
|
|
||||||
|
|
||||||
ControllerHeader* = object
|
|
||||||
`type`* {.importc: "type".}: uint32
|
|
||||||
isHalf* {.importc: "isHalf".}: uint32
|
|
||||||
singleColorsDescriptor* {.importc: "singleColorsDescriptor".}: uint32
|
|
||||||
singleColorBody* {.importc: "singleColorBody".}: uint32
|
|
||||||
singleColorButtons* {.importc: "singleColorButtons".}: uint32
|
|
||||||
splitColorsDescriptor* {.importc: "splitColorsDescriptor".}: uint32
|
|
||||||
leftColorBody* {.importc: "leftColorBody".}: uint32
|
|
||||||
leftColorButtons* {.importc: "leftColorButtons".}: uint32
|
|
||||||
rightColorBody* {.importc: "rightColorBody".}: uint32
|
|
||||||
rightColorbuttons* {.importc: "rightColorbuttons".}: uint32
|
|
||||||
|
|
||||||
|
|
||||||
ControllerLayoutHeader* = object
|
|
||||||
timestampTicks* {.importc: "timestampTicks".}: uint64
|
|
||||||
numEntries* {.importc: "numEntries".}: uint64
|
|
||||||
latestEntry* {.importc: "latestEntry".}: uint64
|
|
||||||
maxEntryIndex* {.importc: "maxEntryIndex".}: uint64
|
|
||||||
|
|
||||||
|
|
||||||
HidControllerInputEntry* = object
|
|
||||||
timestamp* {.importc: "timestamp".}: uint64
|
|
||||||
timestamp_2* {.importc: "timestamp_2".}: uint64
|
|
||||||
buttons* {.importc: "buttons".}: uint64
|
|
||||||
joysticks* {.importc: "joysticks".}: array[JOYSTICK_NUM_STICKS, JoystickPosition]
|
|
||||||
connectionState* {.importc: "connectionState".}: uint64
|
|
||||||
|
|
||||||
|
|
||||||
ControllerLayoutSection* = ref object
|
|
||||||
header*: ControllerLayoutHeader
|
|
||||||
entries* {.importc: "entries".}: array[17, HidControllerInputEntry]
|
|
||||||
|
|
||||||
|
|
||||||
ControllerSection* = ref object
|
|
||||||
header* {.importc: "header".}: ControllerHeader
|
|
||||||
layouts* {.importc: "layouts".}: Buffer[ControllerLayoutSection]
|
|
||||||
unk_1*: Buffer[uint8]
|
|
||||||
macLeft*: HidControllerMAC
|
|
||||||
macRight*: HidControllerMAC
|
|
||||||
unk_2* {.importc: "unk_2".}: array[0x00000DF8, uint8]
|
|
||||||
|
|
||||||
|
|
||||||
InputSharedMemory* = ref object
|
|
||||||
header*: Buffer[uint8]
|
|
||||||
touchscreen*: TouchScreen
|
|
||||||
mouse*: Mouse
|
|
||||||
keyboard*: KeyboardSection
|
|
||||||
controllerSerials*: Buffer[uint8]
|
|
||||||
controllers*: Buffer[ControllerSection]
|
|
||||||
|
|
||||||
####################### END Shared memory data ################################
|
|
||||||
|
|
||||||
VibrationDeviceInfo* = HidVibrationDeviceInfo
|
VibrationDeviceInfo* = HidVibrationDeviceInfo
|
||||||
|
|
||||||
VibrationValue* = HidVibrationValue
|
VibrationValue* = HidVibrationValue
|
||||||
|
|
@ -165,7 +35,6 @@ type
|
||||||
JoyconMode {.pure.} = enum
|
JoyconMode {.pure.} = enum
|
||||||
Single, Dual
|
Single, Dual
|
||||||
|
|
||||||
|
|
||||||
proc init*(): Result = hidInitialize().newResult
|
proc init*(): Result = hidInitialize().newResult
|
||||||
proc exit*() = hidExit()
|
proc exit*() = hidExit()
|
||||||
proc reset*() = hidReset()
|
proc reset*() = hidReset()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue