Skip to content

Commit cc90d20

Browse files
committed
Write-String now successfully scrolls the whole text.
1 parent f6aced3 commit cc90d20

File tree

2 files changed

+85
-38
lines changed

2 files changed

+85
-38
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ After that, you need to install [PowerShell-IoT Module](https://github.com/Power
1111
# What's available so far?
1212
This is still in a really early phase. So far I've been "designing" (if you can call it that way) the alphabet so that later we can send a whole text.
1313

14-
* So far you can only play with setting the brightness `Set-Brightness` (Low, Medium or High)
14+
* So far you can only play with setting the brightness `Set-Brightness` (Lowest, Low, Medium, High or Highest)
1515

1616
* Turn the leds off with `Set-LedsOff`
1717

18-
* Write some letters (a string) with `Write-String`. The number of letters actually depend, but it's usually 3 because each letter is using 3 rows. "M" for example uses 5.
19-
20-
After setting the whole alphabet and things to be displayed on the ScrollpHat, will work on a way to scroll the text.
18+
* Write some letters (a string) with `Write-String`. By default, the text is scrolled forever, unless specified otherwise (by passing `$false` as second parameter)
2119

2220

2321
Any suggestion/contribution is really welcome!

ScrollpHat.psm1

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
# Implement your module commands in this script.
2-
$registerObjectArray = @();
3-
for([int] $i = 1; $i -le 11<#numberOfRegisters#>; ++$i){
4-
$registerObjectArray += [PSCustomObject]@{
5-
Address = $i
6-
InUse = $false
7-
}
8-
}
9-
New-Variable -Name "MatrixRegisters" -Value $registerObjectArray -Scope Script
102

11-
New-Variable -Name "InitialEmptyColumns" -Value 0x04, 0x08 -Option Constant -Scope Script
3+
New-Variable -Name "TotalRegisters" -Value 11 -Option Constant -Scope Script
4+
New-Variable -Name "CurrentRegisterValues" -Value @() -Scope Script
125

136
function Select-ScrollpHat {
147
[int]$DeviceAddress = 0x60
@@ -24,35 +17,49 @@ function Set-Brightness{
2417
[CmdletBinding()]
2518
param (
2619
[Parameter(Mandatory=$true)]
27-
[ValidateSet('Low', 'Medium', 'High')]
20+
[ValidateSet('Lowest','Low', 'Medium', 'High', 'Highest')]
2821
[string]$Intensity
2922
)
23+
24+
if($Intensity -eq 'Highest')
25+
{
26+
Write-Warning "This brightness setting causes some weird noise on my phat. I assume that's using too much energy. Use at your own risk!"
27+
}
28+
29+
3030
[int]$LightningEffectRegisterAddress = 0x0D
3131
$IntensityMap = @{
32-
Low = [convert]::toint32('0001001',2)
33-
Medium = [convert]::toint32('0000000',2)
34-
High = [convert]::toint32('0000111',2)
32+
Lowest = [convert]::toint32('1000',2)
33+
Low = [convert]::toint32('1001',2)
34+
Medium = [convert]::toint32('1110',2)
35+
High = [convert]::toint32('0001',2)
36+
Highest = [convert]::toint32('0111',2)
3537
}
3638
Set-I2CRegister -Device $Script:Device -Register $LightningEffectRegisterAddress -Data $IntensityMap[$Intensity]
3739
Update-Registers
3840
}
3941

4042
function Write-String {
4143
param(
42-
[string]$text
44+
[string]$text,
45+
[System.Boolean]$forever = $true
4346
)
44-
for($i =0; $i -lt $text.Length ; ++$i)
47+
Set-LedsOff
48+
do
4549
{
46-
Write-Char $text[$i]
47-
#After Writing the char, make sure to leave a space.
48-
$blankSpaceRegister = Get-NextAvailableRegisters 1
49-
if($blankSpaceRegister -eq $null){ #we have reached the end of the registers....
50-
return
51-
}
52-
Set-I2CRegister -Device $Script:Device -Register $blankSpaceRegister.Address -Data 0
53-
$blankSpaceRegister.InUse = $true
54-
}
55-
Update-Registers
50+
for($i =0; $i -lt $text.Length ; ++$i)
51+
{
52+
Write-Char $text[$i]
53+
#After Writing the char, make sure to leave a space.
54+
55+
if($Script:CurrentRegisterValues.Count -lt $Script:TotalRegisters){ #We can still set an white column
56+
$Script:CurrentRegisterValues += 0
57+
Set-I2CRegister -Device $Script:Device -Register $Script:CurrentRegisterValues.Count -Data 0
58+
}
59+
Update-Registers
60+
Start-Sleep -Milliseconds 10
61+
}
62+
}while($forever)
5663
}
5764

5865
function Write-Char {
@@ -80,13 +87,48 @@ function Write-Char {
8087
###################################
8188
#get respective bits from hashtable
8289
$bitsArray = $alphabet[$char] # this is an array with required data
83-
$registers = Get-NextAvailableRegisters $bitsArray.Count
90+
$totalRegistersRequired = $Script:CurrentRegisterValues.Count + $bitsArray.Count
91+
92+
$registers = ($Script:CurrentRegisterValues.Count+1) .. $totalRegistersRequired
93+
94+
if($totalRegistersRequired -gt $Script:TotalRegisters ) # this means that we will need to start shifting
95+
{
96+
$i = 0
97+
$wroteWhiteSpace = $false
98+
while($i -lt $bitsArray.Count)
99+
{
100+
#send the first value out.
101+
$null, $Script:CurrentRegisterValues = $Script:CurrentRegisterValues
102+
103+
#SHIFT!
104+
for($j = 1 ; $j -le 10; ++$j) #10 because we will leave the 11 to the new value
105+
{
106+
Set-I2CRegister -Device $Script:Device -Register $j -Data $Script:CurrentRegisterValues[$j-1]
107+
}
108+
109+
#start by writing a white column
110+
if($wroteWhiteSpace -eq $false)
111+
{
112+
Set-I2CRegister -Device $Script:Device -Register 0xB -Data 0
113+
$Script:CurrentRegisterValues+= 0
114+
Update-Registers
115+
$wroteWhiteSpace = $true
116+
continue
117+
}
118+
119+
Set-I2CRegister -Device $Script:Device -Register 0xB -Data $bitsArray[$i]
120+
$Script:CurrentRegisterValues += $bitsArray[$i++]
121+
Update-Registers
122+
#Start-Sleep -Milliseconds 200
123+
}
124+
return
125+
}
126+
#if we get here, this is one of the first letters
84127
$i = 0
85128
foreach($register in $registers)
86129
{
87-
Set-I2CRegister -Device $Script:Device -Register $register.Address -Data $bitsArray[$i]
88-
$register.InUse = $true
89-
++$i
130+
Set-I2CRegister -Device $Script:Device -Register $register -Data $bitsArray[$i]
131+
$Script:CurrentRegisterValues += $bitsArray[$i++]
90132
}
91133
}
92134

@@ -97,11 +139,10 @@ function Update-Registers{
97139
}
98140

99141
function Set-LedsOff {
100-
foreach ($register in @($Script:MatrixRegisters | % {$_})) { # this weird sintax flattens the list.
101-
Set-I2CRegister -Device $Script:Device -Register $register.Address -Data 0x0
102-
$register.InUse = $false
142+
foreach ($register in @(1 .. $Script:TotalRegisters)) {
143+
Set-I2CRegister -Device $Script:Device -Register $register -Data 0x0
103144
}
104-
145+
$Script:CurrentRegisterValues = @()
105146
Update-Registers
106147
}
107148

@@ -115,7 +156,15 @@ function Get-NextAvailableRegisters {
115156
param(
116157
[int]$numberOfRequiredRegisters
117158
)
118-
$Script:MatrixRegisters | Where-Object {$_.InUse -eq $false} | Select-Object -First $numberOfRequiredRegisters
159+
160+
$availableRegisters = $Script:MatrixRegisters | Where-Object {$_.InUse -eq $false}
161+
162+
$requestedRegisters = $availableRegisters | Select-Object -First $numberOfRequiredRegisters
163+
164+
return [PSCustomObject]@{
165+
RequestedRegisters = $requestedRegisters
166+
TotalAvailable = $availableRegisters.Count
167+
}
119168

120169
}
121170

0 commit comments

Comments
 (0)