Tuesday, 28 February 2017

Arduino IDE error - avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied


If you run Arduino IDE on Ubuntu (Arduino 1.5.7 and Ubuntu 14.04 in my case), most possibly you cannot upload to Arduino board, caused by the error of:

avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied
ioctl("TIOCMGET"): Inappropriate ioctl for device

To fix it, enter the command:
sudo usermod -a -G dialout <username>
sudo chmod a+rw /dev/ttyACM0


Where <username> is your user name in Ubuntu, /dev/ttyACM0 is the detected device of your Arduino board.


ref:
http://arduino-er.blogspot.ca/2014/08/arduino-ide-error-avrdude-seropen-cant.html

Sunday, 26 February 2017

Sync Remote Deleted Branches

I finished a feature on Friday and deleted the feature_xxx branch on bitbucket (remote) and my office computer (local). But I want to continue working during the weekend at home with my home computer. I find that the feature_xxx branch is still shown on my home computer, remotely and locally, which means if I do: git branch -a, I get:

      develop
    * feature_xxx
      master
      remotes/origin/develop
      remotes/origin/feature_xxx
      remotes/origin/master

Of course, if I login my bitbucket with a webpage, I could see there are only develop and master left.
How could I keep up to date?

I tried git pull, things seem to be updated with a bunch of green +++++s and red ------s but still showing the feature_xxx branch. 

The reason is: the branches tracked by remotes/origin/* are only a cache of the remote server, and the deleted branch cannot be updated by git fetch.

///////////////////////////////////////////////////////////////////////////////////////////

Solution:

step 0:
git remote show origin

Got:
* remote origin
  Fetch URL: https://boris@bitbucket.org/xxx/yyy.git
  Push  URL: https://boris@bitbucket.org/xxx/yyy.git
  HEAD branch: master
  Remote branches:
    develop                                          tracked
    master                                           tracked
    refs/remotes/origin/feature_xxx stale (use 'git remote prune' to remove)
  Local branches configured for 'git pull':
    develop                      merges with remote develop
    feature_xxx merges with remote feature_xxx
    master                       merges with remote master
  Local refs configured for 'git push':
    develop pushes to develop (up to date)
    master  pushes to master  (up to date)

step 1:
git remote prune origin 

Got:
Pruning origin
URL: https://boris@bitbucket.org/xxx/yyy.git
 * [pruned] origin/feature_xxx

step 2:
Now that the deleted remote branch has been synchronized, we can delete the local branch which doesn't exist anymore.
git branch -d feature_xxx

Note that
(1) you might need to checkout to another branch before deleting it (eg develop).
git checkout develop

(2) you might also get:
       error: The branch 'feature_xxx' is not fully merged.
       If you are sure you want to delete it, run 'git branch -D feature_xxx'.
This is because the local didn't have the updated data yet. Just do:
git pull

Finally, check it out: git branch -a
Got:
    * develop
      master
      remotes/origin/develop
      remotes/origin/master


ref:
https://higoge.github.io/2015/07/07/git-remote05/

Thursday, 16 February 2017

State Machines with C


Thank you very much to John Santic who gave a complete example of FSM in C:

One common way of conquering difficult software design problems is to use a state machine. First you figure out all the states the software can be in. Then you determine all the inputs to the state machine—all the events that can cause the state machine to take some action or to change states. Finally you determine the state machine outputs—all the actions that the state machine can perform.
When your state machine design is done, you'll have a list of states, a list of events (inputs), and a set of action procedures for each state that describe what the state machine does for each event (outputs).
There are two ways to code a state machine in C. One way uses a set of nested switch statements. The outer switch has a case for each possible state. Each of these outer cases has an inner switch with a case for each possible event. The actual code that gets selected performs the actions for that state/event. Alternately, the outer switch could have a case for each event, and the inner switch could have a case for each state.
Another more concise way of coding is to use a lookup table. First, number all your states consecutively, starting with 0—an enum is a convenient way to do this. Do the same for your events. Then make up a set of tables, one table per state. Each table has one entry per event, in the same order as the event enum. Then the entire set of tables is arranged in the same order as the state enum. Each item in a table is the function to execute to perform the action for that particular event in that particular state.
The listing below is an example with three states and two events, and therefore six action procedures.
/* Define the states and events. If your state machine program has multiple
source files, you would probably want to put these definitions in an "include"
file and #include it in each source file. This is because the action
procedures need to update current_state, and so need access to the state
definitions. */

