Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Thursday, 30 October 2008

Start Media Center on a Particular Channel

I was wandering the other day: how is it possible to start Windows Media Center, in fullscreen mode, on a particular channel (BBC News 24), instead of having to go through the menu system, say, in the morning when my light turns on? (except this time it'll be my computer that switches it on, not the alarm clock)

I started googling for command line Media Center (ehshell.exe) operators/switches and quickly came across this page.

This bit of script (in a batch file) will start Media Center, maximised, on the last channel that was being watched:

start /MAX %systemroot%\ehome\ehshell.exe /homepage:VideoFullscreen.xml
(all one line)

There were no parameters that could be passed to the .exe that would specify what channel was going to be displayed. The fact that Media Center remembered the last channel suggested it had to be stored somewhere. I had a look in the ehome directory to see if there was anything in any files there that changed when the channel changed, all to no avail. The next stop was the registry.

Under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ the key Media Center can be found, and within that, Settings, within that, VideoSettings. Now, in this key you'll find a string (REG_SZ) called _tvChannel, this string contains the last/current channel you were/are on (_tvChannelPrev contains the one before that); change it, then when Media Center starts, and you navigate to Live TV, it starts on that channel.

Channel 80, in the UK (freeview), is BBC News 24. Just change the 80 to whatever channel you want. There are two ways I've combined the above to make it easier; the first involves a batch script and a .reg file:

tvChannel.reg ...

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Media Center\Settings\VideoSettings]
"_tvChannel"="80"


startNews.bat ...

@echo off
echo Ammending entry in registry...
regedit /s tvChannel.reg
echo Starting Media Center at channel set in .reg file...
start /MAX %systemroot%\ehome\ehshell.exe /homepage:VideoFullscreen.xml
exit


Running startNews.bat will start Media Center at the channel specified in tvChannel.reg. This, however, isn't very flexible; files need to be manually edited if a different channel is required. A python script would be better, one which outputed the channel, passed as a command line argument, to a .reg file. which was then called. Please keep in mind this is a quick script, i'm sure it could be quite heavily optimised, but it does what it needs to do. Here is is:


Sorry about the image, blogger/blogspot strips out the tabs (as well as, disgusting as they are, spaces instead of tabs). The same could be achieved with a more advanced batch script, but, to me, a python script seemed much easier. I saved this python script as channelLaunch.py so when I use it, the command is:

python channelLaunch.py 80

That's it! Hope it can be of some use to someone.

Thanks

Chris

Monday, 25 August 2008

Simple Soduku Solver

Here is some quick code for a simple Sudoku solver.

This python script will not work for a Sudoku puzzle where guessing is required; that functionality has not yet been worked in - but it will be.

The Puzzle is solved logically by working out what each cell of the grid could be, based on the well known rules of Sudoku. If there is only one possibility for a cell, you know that that has got to be the value. This process is repeated, after each modification has been made, until the grid is complete.

I tested it using http://www.websudoku.com, and it could solve any of the 'easy' puzzles. It could not get any higher than 'medium'.

The Python Script:

import time

##defs##

#find possibilities for rows
def rowPoss(row):
output = []
for i in range(9):
if not (i+1) in rows[row]:
output.append(i+1)
return output

#############################################

#find possibilitites for columns
def colPoss(col):
output = []
column = []
for i in range(9):
column.append(rows[i][col])
for i in range(9):
if not (i+1) in column:
output.append(i+1)
return output

#############################################

#find possibilities for each square
def squarePoss(square):

###########
#000#111#222#
#000#111#222#
#000#111#222#
###########
#333#444#555#
#333#444#555#
#333#444#555#
###########
#666#777#888#
#666#777#888#
#666#777#888#
###########

#set/get square values
temp = []
temp.append(rows[(square / 3)*3][(square % 3)*3])
temp.append(rows[(square / 3)*3][1+(square % 3)*3])
temp.append(rows[(square / 3)*3][2+(square % 3)*3])
temp.append(rows[1+(square / 3)*3][(square % 3)*3])
temp.append(rows[1+(square / 3)*3][1+(square % 3)*3])
temp.append(rows[1+(square / 3)*3][2+(square % 3)*3])
temp.append(rows[2+(square / 3)*3][(square % 3)*3])
temp.append(rows[2+(square / 3)*3][1+(square % 3)*3])
temp.append(rows[2+(square / 3)*3][2+(square % 3)*3])

#return a list of possible values
output = []
for i in range(9):
if not (i+1) in temp:
output.append(i+1)
return output

#############################################

#find all of the possibilities for a particular cell
def cellPoss(row, col):
output = []
for i in range(9):
if ((i+1) in rp[row]) and ((i+1) in cp[col]) and ((i+1) in sp[((row/3)*3)+(col/3)]):
output.append(i+1)
if len(output) == 1:
return output[0]
else:
return output

#############################################

#rows
r0 = [9,1,0,7,0,0,0,0,5]
r1 = [5,0,0,0,0,0,0,7,0]
r2 = [6,0,0,3,0,8,0,0,0]
r3 = [0,3,6,0,9,0,8,0,0]
r4 = [0,0,5,8,6,7,1,0,0]
r5 = [0,0,7,0,3,0,5,4,0]
r6 = [0,0,0,1,0,3,0,0,8]
r7 = [0,8,0,0,0,0,0,0,2]
r8 = [3,0,0,0,0,9,0,5,1]

rows = [r0,r1,r2,r3,r4,r5,r6,r7,r8]

#######################################
#OR, for user input - uncomment
#enter values row by row - just [ENTER] for blank

for i in range(9):
for j in range(9):
s = raw_input('::')
if s == '':
rows[i][j] = 0
else:
rows[i][j] = int(s)

########################################

cellPossibilities = rows
#now find each cell's possibilities
#cellPossibilities[row][column]

#get start time
startTime = time.time()

solved = 0
solvedPrev = -1

while solved < 81:

rp = []
for row in range(9):
rp.append(rowPoss(row))

cp = []
for column in range(9):
cp.append(colPoss(column))

sp = []
for square in range(9):
sp.append(squarePoss(square))

for row in range(9):
for col in range(9):
if (cellPossibilities[row][col] == 0) or (type(cellPossibilities[row][col]) == list):
cellPossibilities[row][col] = cellPoss(row, col)

solvedPrev = solved
solved = 0
for i in range(9):
for j in range(9):
if type(cellPossibilities[i][j]) == int:
solved += 1

if solved == solvedPrev:
print 'too difficult - guessing required'
solved = 81 #'trick it' into finishing


for i in range(9):
print cellPossibilities[i]

print 'It took ', round((time.time()-startTime), 3), ' seconds'



Improvements coming soon...

Thanks for reading,


Chris

EDIT: Appologies for the above code, the indentations seem to have been removed.