Skip to content

Commit 63898ec

Browse files
committed
Simplified the "SadJoey" script. Added a simple setup.
Updated the module, so that functions now respect the following convention: [Verb]-[Phat][Noun]
1 parent ad452b1 commit 63898ec

File tree

5 files changed

+146
-41
lines changed

5 files changed

+146
-41
lines changed

.vscode/launch.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "PowerShell",
9+
"request": "launch",
10+
"name": "PowerShell Launch Current File",
11+
"script": "${file}",
12+
"args": [],
13+
"cwd": "${file}"
14+
},
15+
{
16+
"type": "PowerShell",
17+
"request": "launch",
18+
"name": "PowerShell Launch Current File in Temporary Console",
19+
"script": "${file}",
20+
"args": [],
21+
"cwd": "${file}",
22+
"createTemporaryIntegratedConsole": true
23+
},
24+
{
25+
"type": "PowerShell",
26+
"request": "launch",
27+
"name": "PowerShell Launch Current File w/Args Prompt",
28+
"script": "${file}",
29+
"args": [
30+
"${command:SpecifyScriptArgs}"
31+
],
32+
"cwd": "${file}"
33+
},
34+
{
35+
"type": "PowerShell",
36+
"request": "attach",
37+
"name": "PowerShell Attach to Host Process",
38+
"processId": "${command:PickPSHostProcess}",
39+
"runspaceId": 1
40+
},
41+
{
42+
"type": "PowerShell",
43+
"request": "launch",
44+
"name": "PowerShell Interactive Session",
45+
"cwd": "${workspaceRoot}"
46+
}
47+
]
48+
}

Initialize_SadJoey.ps1

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
Import-Module Microsoft.PowerShell.IoT
22

3-
function Set-RegisterValue {
4-
param($Device, [int]$Register, [int]$Value)
5-
######### Update register Address and value #########
6-
[int]$UpdateRegisterAddress = 0x0C
7-
[int]$UpdateValue = 0xFF # for what I understood, it can be any value " A write operation of any 8-bit value to the Update Column Register is required to update the Data Registers"
8-
9-
Set-I2CRegister -Device $Device -Register $Register -Data $Value #Write the value
10-
Write-Host "Device: $($Device) - Value: $($Value) - Register: $($Register)"
11-
Set-I2CRegister -Device $Device -Register $UpdateRegisterAddress -Data $UpdateValue #update the register
12-
}
13-
143
[int]$DeviceAddress = 0x60
15-
164
######### Configuration Register and Value #########
17-
185
[int]$ConfigurationRegisterAddress = 0x00
196
[int]$ConfigurationRegisterValue = 0x1B
207

21-
######### Matrix Registers and value #########
22-
[int[]]$Matrix1DataRegisterAddress = 0x01 ..0x0B
23-
[int[]]$values = 0x0,0x0,0x0,0x11,0x08,0x08,0x08,0x11,0x0,0x0,0x0
8+
######### Data Registers and value #########
9+
[int[]]$DataRegisterAddress = 0x04 ..0x08
10+
[int[]]$values = 0x12,0x08,0x08,0x08,0x12
2411
######### Lightning Effect Register and value #########
2512
[int]$LightningEffectRegisterAddress = 0x0D
26-
[int]$LightningEffectRegisterValue = 0x08
13+
[int]$LightningEffectRegisterValue = 0x08 #Lowest intensity
14+
2715
######### Get the device and set the Configuration Register
2816
$Device = Get-I2CDevice -Id $DeviceAddress -FriendlyName phat
2917
Set-I2CRegister -Device $Device -Register $ConfigurationRegisterAddress -Data $ConfigurationRegisterValue
@@ -32,9 +20,15 @@ Set-I2CRegister -Device $Device -Register $ConfigurationRegisterAddress -Data $C
3220
Set-I2CRegister -Device $Device -Register $LightningEffectRegisterAddress -Data $LightningEffectRegisterValue
3321