enum states { STATE_1, STATE_2, STATE_3, MAX_STATES } current_state;
enum events { EVENT_1, EVENT_2, MAX_EVENTS } new_event;

/* Provide the fuction prototypes for each action procedure. In a real
program, you might have a separate source file for the action procedures of 
each state. Then you could create a .h file for each of the source files, 
and put the function prototypes for the source file in the .h file. Instead 
of listing the prototypes here, you would just #include the .h files. */

void action_s1_e1 (void);
void action_s1_e2 (void);
void action_s2_e1 (void);
void action_s2_e2 (void);
void action_s3_e1 (void);
void action_s3_e2 (void);
enum events get_new_event (void);

/* Define the state/event lookup table. The state/event order must be the
same as the enum definitions. Also, the arrays must be completely filled - 
don't leave out any events/states. If a particular event should be ignored in 
a particular state, just call a "do-nothing" function. */

void (*const state_table [MAX_STATES][MAX_EVENTS]) (void) = {

    { action_s1_e1, action_s1_e2 }, /* procedures for state 1 */
    { action_s2_e1, action_s2_e2 }, /* procedures for state 2 */
    { action_s3_e1, action_s3_e2 }  /* procedures for state 3 */
};

/* This is the heart of the state machine - where you execute the proper 
action procedure based on the new event you have to process and your current 
state. It's important to make sure the new event and current state are 
valid, because unlike "switch" statements, the lookup table method has no 
"default" case to catch out-of-range values. With a lookup table, 
out-of-range values cause the program to crash! */

void main (void)
{
    new_event = get_new_event (); /* get the next event to process */

    if (((new_event >= 0) && (new_event < MAX_EVENTS))
    && ((current_state >= 0) && (current_state < MAX_STATES))) {

        state_table [current_state][new_event] (); /* call the action procedure */

    } else {

        /* invalid event/state - handle appropriately */
    }
}

/* In an action procedure, you do whatever processing is required for the
particular event in the particular state. Among other things, you might have
to set a new state. */

void action_s1_e1 (void)
{
    /* do some processing here */

    current_state = STATE_2; /* set new state, if necessary */
}

void action_s1_e2 (void) {}  /* other action procedures */
void action_s2_e1 (void) {}
void action_s2_e2 (void) {}
void action_s3_e1 (void) {}
void action_s3_e2 (void) {}

/* Return the next event to process - how this works depends on your
application. */

enum events get_new_event (void)
{
    return EVENT_1;
}

ref:
http://johnsantic.com/comp/state.html     !!!!!!!!!!!!!!!!!!
http://codeandlife.com/2013/10/06/tutorial-state-machines-with-c-callbacks/
https://gist.github.com/nmandery/1717405
http://c.biancheng.net/cpp/html/99.html

Tuesday, 14 February 2017

Practical Stuff: Git Branches and Git Flow


Nothing fancy, just my daily mostly used commands.

a. create develop branch
git branch develop
git push -u origin develop
b. create a new Feature branch
git checkout -b some-feature develop
# Optionally, push branch to origin:
git push -u origin some-feature    

# make some changes...   
git status
git add some-file
git commit
c. Finish a working Feature
git pull origin develop
git checkout develop
git merge --no-ff some-feature
git push origin develop

git branch -d some-feature

# If you pushed branch to origin:
git push origin --delete some-feature
d. To Relase
git checkout -b release-0.1.0 develop

# Optional: Bump version number, commit
# Prepare release, commit
e. Finish a Release
git checkout master
git merge --no-ff release-0.1.0
git push

git checkout develop
git merge --no-ff release-0.1.0
git push

git branch -d release-0.1.0

# If you pushed branch to origin:
git push origin --delete release-0.1.0   

git tag -a v0.1.0 master
git push --tags
f. To make a Hotfix
git checkout -b hotfix-0.1.1 master
g. Finish a Hotfix
git checkout master
git merge --no-ff hotfix-0.1.1
git push

git checkout develop
git merge --no-ff hotfix-0.1.1
git push

git branch -d hotfix-0.1.1

git tag -a v0.1.1 master
git push --tags

# if later, a tagged version wants to be checked out. 
git checkout v0.1.1
# if want to check the information in a tag
git cat-file tag <tagname>


h. Stash

git stash
git stash list

git stash apply
git stash apply stash@{2}


i. Undo a merge
git checkout the branch you were on when merging
git reset --hard

j. dump current local changes
git checkout .

k. Your PM created a branch in the Remote Server, now you need to work on it. Or upon pull request, don't pull (fetch + merge) but fetch first.

git fetch origin SOF-123-new-branch:SOF-123-new-branch
l. wanna see the diff?
$ git diff tag1 tag2
or show log between them:
$ git log tag1..tag2

sometimes it may be convenient to see only the list of files that were changed:
$ git diff tag1 tag2 --stat

and then look at the differences for some particular file:
$ git diff tag1 tag2 -- some/file/name

diff from a branch to a tag:
- switch to that branch (checkout)
- $ git diff tag-v1.0.0





ref:
http://www.codeceo.com/article/how-to-use-git-flow.html    !!!!!!!!!!!
https://higoge.github.io/2015/07/07/git-basic06/   !!!!!!!!!!!!!!!!!
http://nvie.com/posts/a-successful-git-branching-model/       !!!!!!!!
https://github.com/geeeeeeeeek/git-recipes/wiki                    !!!!
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E4%BD%9C%E6%B5%81
https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%82%A8%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86
https://segmentfault.com/q/1010000000140446
http://blog.csdn.net/leedaning/article/details/51304690
http://xigua366.iteye.com/blog/2400324
https://www.oschina.net/translate/git-fetch-and-merge?print
https://stackoverflow.com/questions/3211809/git-diff-between-given-two-tags
https://stackoverflow.com/questions/4185888/how-do-i-read-tagger-information-from-a-git-tag


Monday, 13 February 2017

I2C Competing Error

I've already made those I2C devices lined up with delays but there is still a chance they migh fail ending it: getting return = 1 from Wire.endTransmission();

The solution is when ever catched a non-zero return from Wire.endTransmission(); just Wire.begin() again to re-initialize the I2C bus. 

ref:
https://community.particle.io/t/i2c-connection-randomly-fails-on-p1-photon/13865/4      !!!!!!!
https://community.particle.io/t/strange-i2c-bus-issues-with-photon/13875
https://community.particle.io/t/photon-i2c-bus-issues/13416/18



Start a Empty Branch in Git

We are trying to create an EMPTY branch called gh-pages:
$cd repo

$ git checkout --orphan gh-pages
# create an orphan branch and it is independent
Switched to a new branch 'gh-pages'

git rm -rf .
# delete all the files under the original source tree
rm '.gitignore'
Note that you won't see the current new branch if you do git branch until the first commit happens. So let's now add something.
$ echo \"My GitHub Page\" > index.html
$ git add .
$ git commit -a -m \"First pages commit\"
$ git push origin gh-pages
After commit, we can now see the new branch with git branch and we can push it to the remote repo.

ref:
http://skyao.github.io/2015/03/18/git-create-empty-branch/

How to Solve CONFLICT When Trying to merge branches

We would like to merge branch issue2 and branch issue3 to master.
So first, switch to master and merge issue2:
$ git checkout master
Switched to branch 'master'
$ git merge issue2
Updating b2b23c4..8f7aa27
Fast-forward
 myfile.txt |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
This is fast-forward merge.
目前的历史记录


Now let's merge issue3:
$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
We can see the automatical merge failed because there is conflict on the same lines from two different branches. And we can see the conflict files become:
old stuff
add some new stuff
<<<<<<< HEAD
did something before
=======
pull something from some one else
>>>>>>> issue3
Git has generated the differences where the conflicts happened. We must change the file to:

old stuff
add some new stuff
did something before
pull something from some one else
Basically, manually fix the conflict and commit again: 

$ git add myfile.txt
$ git commit -m "merge issue3"
# On branch master
nothing to commit (working directory clean)
We can see this is not fast-forward but non fast-forward.
目前的历史记录

ref:
http://backlogtool.com/git-guide/cn/stepup/stepup2_7.html

Wednesday, 8 February 2017