3422
######### Write the #sadJoey pattern to the Data registers #########
35-
3623
$i = 0
37-
foreach ($register in $Matrix1DataRegisterAddress) {
38-
Set-RegisterValue -Device $Device -Register $register -Value $values[$i]
24+
foreach ($register in $DataRegisterAddress) {
25+
Set-I2CRegister -Device $Device -Register $register -Data $values[$i]
3926
$i++
40-
}
27+
}
28+
29+
#In order to update the registers, we need to write something to the column register, accoding to the datasheet: "A write operation of any 8-bit value to the Update Column Register is required to update the Data Registers"
30+
[int]$UpdateRegisterAddress = 0x0C
31+
[int]$UpdateValue = 0xFF
32+
33+
#After executing this instruction, a Sad Joey should appear on your pHat :)
34+
Set-I2CRegister -Device $Device -Register $UpdateRegisterAddress -Data $UpdateValue

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ If you don't have PowerShell installed on your Raspberry, follow [this guide](ht
99
After that, you need to install [PowerShell-IoT Module](https://github.com/PowerShell/PowerShell-IoT#installation)
1010

1111
# What's available so far?
12-
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.
1312

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

1615
* Turn the leds off with `Set-LedsOff`
1716

ScrollpHat.psm1

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Select-ScrollpHat {
1313
$Script:Device
1414
}
1515

16-
function Set-Brightness{
16+
function Set-PhatBrightness{
1717
[CmdletBinding()]
1818
param (
1919
[Parameter(Mandatory=$true)]
@@ -36,40 +36,43 @@ function Set-Brightness{
3636
Highest = [convert]::toint32('0111',2)
3737
}
3838
Set-I2CRegister -Device $Script:Device -Register $LightningEffectRegisterAddress -Data $IntensityMap[$Intensity]
39-
Update-Registers
39+
Update-PhatRegisters
4040
}
4141

42-
function Write-String {
42+
function Write-PhatString {
4343
[CmdletBinding()]
4444
param(
4545
[parameter(ValueFromPipeline=$True)]
4646
[string]$text,
4747
[int]$WaitMiliseconds = 40,
4848
[System.Boolean]$forever = $false
4949
)
50-
Set-LedsOff
50+
Set-PhatLedsOff
51+
if($forever)
52+
{
53+
$text +=" "
54+
}
5155
$iterations = 0
52-
Write-Host "Before While"
5356
do
5457
{
5558
for($i =0; $i -lt $text.Length ; ++$i)
5659
{
57-
Write-Char $text[$i]
60+
Write-PhatChar $text[$i]
5861
#After Writing the char, make sure to leave a space.
5962
# Write-Host "Wrote char $($text[$i])"
6063
if($Script:CurrentRegisterValues.Count -lt $Script:TotalRegisters){ #We can still set an white column
6164
$Script:CurrentRegisterValues += 0
6265
Set-I2CRegister -Device $Script:Device -Register $Script:CurrentRegisterValues.Count -Data 0
6366
}
64-
Update-Registers
67+
Update-PhatRegisters
6568
Start-Sleep -Milliseconds $WaitMiliseconds
6669
}
6770
# $iterations +=1
6871
# Write-Host $iterations
6972
}while($forever)
7073
}
7174

72-
function Write-Char {
75+
function Write-PhatChar {
7376
param(
7477
[ValidateLength(1,1)]
7578
[string]$char
@@ -114,6 +117,7 @@ function Write-Char {
114117
"0" = 0x0E, 0x15, 0x0E
115118
"!" = 0x17
116119
" " = 0X00,0X00
120+
"-" = 0x04,0x04,0x04
117121
}
118122
###################################
119123
#get respective bits from hashtable
@@ -135,24 +139,22 @@ function Write-Char {
135139
for($j = 1 ; $j -le 10; ++$j) #10 because we will leave the 11 to the new value
136140
{
137141
Set-I2CRegister -Device $Script:Device -Register $j -Data $Script:CurrentRegisterValues[$j-1]
138-
Update-Registers
142+
#Update-PhatRegisters
139143
}
140-
141-
142144
#start by writing a white column
143145
if($wroteWhiteSpace -eq $false)
144146
{
145147
Set-I2CRegister -Device $Script:Device -Register 0xB -Data 0
146148
$Script:CurrentRegisterValues+= 0
147-
#Update-Registers
149+
#Update-PhatRegisters
148150
$wroteWhiteSpace = $true
149151
continue
150152
}
151153

152154
Set-I2CRegister -Device $Script:Device -Register 0xB -Data $bitsArray[$i]
153155
$Script:CurrentRegisterValues += $bitsArray[$i++]
154-
Update-Registers
155-
Start-Sleep -Milliseconds 5
156+
Update-PhatRegisters
157+
#Start-Sleep -Milliseconds 5
156158
}
157159
return
158160
}
@@ -165,21 +167,21 @@ function Write-Char {
165167
}
166168
}
167169

168-
function Update-Registers{
170+
function Update-PhatRegisters{
169171
[int]$UpdateRegisterAddress = 0x0C
170172
[int]$UpdateValue = 0xFF # for what I understood, it can be any value " A write operation of any 8-bit value to the Update Column Register is required to update the Data Registers"
171173
Set-I2CRegister -Device $Script:Device -Register $UpdateRegisterAddress -Data $UpdateValue
172174
}
173175

174-
function Set-LedsOff {
176+
function Set-PhatLedsOff {
175177
foreach ($register in @(1 .. $Script:TotalRegisters)) {
176178
Set-I2CRegister -Device $Script:Device -Register $register -Data 0x0
177179
}
178180
$Script:CurrentRegisterValues = @()
179-
Update-Registers
181+
Update-PhatRegisters
180182
}
181183

182-
function Reset-Registers {
184+
function Reset-PhatRegisters {
183185
$resetRegisterAddress = 0xFF
184186
$resetRegisterValue = 0xF #can be any value
185187
Set-I2CRegister -Device $Script:Device -Register $resetRegisterAddress -Data $resetRegisterValue
@@ -198,9 +200,38 @@ function Get-NextAvailableRegisters {
198200
RequestedRegisters = $requestedRegisters
199201
TotalAvailable = $availableRegisters.Count
200202
}
203+
}
201204

205+
function Set-PhatCrazyLeds () {
206+
$brightnessValues = 'Lowest','Low', 'Medium', 'High', 'Highest'
207+
$iteration
208+
while($true)
209+
{
210+
$randomRegister = Get-Random -Minimum 1 -Maximum 11
211+
$randomValue = Get-Random -Minimum 0 -Maximum 17
212+
213+
Set-I2CRegister -Device $Script:Device -Register $randomRegister -Data $randomValue
214+
#if($iteration % 4 -eq 0)
215+
#{
216+
$brightnessIndex = Get-Random -Minimum 0 -Maximum ($brightnessValues.Count-1)
217+
Set-PhatBrightness -Intensity ($brightnessValues[$brightnessIndex])
218+
#}
219+
220+
Update-PhatRegisters
221+
Start-Sleep -Milliseconds 30
222+
#++$iteration
223+
}
202224
}
203225

226+
# function Get-SpotifyCurrentlyPlayingInfo()
227+
# {
228+
# $header = @{"Authorization" = Get-Content -Path "/home/pi/token.txt"}
229+
230+
# $trackInfo = ((Invoke-WebRequest -Uri https://api.spotify.com/v1/me/player/currently-playing?market=PT -Headers $header).Content | ConvertFrom-Json).item
231+
232+
# $trackInfo.Artists[0].Name + " - "+ $trackInfo.name
233+
# }
234+
204235
Select-ScrollpHat
205236

206237
# Export only the functions using PowerShell standard verb-noun naming.

simple_Setup.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Import-Module Microsoft.PowerShell.IoT
2+
3+
[int]$DeviceAddress = 0x60
4+
[int]$ConfigurationRegisterAddress = 0x00
5+
[int]$ConfigurationRegisterValue = 0x1B
6+
# Get the scroll pHat from I2C.
7+
$Device = Get-I2CDevice -Id $DeviceAddress -FriendlyName phat
8+
# Set the configuration register with the respective configuration value
9+
Set-I2CRegister -Device $Device -Register $ConfigurationRegisterAddress -Data $ConfigurationRegisterValue
10+
11+
#Let's Write the A letter. For that, we will need 3 Registers
12+
#Registers: 1 2 3
13+
# x
14+
# x x
15+
# x x x
16+
# x x
17+
# x x
18+
19+
$registers = 0x01..0x3
20+
$letterA = 0x3E, 0x05, 0x3E
21+
22+
$index = 0
23+
#Set the value on $letterA array on the correspondent register.
24+
foreach($register in $registers)
25+
{
26+
Set-I2CRegister -Device $Device -Register $register -Data $letterA[$index]
27+
++$index
28+
}
29+
#In order to update the registers, we need to write something to the column register, accoding to the datasheet: "A write operation of any 8-bit value to the Update Column Register is required to update the Data Registers"
30+
[int]$UpdateRegisterAddress = 0x0C
31+
[int]$UpdateValue = 0xFF
32+
#After executing this instruction, you should see the A letter on your Scroll pHat :)
33+
Set-I2CRegister -Device $Device -Register $UpdateRegisterAddress -Data $UpdateValue

0 commit comments

Comments
 (0)